Skip to content

Commit

Permalink
Adds tests for FindShadowBy annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiwari committed Jan 27, 2020
1 parent c48d244 commit d35701f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<WebElement> elementsResult = by.findElements(driver);
assertThat(elementsResult).hasSize(2);
assertThat(elementsResult).contains(innerShadow1, innerShadow2);
verify(driver, times(2)).executeScript(anyString(), any(WebElement.class));
}

@Test
Expand All @@ -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<WebElement> elementsResult = by.findElements(driver);
assertThat(elementsResult).isNotNull();
assertThat(elementsResult).isEmpty();
verify(driver, times(2)).executeScript(anyString(), any(WebElement.class));
}

private interface AllDriver extends
Expand Down

0 comments on commit d35701f

Please sign in to comment.