ByAll was re-implemented.#680
ByAll was re-implemented.#680TikhomirovSergey merged 6 commits intoappium:masterappium/java-client:masterfrom HlebHalkouski:masterCopy head branch name to clipboard
Conversation
…or single search.
| public ByAll(By[] bys) { | ||
| super(bys); | ||
| checkNotNull(bys); | ||
| if (bys.length == 0) { |
There was a problem hiding this comment.
better to use isEmpty()
| private By[] bys; | ||
|
|
||
| private Function<SearchContext, WebElement> getSearchingFunction(By by) { | ||
| return input -> { |
There was a problem hiding this comment.
return (input) -> ... is more readable
|
|
||
| private By[] bys; | ||
|
|
||
| private Function<SearchContext, WebElement> getSearchingFunction(By by) { |
There was a problem hiding this comment.
I'd mark it with Nullable annotation
There was a problem hiding this comment.
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.
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.
@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.
If you know how to need to do it without double invoking searching function, show me please.
There was a problem hiding this comment.
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.
I'd keep it as List<By> internally
|
|
||
| public class ByAll extends org.openqa.selenium.support.pagefactory.ByAll { | ||
|
|
||
| private List<By> bys; |
| private Function<SearchContext, Optional<WebElement>> getSearchingFunction(By by) { | ||
| return input -> { | ||
| try { | ||
| return input.findElement(by); |
There was a problem hiding this comment.
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.
why this is needed?
There was a problem hiding this comment.
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.
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.
I need a vacation
There was a problem hiding this comment.
functional programming makes fun :)
| checkNotNull(bys); | ||
|
|
||
| this.bys = Arrays.asList(bys); | ||
| if (this.bys.isEmpty()) { |
There was a problem hiding this comment.
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.
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
xin 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
findElementsmethod and return the first element in list.