-
-
Notifications
You must be signed in to change notification settings - Fork 762
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ByAll was re-implemented. #680
Conversation
…or single search.
public ByAll(By[] bys) { | ||
super(bys); | ||
checkNotNull(bys); | ||
if (bys.length == 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
better to use isEmpty()
private By[] bys; | ||
|
||
private Function<SearchContext, WebElement> getSearchingFunction(By by) { | ||
return input -> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return (input) -> ... is more readable
|
||
private By[] bys; | ||
|
||
private Function<SearchContext, WebElement> getSearchingFunction(By by) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd mark it with Nullable annotation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or it would be even better to return an Optional value
|
||
@Override | ||
public WebElement findElement(SearchContext context) { | ||
for (By by : bys) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return bys.stream()
.map(x -> getSearchingFunction(x).apply(context))
.filter(Optional::isPresent)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString()));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mykola-mokhnach I don't think that we can use stream in this case, because it returns By
type not WebElement
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you know how to need to do it without double invoking searching function, show me please.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can. Updated my comment above
|
||
public class ByAll extends org.openqa.selenium.support.pagefactory.ByAll { | ||
|
||
private By[] bys; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd keep it as List<By>
internally
|
||
public class ByAll extends org.openqa.selenium.support.pagefactory.ByAll { | ||
|
||
private List<By> bys; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be final
private Function<SearchContext, Optional<WebElement>> getSearchingFunction(By by) { | ||
return input -> { | ||
try { | ||
return input.findElement(by); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Optional.of(input.findElement(by))
.filter(Optional::isPresent) | ||
.findFirst() | ||
.orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString())) | ||
.orElse(null); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this is needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is double Optional : Optional<Optional<WebElement>
. First orElseThrow
for filter, the second orElse
for searching function. I can use get
instead orElse
, because we already check Optional::isPresent
, but there is warning here: "Optional.get()' without 'isPresent()' check"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be easier to read then
bys.stream()
.map(by -> getSearchingFunction(by).apply(context))
.filter(Optional::isPresent)
.map(Optional::get)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Cannot locate an element using " + toString()));
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need a vacation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
functional programming makes fun :)
checkNotNull(bys); | ||
|
||
this.bys = Arrays.asList(bys); | ||
if (this.bys.isEmpty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can also use http://google.github.io/guava/releases/snapshot/api/docs/com/google/common/base/Preconditions.html#checkArgument(boolean) since checkNotNull is already there
BTW, do we have any functional or unit test, which covers this method? |
return bys.stream() | ||
.map(by -> getSearchingFunction(by).apply(context)) | ||
.filter(Optional::isPresent) | ||
.findFirst().map(Optional::get) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mapping should be done before findFirst
@mykola-mokhnach So can we merge it or do you have some concerns/remarks? |
@TikhomirovSergey There was only one minor comment about checkArgument usage. Everything else looks good. |
@HlebHalkouski @mykola-mokhnach I will merge it soon. |
@TikhomirovSergey ok, Thank you! |
Change list
ByAll was re-implemented. Now it return the fist founded element for single search.
Types of changes
What types of changes are you proposing/introducing to Java client?
Put an
x
in the boxes that applyDetails
This changes improve time of seaching single element for 'All Possible' strategy. Now it doesn't search all possible elements for single.
Selenium implementation was for single search: invoke
findElements
method and return the first element in list.