-
-
Notifications
You must be signed in to change notification settings - Fork 758
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
Changes from 3 commits
d1a9e51
a06a929
5b7955b
0cf1f9e
cef9efe
3917b52
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package io.appium.java_client.pagefactory.bys.builder; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
import org.openqa.selenium.By; | ||
import org.openqa.selenium.NoSuchElementException; | ||
import org.openqa.selenium.SearchContext; | ||
import org.openqa.selenium.WebElement; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
|
||
|
||
public class ByAll extends org.openqa.selenium.support.pagefactory.ByAll { | ||
|
||
private final List<By> bys; | ||
|
||
private Function<SearchContext, Optional<WebElement>> getSearchingFunction(By by) { | ||
return input -> { | ||
try { | ||
return Optional.of(input.findElement(by)); | ||
} catch (NoSuchElementException e) { | ||
return Optional.empty(); | ||
} | ||
}; | ||
} | ||
|
||
/** | ||
* @param bys is a set of {@link org.openqa.selenium.By} which forms the all possible searching. | ||
*/ | ||
public ByAll(By[] bys) { | ||
super(bys); | ||
checkNotNull(bys); | ||
|
||
this.bys = Arrays.asList(bys); | ||
if (this.bys.isEmpty()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
throw new IllegalArgumentException("By array should not be empty"); | ||
} | ||
} | ||
|
||
@Override | ||
public WebElement findElement(SearchContext context) { | ||
return bys.stream() | ||
.map(by -> getSearchingFunction(by).apply(context)) | ||
.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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. This is double Optional : There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. functional programming makes fun :) |
||
} | ||
} |
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