Skip to content
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

Issue 9549 #9557

Merged
merged 3 commits into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions java/client/src/org/openqa/selenium/support/PageFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.openqa.selenium.support;

import org.openqa.selenium.SearchContext;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.pagefactory.DefaultElementLocatorFactory;
import org.openqa.selenium.support.pagefactory.DefaultFieldDecorator;
Expand Down Expand Up @@ -51,30 +52,30 @@ public class PageFactory {
* which takes a WebDriver instance as its only argument or falling back on a no-arg constructor.
* An exception will be thrown if the class cannot be instantiated.
*
* @param driver The driver that will be used to look up the elements
* @param searchContext The search context that will be used to look up the elements
* @param pageClassToProxy A class which will be initialised.
* @param <T> Class of the PageObject
* @return An instantiated instance of the class with WebElement and List&lt;WebElement&gt;
* fields proxied
* @see FindBy
* @see CacheLookup
*/
public static <T> T initElements(WebDriver driver, Class<T> pageClassToProxy) {
T page = instantiatePage(driver, pageClassToProxy);
initElements(driver, page);
public static <T> T initElements(SearchContext searchContext, Class<T> pageClassToProxy) {
T page = instantiatePage(searchContext, pageClassToProxy);
initElements(searchContext, page);
return page;
}

/**
* As {@link #initElements(WebDriver, Class)} but will only replace the fields of an already
* As {@link #initElements(SearchContext, Class)} but will only replace the fields of an already
* instantiated Page Object.
*
* @param driver The driver that will be used to look up the elements
* @param searchContext The driver that will be used to look up the elements
* @param page The object with WebElement and List&lt;WebElement&gt; fields that
* should be proxied.
*/
public static void initElements(WebDriver driver, Object page) {
initElements(new DefaultElementLocatorFactory(driver), page);
public static void initElements(SearchContext searchContext, Object page) {
initElements(new DefaultElementLocatorFactory(searchContext), page);
}

/**
Expand Down Expand Up @@ -119,11 +120,11 @@ private static void proxyFields(FieldDecorator decorator, Object page, Class<?>
}
}

private static <T> T instantiatePage(WebDriver driver, Class<T> pageClassToProxy) {
private static <T> T instantiatePage(SearchContext searchContext, Class<T> pageClassToProxy) {
try {
try {
Constructor<T> constructor = pageClassToProxy.getConstructor(WebDriver.class);
return constructor.newInstance(driver);
return constructor.newInstance(searchContext);
} catch (NoSuchMethodException e) {
return pageClassToProxy.getDeclaredConstructor().newInstance();
}
Expand Down
23 changes: 12 additions & 11 deletions java/client/test/org/openqa/selenium/support/PageFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.junit.experimental.categories.Category;
import org.mockito.ArgumentMatchers;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.SearchContext;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
Expand All @@ -41,7 +42,7 @@
@Category(UnitTests.class)
public class PageFactoryTest {

private WebDriver driver;
private SearchContext searchContext;

@Test
public void shouldProxyElementsInAnInstantiatedPage() {
Expand All @@ -50,15 +51,15 @@ public void shouldProxyElementsInAnInstantiatedPage() {
assertThat(page.q).isNull();
assertThat(page.list).isNull();

PageFactory.initElements(driver, page);
PageFactory.initElements(searchContext, page);

assertThat(page.q).isNotNull();
assertThat(page.list).isNotNull();
}

@Test
public void shouldInsertProxiesForPublicWebElements() {
PublicPage page = PageFactory.initElements(driver, PublicPage.class);
PublicPage page = PageFactory.initElements(searchContext, PublicPage.class);

assertThat(page.q).isNotNull();
assertThat(page.list).isNotNull();
Expand All @@ -68,7 +69,7 @@ public void shouldInsertProxiesForPublicWebElements() {
public void shouldProxyElementsFromParentClassesToo() {
ChildPage page = new ChildPage();

PageFactory.initElements(driver, page);
PageFactory.initElements(searchContext, page);

assertThat(page.q).isNotNull();
assertThat(page.list).isNotNull();
Expand All @@ -77,7 +78,7 @@ public void shouldProxyElementsFromParentClassesToo() {

@Test
public void shouldProxyRenderedWebElementFields() {
PublicPage page = PageFactory.initElements(driver, PublicPage.class);
PublicPage page = PageFactory.initElements(searchContext, PublicPage.class);

assertThat(page.rendered).isNotNull();
}
Expand All @@ -86,15 +87,15 @@ public void shouldProxyRenderedWebElementFields() {
public void shouldProxyPrivateElements() {
PrivatePage page = new PrivatePage();

PageFactory.initElements(driver, page);
PageFactory.initElements(searchContext, page);

assertThat(page.getField()).isNotNull();
assertThat(page.getList()).isNotNull();
}

@Test
public void shouldUseAConstructorThatTakesAWebDriverAsAnArgument() {
driver = mock(WebDriver.class);
WebDriver driver = mock(WebDriver.class);

ConstructedPage page = PageFactory.initElements(driver, ConstructedPage.class);

Expand Down Expand Up @@ -127,7 +128,7 @@ public void triesToDecorateNonWebElements() {
public void shouldNotDecorateListsOfWebElementsThatAreNotAnnotated() {
UnmarkedListPage page = new UnmarkedListPage();

PageFactory.initElements(driver, page);
PageFactory.initElements(searchContext, page);

assertThat(page.elements).isNull();
}
Expand All @@ -136,7 +137,7 @@ public void shouldNotDecorateListsOfWebElementsThatAreNotAnnotated() {
public void shouldNotDecorateListsThatAreTypedButNotWebElementLists() {
UnmarkedListPage page = new UnmarkedListPage();

PageFactory.initElements(driver, page);
PageFactory.initElements(searchContext, page);

assertThat(page.objects).isNull();
}
Expand All @@ -145,7 +146,7 @@ public void shouldNotDecorateListsThatAreTypedButNotWebElementLists() {
public void shouldNotDecorateUnTypedLists() {
UnmarkedListPage page = new UnmarkedListPage();

PageFactory.initElements(driver, page);
PageFactory.initElements(searchContext, page);

assertThat(page.untyped).isNull();
}
Expand All @@ -168,7 +169,7 @@ public void shouldComplainWhenMoreThanOneFindByShortFormAttributeIsSet() {

@Test
public void shouldNotThrowANoSuchElementExceptionWhenUsedWithAFluentWait() {
driver = mock(WebDriver.class);
WebDriver driver = mock(WebDriver.class);
when(driver.findElement(ArgumentMatchers.any())).thenThrow(new NoSuchElementException("because"));

TickingClock clock = new TickingClock();
Expand Down