1   package com.atlassian.pageobjects.elements.test;
2   
3   import com.atlassian.pageobjects.elements.PageElement;
4   import com.atlassian.pageobjects.elements.TimedElement;
5   import com.atlassian.pageobjects.elements.query.Poller;
6   import com.atlassian.pageobjects.elements.test.pageobjects.page.ElementsPage;
7   import com.atlassian.pageobjects.elements.timeout.TimeoutType;
8   import org.junit.Test;
9   import org.openqa.selenium.By;
10  
11  import static junit.framework.Assert.assertEquals;
12  import static org.hamcrest.Matchers.equalToIgnoringCase;
13  
14  public class TestTimedElement extends AbstractFileBasedServerTest
15  {
16      @Test
17      public void testIsPresent()
18      {
19          product.visit(ElementsPage.class);
20  
21          // Positive - verify element that exists
22          Poller.waitUntilTrue(product.find(By.id("test1_addElementsButton")).timed().isPresent());
23  
24          // Negative - verify element that does not exist
25          Poller.waitUntilFalse(product.find(By.id("non_present_element")).timed().isPresent());
26  
27          // Delayed presence & Delayed positive - click on button that adds a span with delay, verify isPresent waits.
28          product.find(By.id("test1_addElementsButton")).click();
29          Poller.waitUntilTrue(product.find(By.id("test1_delayedSpan")).timed().isPresent());
30  
31          // Delayed Negative
32      }
33  
34      @Test
35      public void testIsVisible()
36      {
37          product.visit(ElementsPage.class);
38  
39          TimedElement testInput = product.find(By.id("test2_input")).timed();
40  
41          // Positive - verify input that is visible
42          Poller.waitUntilTrue(testInput.isVisible());
43  
44          // Negative - click on button to make input invisible and verify
45          product.find(By.id("test2_toggleInputVisibility")).click();
46          Poller.waitUntilFalse(testInput.isVisible());
47  
48          // Delayed presence - click on a button that adds an element with delay, verify isVisible waits
49          product.find(By.id("test2_addElementsButton")).click();
50          Poller.waitUntilTrue(product.find(By.id("test2_delayedSpan")).timed().isVisible());
51  
52          // Delayed positive - click on button to make input visible with delay and verify
53          product.find(By.id("test2_toggleInputVisibilityWithDelay")).click();
54          Poller.waitUntilTrue(testInput.isVisible());
55  
56          // Delayed Negative
57      }
58  
59      @Test
60      public void testText()
61      {
62          product.visit(ElementsPage.class);
63  
64          // Positive - verify span with text
65          Poller.waitUntilEquals("Span Value", product.find(By.id("test3_span")).timed().getText());
66  
67          // check non-case-sensitive
68          Poller.waitUntil(product.find(By.id("test3_span")).timed().getText(), equalToIgnoringCase("span value"));
69  
70          // Negative - verify a span that has no text
71          Poller.waitUntilEquals("", product.find(By.id("test3_spanEmpty")).timed().getText());
72  
73          // Delayed presence - click on button that adds a span with delay, verify getText waits
74          product.find(By.id("test3_addElementsButton")).click();
75          Poller.waitUntilEquals("Delayed Span", product.find(By.id("test3_delayedSpan")).timed().getText());
76  
77          // Delayed postive - click on button that sets the text of span with delay, verify getText waits
78          product.find(By.id("test3_setTextButton")).click();
79          Poller.waitUntilEquals("Delayed Text", product.find(By.id("test3_spanEmpty")).timed().getText());
80  
81          // Delayed negative
82      }
83  
84      @Test
85      public void testHasText()
86      {
87          product.visit(ElementsPage.class);
88  
89          // Positive - verify span with text
90          Poller.waitUntilTrue(product.find(By.id("test3_span")).timed().hasText("Span Value"));
91  
92          // Negative - verify span with wrong text
93          Poller.waitUntilFalse(product.find(By.id("test3_spanEmpty")).timed().hasText("foo"));
94  
95          // Delayed presence - click on button that adds a span with delay, verify getText waits
96          product.find(By.id("test3_addElementsButton")).click();
97          Poller.waitUntilTrue(product.find(By.id("test3_delayedSpan")).timed().hasText("Delayed Span"));
98      }
99  
100     @Test
101     public void testAttribute()
102     {
103         product.visit(ElementsPage.class);
104 
105         // Positive - verify class attribute of span
106         Poller.waitUntilEquals("test5-input-class", product.find(By.id("test5_input")).timed().getAttribute("class"));
107 
108         // Negative
109 
110         // Delayed presence - click on button that adds a span with delay, verify getAtribute waits
111         product.find(By.id("test5_addElementsButton")).click();
112         Poller.waitUntilEquals("test5-span-delayed", product.find(By.id("test5_delayedSpan")).timed().getAttribute("class"));
113 
114         // Delayed positive - click on a button that adds attribute of a span, verify getAttribute waits
115         product.find(By.id("test5_addAttribute")).click();
116         Poller.waitUntilEquals("test5-input-value", product.find(By.id("test5_input")).timed().getAttribute("value"));
117 
118         // Delayed negative
119     }
120 
121     @Test
122     public void testTimedElementLocatesElementWithinAContext()
123     {
124         product.visit(ElementsPage.class);
125 
126         product.find(By.id("test6_hideSpanButton")).click();
127         Poller.waitUntilFalse(product.find(By.id("test6_Div")).find(By.className("test6_class")).timed().isVisible());
128     }
129 
130     @Test
131     public void testGetTagName()
132     {
133         product.visit(ElementsPage.class);
134 
135         product.find(By.id("test1_addElementsButton")).click();
136         Poller.waitUntilEquals("span", product.find(By.id("test1_delayedSpan")).timed().getTagName());
137     }
138 
139     @Test
140     public void testHasAttribute()
141     {
142         product.visit(ElementsPage.class);
143 
144         // Delayed presence
145         product.find(By.id("test1_addElementsButton")).click();
146         Poller.waitUntilTrue(product.find(By.id("test1_delayedSpan")).timed().hasAttribute("class", "testClass"));
147 
148         // incorrect attribute
149         Poller.waitUntilFalse(product.find(By.id("test1_delayedSpan")).timed().hasAttribute("class", "foo"));
150 
151         // attribute not present
152         Poller.waitUntilFalse(product.find(By.id("test1_delayedSpan")).timed().hasAttribute("nonexistant", "foo"));
153     }
154 
155     @Test
156     public void testTimedElementWithFindAll()
157     {
158         product.visit(ElementsPage.class);
159         PageElement leafList = product.find(By.id("test4_leafList"));
160         for (PageElement li : leafList.findAll(By.tagName("li")))
161         {
162             Poller.waitUntilTrue(li.timed().isPresent());
163         }
164     }
165 
166     @Test
167     public void testElementWithTimeout()
168     {
169         product.visit(ElementsPage.class);
170         PageElement element = product.find(By.id("test1_addElementsButton"));
171         assertEquals(element.timed().isPresent().defaultTimeout(), product.timeouts().timeoutFor(TimeoutType.DEFAULT));
172         element = element.withTimeout(TimeoutType.AJAX_ACTION);
173         assertEquals(element.timed().isPresent().defaultTimeout(), product.timeouts().timeoutFor(TimeoutType.AJAX_ACTION));
174     }
175 }