Example usage for org.openqa.selenium WebDriver findElements

List of usage examples for org.openqa.selenium WebDriver findElements

Introduction

In this page you can find the example usage for org.openqa.selenium WebDriver findElements.

Prototype

@Override
List<WebElement> findElements(By by);

Source Link

Document

Find all elements within the current page using the given mechanism.

Usage

From source file:projectGutenbergTest.GutenbergTest.java

@Test
public void Test3() {
    WebElement element = driver.findElement(By.id("titleAuthorWithCityTextBox"));
    element.sendKeys("Esbjerg");
    WebElement element2 = driver.findElement(By.name("ctl03"));
    element2.click();/*  w  w  w. j  a v a 2 s  .  c  om*/

    (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            int amount = d.findElements(By.xpath("//tbody/tr")).size();
            System.out.println("List amount is: " + amount);
            return amount == 6;
        }
    });

    //Get from file later
    expectedBooks = new ArrayList<ExpectedOutput>();
    expectedBooks.add(
            new ExpectedOutput("The 1994 CIA World Factbook", "United States Central Intelligence Agency,"));
    expectedBooks.add(
            new ExpectedOutput("The 1997 CIA World Factbook", "United States. Central Intelligence Agency.,"));
    expectedBooks.add(new ExpectedOutput("Denmark", "M. Pearson Thomson,"));
    expectedBooks.add(new ExpectedOutput("The 1990 CIA World Factbook",
            "M. United States. Central Intelligence Agency,"));
    expectedBooks.add(
            new ExpectedOutput("The 1998 CIA World Factbook", "United States. Central Intelligence Agency.,"));

    expectedBooks.get(0).title.equals(Compare(driver, 2).title);
    expectedBooks.get(1).title.equals(Compare(driver, 3).title);
    expectedBooks.get(2).title.equals(Compare(driver, 4).title);
    expectedBooks.get(3).title.equals(Compare(driver, 5).title);
    expectedBooks.get(4).title.equals(Compare(driver, 6).title);
}

From source file:projectGutenbergTest.GutenbergTest.java

@Test
public void Test8() {
    WebElement element = driver.findElement(By.id("mentionedInAreaLatitudeBox"));
    element.sendKeys("15");
    WebElement element2 = driver.findElement(By.id("mentionedInAreaLongitudeBox"));
    element2.sendKeys("43");
    WebElement element3 = driver.findElement(By.name("ctl08"));
    element3.click();/* w  w  w  . j a  v a  2s.  c om*/

    (new WebDriverWait(driver, maxTimer)).until(new ExpectedCondition<Boolean>() {
        public Boolean apply(WebDriver d) {
            int amount = d.findElements(By.xpath("//tbody/tr")).size();
            System.out.println("List amount is: " + amount);
            return amount == 5;
        }
    });

    //Get from file later
    List<String> Books = new ArrayList<String>();
    Books.add("The 1994 CIA World Factbook");
    Books.add("The 1997 CIA World Factbook");
    Books.add("The 1990 CIA World Factbook");
    Books.add("The 1998 CIA World Factbook");

    Books.get(0).equals(Compare(driver, 2).title);
    Books.get(1).equals(Compare(driver, 3).title);
    Books.get(2).equals(Compare(driver, 4).title);
    Books.get(3).equals(Compare(driver, 5).title);
}

From source file:ru.stqa.selenium.wait.ExpectedConditionsTest.java

License:Apache License

@Test
public void testFirstElementLocatedIsPresent() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final List<WebElement> list = new ArrayList<WebElement>() {
        {//from   w  w  w.  j  av a 2 s  .co m
            add(mock(WebElement.class));
            add(mock(WebElement.class));
        }
    };
    when(mockedDriver.findElements(By.id("my-id"))).thenReturn(new ArrayList<WebElement>()).thenReturn(list);

    TestingClock clock = new TestingClock();
    WebDriverWait wait = new Waiter(mockedDriver, clock, clock, 1, 100);

    WebElement element = wait.until(firstElementLocated(By.id("my-id"), isPresent()));

    assertEquals(element, list.get(0));
    assertEquals(clock.now(), 100L);
    verify(mockedDriver, times(2)).findElements(By.id("my-id"));
    verifyZeroInteractions(list.get(0));
    verifyZeroInteractions(list.get(1));
}

From source file:ru.stqa.selenium.wait.ExpectedConditionsTest.java

License:Apache License

@Test
public void testFirstElementLocatedIsDisplayed() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final List<WebElement> list = new ArrayList<WebElement>() {
        {//from w  ww.j av a2  s.  c o m
            add(mock(WebElement.class));
            add(mock(WebElement.class));
        }
    };
    when(mockedDriver.findElements(By.id("my-id"))).thenReturn(list);
    when(list.get(0).isDisplayed()).thenReturn(false).thenReturn(true);

    TestingClock clock = new TestingClock();
    WebDriverWait wait = new Waiter(mockedDriver, clock, clock, 1, 100);

    WebElement element = wait.until(firstElementLocated(By.id("my-id"), isVisible()));

    assertEquals(element, list.get(0));
    assertEquals(clock.now(), 100L);
    verify(mockedDriver, times(2)).findElements(By.id("my-id"));
    verify(list.get(0), times(2)).isDisplayed();
    verifyZeroInteractions(list.get(1));
}

From source file:ru.stqa.selenium.wait.ExpectedConditionsTest.java

License:Apache License

@Test
public void testSomeElementLocatedIsDisplayed() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final List<WebElement> list = new ArrayList<WebElement>() {
        {/*from   w  w w . j  a  va  2s  .  co  m*/
            add(mock(WebElement.class));
            add(mock(WebElement.class));
        }
    };
    when(mockedDriver.findElements(By.id("my-id"))).thenReturn(list);
    when(list.get(0).isDisplayed()).thenReturn(false).thenReturn(false).thenReturn(true);
    when(list.get(1).isDisplayed()).thenReturn(false).thenReturn(true).thenReturn(true);

    TestingClock clock = new TestingClock();
    WebDriverWait wait = new Waiter(mockedDriver, clock, clock, 1, 100);

    WebElement element = wait.until(someElementLocated(By.id("my-id"), isVisible()));

    assertEquals(element, list.get(1));
    assertEquals(clock.now(), 100L);
    verify(mockedDriver, times(2)).findElements(By.id("my-id"));
    verify(list.get(0), times(2)).isDisplayed();
    verify(list.get(1), times(2)).isDisplayed();
}

From source file:ru.stqa.selenium.wait.ExpectedConditionsTest.java

License:Apache License

@Test
public void testEachElementLocatedIsDisplayed() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final List<WebElement> list = new ArrayList<WebElement>() {
        {//from  w w  w  .ja v  a2s . c  o m
            add(mock(WebElement.class));
            add(mock(WebElement.class));
        }
    };
    when(mockedDriver.findElements(By.id("my-id"))).thenReturn(list);
    when(list.get(0).isDisplayed()).thenReturn(false).thenReturn(true);
    when(list.get(1).isDisplayed()).thenReturn(false).thenReturn(true);

    TestingClock clock = new TestingClock();
    WebDriverWait wait = new Waiter(mockedDriver, clock, clock, 1, 100);

    List<WebElement> elements = wait.until(eachElementLocated(By.id("my-id"), isVisible()));

    assertEquals(elements, list);
    assertEquals(clock.now(), 200L);
    verify(mockedDriver, times(3)).findElements(By.id("my-id"));
    verify(list.get(0), times(3)).isDisplayed();
    verify(list.get(1), times(2)).isDisplayed();
}

From source file:ru.stqa.selenium.wait.ExpectedConditionsTest.java

License:Apache License

@Test
public void testListOfElementsLocatedIsNotEmpty() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final List<WebElement> list = new ArrayList<WebElement>() {
        {// w ww .  j  a  va  2s  .c om
            add(mock(WebElement.class));
            add(mock(WebElement.class));
        }
    };
    when(mockedDriver.findElements(By.id("my-id"))).thenReturn(new ArrayList<WebElement>()).thenReturn(list);

    TestingClock clock = new TestingClock();
    WebDriverWait wait = new Waiter(mockedDriver, clock, clock, 1, 100);

    List<WebElement> elements = wait.until(listOfElementsLocated(By.id("my-id"), isNotEmpty()));

    assertEquals(elements, list);
    assertEquals(clock.now(), 100L);
    verify(mockedDriver, times(2)).findElements(By.id("my-id"));
    verifyZeroInteractions(list.get(0));
    verifyZeroInteractions(list.get(1));
}

From source file:ru.stqa.selenium.wait.RepeatableActionsTest.java

License:Apache License

@Test
public void findElementsActionShouldCallFindElements() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final WebElement mockedElement1 = mock(WebElement.class);
    final WebElement mockedElement2 = mock(WebElement.class);
    final List<WebElement> mockedElements = Lists.newArrayList(mockedElement1, mockedElement2);

    when(mockedDriver.findElements(By.name("foo"))).thenReturn(mockedElements);

    List<WebElement> elements = new ActionRepeater<WebDriver>(mockedDriver, 1, 1)
            .tryTo(performFindElements(By.name("foo")));

    verify(mockedDriver, times(1)).findElements(By.name("foo"));
    assertThat(elements, is(mockedElements));
}

From source file:ru.stqa.selenium.wait.RepeatableActionsTest.java

License:Apache License

@Test
public void findElementsActionShouldIgnoreEmptyList() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final WebElement mockedElement1 = mock(WebElement.class);
    final WebElement mockedElement2 = mock(WebElement.class);
    final List<WebElement> mockedElements = Lists.newArrayList(mockedElement1, mockedElement2);

    when(mockedDriver.findElements(By.name("foo"))).thenReturn(new ArrayList<WebElement>())
            .thenReturn(new ArrayList<WebElement>()).thenReturn(mockedElements);

    List<WebElement> elements = new ActionRepeater<WebDriver>(mockedDriver, 1, 1)
            .tryTo(performFindElements(By.name("foo")));

    verify(mockedDriver, times(3)).findElements(By.name("foo"));
    assertThat(elements, is(mockedElements));
}

From source file:ru.stqa.selenium.wait.RepeatableActionsTest.java

License:Apache License

@Test
public void findElementsActionShouldIgnoreNoSuchElementException() {
    final WebDriver mockedDriver = mock(WebDriver.class);
    final WebElement mockedElement1 = mock(WebElement.class);
    final WebElement mockedElement2 = mock(WebElement.class);
    final List<WebElement> mockedElements = Lists.newArrayList(mockedElement1, mockedElement2);

    when(mockedDriver.findElements(By.name("foo"))).thenThrow(NoSuchElementException.class)
            .thenThrow(NoSuchElementException.class).thenReturn(mockedElements);

    List<WebElement> elements = new ActionRepeater<WebDriver>(mockedDriver, 1, 1)
            .tryTo(performFindElements(By.name("foo")));

    verify(mockedDriver, times(3)).findElements(By.name("foo"));
    assertThat(elements, is(mockedElements));
}