Skip to content

Commit 323132d

Browse files
committed
PageFactory: add field when constructing the locator.
1 parent 0b30188 commit 323132d

File tree

6 files changed

+29
-17
lines changed

6 files changed

+29
-17
lines changed

java/client/src/org/openqa/selenium/support/AbstractFindByBuilder.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@
1919

2020
import org.openqa.selenium.By;
2121

22+
import java.lang.reflect.Field;
2223
import java.util.HashSet;
2324
import java.util.Set;
2425

2526
public abstract class AbstractFindByBuilder {
2627

27-
public abstract By buildIt(Object annotation);
28+
public abstract By buildIt(Object annotation, Field field);
2829

2930
protected By buildByFromFindBy(FindBy findBy) {
3031
assertValidFindBy(findBy);
@@ -38,29 +39,37 @@ protected By buildByFromFindBy(FindBy findBy) {
3839
}
3940

4041
protected By buildByFromShortFindBy(FindBy findBy) {
41-
if (!"".equals(findBy.className()))
42+
if (!"".equals(findBy.className())) {
4243
return By.className(findBy.className());
44+
}
4345

44-
if (!"".equals(findBy.css()))
46+
if (!"".equals(findBy.css())) {
4547
return By.cssSelector(findBy.css());
48+
}
4649

47-
if (!"".equals(findBy.id()))
50+
if (!"".equals(findBy.id())) {
4851
return By.id(findBy.id());
52+
}
4953

50-
if (!"".equals(findBy.linkText()))
54+
if (!"".equals(findBy.linkText())) {
5155
return By.linkText(findBy.linkText());
56+
}
5257

53-
if (!"".equals(findBy.name()))
58+
if (!"".equals(findBy.name())) {
5459
return By.name(findBy.name());
60+
}
5561

56-
if (!"".equals(findBy.partialLinkText()))
62+
if (!"".equals(findBy.partialLinkText())) {
5763
return By.partialLinkText(findBy.partialLinkText());
64+
}
5865

59-
if (!"".equals(findBy.tagName()))
66+
if (!"".equals(findBy.tagName())) {
6067
return By.tagName(findBy.tagName());
68+
}
6169

62-
if (!"".equals(findBy.xpath()))
70+
if (!"".equals(findBy.xpath())) {
6371
return By.xpath(findBy.xpath());
72+
}
6473

6574
// Fall through
6675
return null;

java/client/src/org/openqa/selenium/support/FindAll.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.lang.annotation.Retention;
2626
import java.lang.annotation.RetentionPolicy;
2727
import java.lang.annotation.Target;
28+
import java.lang.reflect.Field;
2829

2930
/**
3031
* Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags
@@ -47,7 +48,7 @@
4748
FindBy[] value();
4849

4950
public static class FindByBuilder extends AbstractFindByBuilder {
50-
public By buildIt(Object annotation) {
51+
public By buildIt(Object annotation, Field field) {
5152
FindAll findBys = (FindAll) annotation;
5253
assertValidFindAll(findBys);
5354

java/client/src/org/openqa/selenium/support/FindBy.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.annotation.Retention;
2525
import java.lang.annotation.RetentionPolicy;
2626
import java.lang.annotation.Target;
27+
import java.lang.reflect.Field;
2728

2829
/**
2930
* <p>Used to mark a field on a Page Object to indicate an alternative mechanism for locating the
@@ -77,7 +78,7 @@
7778
String xpath() default "";
7879

7980
public static class FindByBuilder extends AbstractFindByBuilder {
80-
public By buildIt(Object annotation) {
81+
public By buildIt(Object annotation, Field field) {
8182
FindBy findBy = (FindBy) annotation;
8283
assertValidFindBy(findBy);
8384

java/client/src/org/openqa/selenium/support/FindBys.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.lang.annotation.Retention;
2525
import java.lang.annotation.RetentionPolicy;
2626
import java.lang.annotation.Target;
27+
import java.lang.reflect.Field;
2728

2829
/**
2930
* Used to mark a field on a Page Object to indicate that lookup should use a series of @FindBy tags
@@ -45,7 +46,7 @@
4546
FindBy[] value();
4647

4748
public static class FindByBuilder extends AbstractFindByBuilder {
48-
public By buildIt(Object annotation) {
49+
public By buildIt(Object annotation, Field field) {
4950
FindBys findBys = (FindBys) annotation;
5051
assertValidFindBys(findBys);
5152

java/client/src/org/openqa/selenium/support/pagefactory/Annotations.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,14 @@ public By buildBy() {
6969
builder = annotation.annotationType()
7070
.getAnnotation(PageFactoryFinder.class).value()
7171
.newInstance();
72-
} catch (InstantiationException e) {
73-
} catch (IllegalAccessException e) {
72+
} catch (ReflectiveOperationException e) {
73+
// Fall through.
7474
}
7575
}
7676
if (builder != null) {
77-
ans = builder.buildIt(annotation);
77+
ans = builder.buildIt(annotation, field);
7878
break;
7979
}
80-
8180
}
8281

8382
if (ans == null) {

java/client/test/org/openqa/selenium/support/pagefactory/AnnotationsTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.lang.annotation.Retention;
4141
import java.lang.annotation.RetentionPolicy;
4242
import java.lang.annotation.Target;
43+
import java.lang.reflect.Field;
4344
import java.util.List;
4445

4546
@RunWith(JUnit4.class)
@@ -109,7 +110,7 @@ public class AnnotationsTest {
109110
public static class FindByXXXXBuilder extends AbstractFindByBuilder {
110111

111112
@Override
112-
public By buildIt(Object annotation) {
113+
public By buildIt(Object annotation, Field field) {
113114
return new By() {
114115
@Override
115116
public List<WebElement> findElements(SearchContext context) {

0 commit comments

Comments
 (0)