1 package com.atlassian.pageobjects.elements.test.locatable;
2
3 import com.atlassian.pageobjects.elements.WebDriverLocatable;
4 import com.atlassian.pageobjects.elements.WebDriverLocators;
5 import org.junit.Test;
6 import org.junit.runner.RunWith;
7 import org.mockito.Mock;
8 import org.mockito.runners.MockitoJUnitRunner;
9 import org.openqa.selenium.By;
10 import org.openqa.selenium.NoSuchElementException;
11 import org.openqa.selenium.WebDriver;
12 import org.openqa.selenium.WebElement;
13
14 import static com.atlassian.pageobjects.elements.WebDriverLocatable.LocateTimeout.zero;
15 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
16 import static org.junit.Assert.*;
17 import static org.mockito.Mockito.when;
18
19 @RunWith(MockitoJUnitRunner.class)
20 public class TestWebDriverLocators
21 {
22
23 @Mock
24 private WebDriver webDriver;
25
26 @Mock
27 private WebElement webElement;
28
29 @Test
30 public void testRoot()
31 {
32 WebDriverLocatable root = WebDriverLocators.root();
33 assertNull(root.getParent());
34 assertNull(root.getLocator());
35 assertTrue(root.isPresent(webDriver, zero()));
36 assertSame(webDriver, root.waitUntilLocated(webDriver, zero()));
37 }
38
39 @Test
40 public void testSingleLocatorUsesProvidedPollInterval()
41 {
42 when(webDriver.findElement(By.id("id")))
43 .thenThrow(new NoSuchElementException("No"))
44 .thenThrow(new NoSuchElementException("No"))
45 .thenReturn(webElement);
46
47 WebDriverLocatable locatable = WebDriverLocators.single(By.id("id"));
48 assertNotNull(locatable.getParent());
49
50 assertNotNull(locatable.waitUntilLocated(webDriver,
51 WebDriverLocatable.LocateTimeout.builder().timeout(10).pollInterval(1).build()));
52 }
53
54 @Test(expected = NoSuchElementException.class)
55 public void testSingleLocatorFailsIfAfterTimeoutExpires()
56 {
57 when(webDriver.findElement(By.id("id"))).thenThrow(new NoSuchElementException("No"));
58
59 WebDriverLocatable locatable = WebDriverLocators.single(By.id("id"));
60 assertNotNull(locatable.getParent());
61
62 final long start = System.currentTimeMillis();
63 try
64 {
65 locatable.waitUntilLocated(webDriver,
66 WebDriverLocatable.LocateTimeout.builder().timeout(5).pollInterval(1).build());
67 }
68 finally
69 {
70
71 assertThat(System.currentTimeMillis() - start, greaterThanOrEqualTo(5L));
72 }
73 }
74 }