Skip to content

Commit d1a9e51

Browse files
committed
By All was re-implemented, now it returns the first founded element for single search.
1 parent ac44a16 commit d1a9e51

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

src/main/java/io/appium/java_client/pagefactory/DefaultElementByBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.appium.java_client.pagefactory.bys.ContentMappedBy;
2424
import io.appium.java_client.pagefactory.bys.ContentType;
2525
import io.appium.java_client.pagefactory.bys.builder.AppiumByBuilder;
26+
import io.appium.java_client.pagefactory.bys.builder.ByAll;
2627
import io.appium.java_client.pagefactory.bys.builder.ByChained;
2728
import io.appium.java_client.pagefactory.bys.builder.HowToUseSelectors;
2829
import org.openqa.selenium.By;
@@ -31,7 +32,6 @@
3132
import org.openqa.selenium.support.FindAll;
3233
import org.openqa.selenium.support.FindBy;
3334
import org.openqa.selenium.support.FindBys;
34-
import org.openqa.selenium.support.pagefactory.ByAll;
3535

3636
import java.lang.annotation.Annotation;
3737
import java.lang.reflect.AnnotatedElement;

src/main/java/io/appium/java_client/pagefactory/bys/builder/AppiumByBuilder.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.openqa.selenium.By;
2626
import org.openqa.selenium.support.pagefactory.AbstractAnnotations;
27-
import org.openqa.selenium.support.pagefactory.ByAll;
2827

2928
import java.lang.annotation.Annotation;
3029
import java.lang.reflect.AnnotatedElement;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package io.appium.java_client.pagefactory.bys.builder;
2+
3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.NoSuchElementException;
7+
import org.openqa.selenium.SearchContext;
8+
import org.openqa.selenium.WebElement;
9+
10+
import java.util.function.Function;
11+
12+
13+
public class ByAll extends org.openqa.selenium.support.pagefactory.ByAll {
14+
15+
private By[] bys;
16+
17+
private Function<SearchContext, WebElement> getSearchingFunction(By by) {
18+
return input -> {
19+
try {
20+
return input.findElement(by);
21+
} catch (NoSuchElementException e) {
22+
return null;
23+
}
24+
};
25+
}
26+
27+
/**
28+
* @param bys is a set of {@link org.openqa.selenium.By} which forms the all possible searching.
29+
*/
30+
public ByAll(By[] bys) {
31+
super(bys);
32+
checkNotNull(bys);
33+
if (bys.length == 0) {
34+
throw new IllegalArgumentException("By array should not be empty");
35+
}
36+
this.bys = bys;
37+
}
38+
39+
@Override
40+
public WebElement findElement(SearchContext context) {
41+
for (By by : bys) {
42+
WebElement element = getSearchingFunction(by).apply(context);
43+
if (element != null) {
44+
return element;
45+
}
46+
}
47+
throw new NoSuchElementException("Cannot locate an element using " + toString());
48+
}
49+
}

0 commit comments

Comments
 (0)