From 4426fffe9f9c999f236710c016b1406b4d1900de Mon Sep 17 00:00:00 2001 From: Eduardo Wermuth Date: Tue, 31 Dec 2019 15:21:26 -0500 Subject: [PATCH] Adds tests for FindShadowBy annotation --- .../support/pagefactory/AnnotationsTest.java | 22 ++++++++ .../pagefactory/ByChainedShadowTest.java | 56 ++++++++++--------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/java/client/test/org/openqa/selenium/support/pagefactory/AnnotationsTest.java b/java/client/test/org/openqa/selenium/support/pagefactory/AnnotationsTest.java index 21f38ec729c7a..c4ad2ef10cc23 100644 --- a/java/client/test/org/openqa/selenium/support/pagefactory/AnnotationsTest.java +++ b/java/client/test/org/openqa/selenium/support/pagefactory/AnnotationsTest.java @@ -29,6 +29,7 @@ import org.openqa.selenium.support.FindAll; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.FindBys; +import org.openqa.selenium.support.FindShadowBy; import org.openqa.selenium.support.How; import org.openqa.selenium.support.PageFactoryFinder; @@ -94,6 +95,14 @@ public class AnnotationsTest { @FindBy(id = "crackers")}) public WebElement findAllMultipleHows_field; + @FindShadowBy({@FindBy(xpath = "foo"), + @FindBy(css = "bar")}) + public WebElement findShadowBy_field; + + @FindShadowBy({@FindBy(xpath = "foo", css = "cheese"), + @FindBy(css = "bar")}) + public WebElement findShadowMultipleHows_field; + @FindBy(using = "cheese") public WebElement findByUnsetHow_field; @@ -240,4 +249,17 @@ public void findBySomethingElse() throws Exception { .isEqualTo("FindByXXXX's By"); } + @Test + public void findByShadow() throws NoSuchFieldException { + assertThat(new Annotations(getClass().getField("findShadowBy_field")).buildBy()) + .isEqualTo(new ByChainedShadow(By.xpath("foo"), By.cssSelector("bar"))); + } + + @Test + public void findByShadowMultipleHows() { + assertThatExceptionOfType(IllegalArgumentException.class) + .describedAs("Expected field annotated with @FindAll containing bad @FindBy to throw error") + .isThrownBy(() -> new Annotations(getClass().getField("findShadowMultipleHows_field")).buildBy()); + } + } diff --git a/java/client/test/org/openqa/selenium/support/pagefactory/ByChainedShadowTest.java b/java/client/test/org/openqa/selenium/support/pagefactory/ByChainedShadowTest.java index 04874b62a5de5..88aed06f91938 100644 --- a/java/client/test/org/openqa/selenium/support/pagefactory/ByChainedShadowTest.java +++ b/java/client/test/org/openqa/selenium/support/pagefactory/ByChainedShadowTest.java @@ -3,7 +3,10 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -20,11 +23,10 @@ import java.util.Arrays; import java.util.Collections; +import java.util.List; public class ByChainedShadowTest { - public static final String SHADOW_ROOT_SCRIPT = "arguments[0].shadowRoot"; - @Test public void testEmptyBysFindElement() { AllDriver driver = mock(AllDriver.class); @@ -55,6 +57,7 @@ public void testNonEmptyFindElement() { ByChainedShadow by = new ByChainedShadow(By.xpath("xpath")); assertThat(by.findElement(driver)).isEqualTo(element1); + verify(driver, never()).executeScript(any(), any()); } @Test @@ -68,6 +71,7 @@ public void testNonEmptyFindElements() { ByChainedShadow by = new ByChainedShadow(By.xpath("xpath")); assertThat(by.findElements(driver)).hasSize(2); assertThat(by.findElements(driver)).contains(element1, element2); + verify(driver, never()).executeScript(any(), any()); } @Test @@ -81,16 +85,14 @@ public void testMultipleBysFindElement() { WebElement innerShadow2 = mock(WebElement.class); when(driver.findElementsByXPath("xpath")).thenReturn(Lists.list(element1, element2)); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element1)).thenReturn(shadowElement1); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element2)).thenReturn(shadowElement2); - when(shadowElement1.findElements(By.cssSelector("css"))) - .thenReturn(Collections.singletonList(innerShadow1)); - when(shadowElement2.findElements(By.cssSelector("css"))) - .thenReturn(Collections.singletonList(innerShadow2)); + when(driver.executeScript(anyString(), eq(element1))).thenReturn(shadowElement1); + when(driver.executeScript(anyString(), eq(element2))).thenReturn(shadowElement2); + when(shadowElement1.findElements(By.cssSelector("css"))).thenReturn(Collections.singletonList(innerShadow1)); + when(shadowElement2.findElements(By.cssSelector("css"))).thenReturn(Collections.singletonList(innerShadow2)); ByChainedShadow by = new ByChainedShadow(By.xpath("xpath"), By.cssSelector("css")); - verify(driver, times(2)).executeScript(any()); assertThat(by.findElement(driver)).isEqualTo(innerShadow1); + verify(driver, times(2)).executeScript(anyString(), any(WebElement.class)); } @Test @@ -102,15 +104,15 @@ public void testMultipleBysErrorFindElement() { WebElement shadowElement2 = mock(WebElement.class); when(driver.findElementsByXPath("xpath")).thenReturn(Lists.list(element1, element2)); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element1)).thenReturn(shadowElement1); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element2)).thenReturn(shadowElement2); + when(driver.executeScript(anyString(), eq(element1))).thenReturn(shadowElement1); + when(driver.executeScript(anyString(), eq(element2))).thenReturn(shadowElement2); when(shadowElement1.findElements(By.cssSelector("css"))).thenReturn(Collections.emptyList()); when(shadowElement2.findElements(By.cssSelector("css"))).thenReturn(Collections.emptyList()); ByChainedShadow by = new ByChainedShadow(By.xpath("xpath"), By.cssSelector("css")); - verify(driver, times(2)).executeScript(any()); assertThatExceptionOfType(NoSuchElementException.class) .isThrownBy(() -> by.findElement(driver)); + verify(driver, times(2)).executeScript(anyString(), any(WebElement.class)); } @Test @@ -123,18 +125,17 @@ public void testMultipleBysFindElements() { WebElement innerShadow1 = mock(WebElement.class); WebElement innerShadow2 = mock(WebElement.class); - when(driver.findElementsByXPath("xpath")).thenReturn(Lists.list(element1, element1)); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element1)).thenReturn(shadowElement1); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element2)).thenReturn(shadowElement2); - when(shadowElement1.findElements(By.cssSelector("css"))) - .thenReturn(Collections.singletonList(innerShadow1)); - when(shadowElement2.findElements(By.cssSelector("css"))) - .thenReturn(Collections.singletonList(innerShadow2)); + when(driver.findElementsByXPath("xpath")).thenReturn(Lists.list(element1, element2)); + when(driver.executeScript(anyString(), eq(element1))).thenReturn(shadowElement1); + when(driver.executeScript(anyString(), eq(element2))).thenReturn(shadowElement2); + when(shadowElement1.findElements(By.cssSelector("css"))).thenReturn(Collections.singletonList(innerShadow1)); + when(shadowElement2.findElements(By.cssSelector("css"))).thenReturn(Collections.singletonList(innerShadow2)); ByChainedShadow by = new ByChainedShadow(By.xpath("xpath"), By.cssSelector("css")); - verify(driver, times(2)).executeScript(any()); - assertThat(by.findElements(driver)).hasSize(2); - assertThat(by.findElements(driver)).contains(innerShadow1, innerShadow2); + List elementsResult = by.findElements(driver); + assertThat(elementsResult).hasSize(2); + assertThat(elementsResult).contains(innerShadow1, innerShadow2); + verify(driver, times(2)).executeScript(anyString(), any(WebElement.class)); } @Test @@ -146,15 +147,16 @@ public void testMultipleBysEmptyFindElements() { WebElement shadowElement2 = mock(WebElement.class); when(driver.findElementsByXPath("xpath")).thenReturn(Lists.list(element1, element1)); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element1)).thenReturn(shadowElement1); - when(driver.executeScript(SHADOW_ROOT_SCRIPT, element2)).thenReturn(shadowElement2); + when(driver.executeScript(anyString(), eq(element1))).thenReturn(shadowElement1); + when(driver.executeScript(anyString(), eq(element2))).thenReturn(shadowElement2); when(shadowElement1.findElements(By.cssSelector("css"))).thenReturn(Collections.emptyList()); when(shadowElement2.findElements(By.cssSelector("css"))).thenReturn(Collections.emptyList()); ByChainedShadow by = new ByChainedShadow(By.xpath("xpath"), By.cssSelector("css")); - verify(driver, times(2)).executeScript(any()); - assertThat(by.findElements(driver)).isNotNull(); - assertThat(by.findElements(driver)).isEmpty(); + List elementsResult = by.findElements(driver); + assertThat(elementsResult).isNotNull(); + assertThat(elementsResult).isEmpty(); + verify(driver, times(2)).executeScript(anyString(), any(WebElement.class)); } private interface AllDriver extends