View Javadoc

1   package com.atlassian.pageobjects.elements.test;
2   
3   import com.atlassian.pageobjects.browser.Browser;
4   import com.atlassian.pageobjects.elements.GlobalElementFinder;
5   import com.atlassian.pageobjects.elements.Options;
6   import com.atlassian.pageobjects.elements.PageElement;
7   import com.atlassian.pageobjects.elements.PageElementFinder;
8   import com.atlassian.pageobjects.elements.SelectElement;
9   import com.atlassian.pageobjects.elements.WebDriverElement;
10  import com.atlassian.pageobjects.elements.query.Poller;
11  import com.atlassian.pageobjects.elements.test.pageobjects.page.DynamicPage;
12  import com.atlassian.pageobjects.elements.test.pageobjects.page.ElementsPage;
13  import com.atlassian.pageobjects.elements.timeout.TimeoutType;
14  import com.atlassian.webdriver.testing.annotation.IgnoreBrowser;
15  import com.atlassian.webdriver.utils.by.ByJquery;
16  import org.junit.Before;
17  import org.junit.Test;
18  import org.openqa.selenium.By;
19  
20  import static com.atlassian.pageobjects.elements.query.Poller.waitUntilEquals;
21  import static com.atlassian.pageobjects.elements.query.Poller.waitUntilFalse;
22  import static com.atlassian.pageobjects.elements.query.Poller.waitUntilTrue;
23  import static junit.framework.Assert.assertEquals;
24  import static junit.framework.Assert.assertFalse;
25  import static junit.framework.Assert.assertNotNull;
26  import static junit.framework.Assert.assertTrue;
27  
28  @IgnoreBrowser(Browser.HTMLUNIT_NOJS)
29  public class TestElement extends AbstractFileBasedServerTest
30  {
31      private PageElementFinder elementFinder;
32  
33      @Before
34      public void initFinder()
35      {
36          elementFinder = product.getPageBinder().bind(GlobalElementFinder.class);
37      }
38  
39      @Test
40      public void testFieldInjection()
41      {
42          ElementsPage elementsPage = product.visit(ElementsPage.class);
43  
44          // click on a button that was injected via @ElementBy
45          elementsPage.test1_addElementsButton().click();
46  
47          // verify delayed element that was injected via @ElementBy waits
48          Poller.waitUntilTrue(elementsPage.test1_delayedSpan().timed().isPresent());
49      }
50  
51  
52      @Test
53      public void testIsPresent()
54      {
55          product.visit(ElementsPage.class);
56  
57          // Positive - verify element that exists
58          assertTrue(product.find(By.id("test1_addElementsButton")).isPresent());
59  
60          // Negative - verify element that does not exist
61          assertFalse(product.find(By.id("test1_non_existant")).isPresent());
62  
63          // Delayed presence - click on button that adds a span with delay, verify isPresent does not wait.
64          product.find(By.id("test1_addElementsButton")).click();
65          assertFalse(product.find(By.id("test1_delayedSpan")).isPresent());
66      }
67  
68      @Test
69      public void testIsPresentWithParent()
70      {
71          product.visit(ElementsPage.class);
72  
73          final PageElement parentContainer = product.find(By.id("parent-container"));
74          assertTrue(parentContainer.isPresent());
75          assertTrue(parentContainer.find(By.className("child-button")).isPresent());
76      }
77  
78  
79      @Test
80      public void testIsPresentWhenParentIsNotPresent()
81      {
82          product.visit(ElementsPage.class);
83  
84          final PageElement notExistingParent = product.find(By.id("not-existing-parent"));
85          assertFalse(notExistingParent.isPresent());
86          assertFalse(notExistingParent.find(By.className("child-button")).isPresent());
87      }
88  
89      @Test
90      public void testIsVisible()
91      {
92          product.visit(ElementsPage.class);
93  
94          PageElement testInput = product.find(By.id("test2_input"));
95  
96          // Positive - verify input that is visible
97          assertTrue(testInput.isVisible());
98  
99          // Delayed presence - click on a button that adds an element with delay, verify isVisible waits
100         product.find(By.id("test2_addElementsButton")).click();
101         assertTrue(product.find(By.id("test2_delayedSpan")).isVisible());
102 
103         // Delayed positive - click on button to make input visible with delay and verify that it did not wait
104         product.find(By.id("test2_toggleInputVisibility")).click();
105         product.find(By.id("test2_toggleInputVisibilityWithDelay")).click();
106         assertFalse(testInput.isVisible());
107     }
108 
109 
110     @Test
111     public void testGetText()
112     {
113         product.visit(ElementsPage.class);
114 
115         // Positive - verify span with text
116         assertEquals("Span Value", product.find(By.id("test3_span")).getText());
117 
118         // Delayed presence - click on button that adds a span with delay, verify getText waits
119         product.find(By.id("test3_addElementsButton")).click();
120         assertEquals("Delayed Span", product.find(By.id("test3_delayedSpan")).getText());
121 
122         // Delayed positive - click on button that sets the text of span with delay, verify getText does not wait
123         product.find(By.id("test3_setTextButton")).click();
124         assertEquals("", product.find(By.id("test3_spanEmpty")).getText());
125     }
126 
127     @Test
128     public void testFind()
129     {
130         product.visit(ElementsPage.class);
131 
132         // find a delayed elements within another
133         PageElement childList = product.find(By.id("test4_parentList")).find(By.tagName("ul"));
134         assertEquals("test4_childList", childList.getAttribute("id"));
135 
136         PageElement leafList = childList.find(By.tagName("ul"));
137         assertEquals("test4_leafList", leafList.getAttribute("id"));
138 
139         PageElement listItem = leafList.find(By.tagName("li"));
140         assertEquals("Item 1", listItem.getText());
141 
142         //wait for presence on an element within another
143         product.find(By.id("test4_addElementsButton")).click();
144         waitUntilTrue(leafList.find(By.linkText("Item 4")).timed().isPresent());
145 
146         //wait for text on an element within another
147         driver.get(rootUrl + "/html/elements.html");
148 
149         product.find(By.id("test4_addElementsButton")).click();
150         assertEquals("Item 5", product.find(By.className("listitem-active")).getText());
151     }
152 
153     @Test
154     public void testGetTagName()
155     {
156         ElementsPage elementsPage = product.visit(ElementsPage.class);
157 
158         assertEquals("input", elementsPage.test1_addElementsButton().getTagName());
159     }
160 
161     @Test
162     public void testHasAttribute()
163     {
164         ElementsPage elementsPage = product.visit(ElementsPage.class);
165 
166         // positive
167         assertTrue(elementsPage.test1_addElementsButton().hasAttribute("type", "button"));
168 
169         // incorrect attribute
170         assertFalse(elementsPage.test1_addElementsButton().hasAttribute("type", "bar"));
171 
172         // attribute not present
173         assertFalse(elementsPage.test1_addElementsButton().hasAttribute("nonexistant", "foo"));
174     }
175 
176     @Test
177     public void shouldFindElementByJquery()
178     {
179         product.visit(ElementsPage.class);
180         final PageElement awesomeDiv = product.find(By.id("awesome-div"));
181         final PageElement awesomeSpan = awesomeDiv.find(ByJquery.$("span:contains(Awesome)"));
182         assertNotNull(awesomeSpan);
183         assertEquals("awesome-span", awesomeSpan.getAttribute("id"));
184 
185         final SelectElement awesomeSelect = elementFinder.find(By.id("awesome-select"), SelectElement.class);
186         awesomeSelect.find(ByJquery.$("option:contains(Volvo)")).select();
187         assertEquals(Options.value("volvo"), awesomeSelect.getSelected());
188     }
189 
190     @Test
191     public void shouldRebindElementIfStale_whenLocatedByAnnotation()
192     {
193         DynamicPage page = product.visit(DynamicPage.class);
194 
195         assertEquals("Hello Tester!", page.createFieldSet().helloWorld("Tester").getMessage());
196         assertEquals("Hello Developer!", page.createFieldSet().helloWorld("Developer").getMessage());
197 
198     }
199 
200     @Test
201     public void shouldRebindElementIfStale_whenOriginalElementBecomesStaleAfterSomeTime()
202     {
203         // This strategy can be used by pabeobjects that reload the same page after an action
204 
205         DynamicPage page = product.visit(DynamicPage.class);
206         page.createFieldSet();
207 
208         PageElementFinder elementFinder = page.getElementFinder();
209         PageElement button = elementFinder.find(By.id("helloWorldButton"), TimeoutType.AJAX_ACTION);
210 
211         product.getTester().getDriver().executeScript("$('#helloWorldButton').addClass('posting')");
212         page.removeAndCreateFieldSetSlowly();
213         waitUntilFalse(button.timed().hasClass("posting"));
214     }
215 
216 
217     @Test
218     public void shouldRebindElementIfStale_whenLocatedByElementFinder()
219     {
220         DynamicPage page = product.visit(DynamicPage.class);
221 
222         PageElementFinder elementFinder = page.getElementFinder();
223         PageElement username = elementFinder.find(By.id("nameTextBox"));
224         PageElement button = elementFinder.find(By.id("helloWorldButton"));
225         PageElement message = elementFinder.find(By.id("messageSpan"));
226 
227         page.createFieldSet();
228 
229         username.type("Tester");
230         button.click();
231         assertEquals("Hello Tester!", message.getText());
232 
233         // recreate the fields
234         page.createFieldSet();
235         username.type("Developer");
236         button.click();
237         assertEquals("Hello Developer!", message.getText());
238     }
239 
240     @Test
241     public void shouldRebindElementsIfStale_whenLocatedByParentFindSingle()
242     {
243         DynamicPage page = product.visit(DynamicPage.class);
244         PageElementFinder elementFinder = page.getElementFinder();
245 
246         PageElement div = elementFinder.find(By.id("placeHolderDiv"));
247         PageElement username = div.find(By.tagName("fieldset")).find(By.id("nameTextBox"));
248         PageElement button = div.find(By.tagName("fieldset")).find(By.id("helloWorldButton"));
249         PageElement message = div.find(By.tagName("fieldset")).find(By.id("messageSpan"));
250 
251         page.createFieldSet();
252         username.type("Tester");
253         button.click();
254         assertEquals("Hello Tester!", message.getText());
255 
256         //recreate the fields
257         page.createFieldSet();
258         username.type("Developer");
259         button.click();
260         assertEquals("Hello Developer!", message.getText());
261     }
262 
263     @Test
264     public void elementShouldBeConvertedToWebElement()
265     {
266         DynamicPage page = product.visit(DynamicPage.class);
267         PageElementFinder elementFinder = page.getElementFinder();
268 
269         PageElement div = elementFinder.find(By.id("placeHolderDiv"));
270         WebDriverElement username = (WebDriverElement) div.find(By.tagName("fieldset")).find(By.id("nameTextBox"));
271         PageElement button = div.find(By.tagName("fieldset")).find(By.id("helloWorldButton"));
272         PageElement message = div.find(By.tagName("fieldset")).find(By.id("messageSpan"));
273 
274         page.createFieldSet();
275         username.asWebElement().sendKeys("Tester");
276         button.click();
277         assertEquals("Hello Tester!", message.getText());
278 
279         //recreate the fields
280         page.createFieldSet();
281         username.asWebElement().sendKeys("Developer");
282         button.click();
283         assertEquals("Hello Developer!", message.getText());
284     }
285 
286     @Test
287     public void shouldRebindElementsIfStale_whenLocatingByParentFindAll()
288     {
289         DynamicPage page = product.visit(DynamicPage.class);
290         PageElementFinder elementFinder = page.getElementFinder();
291 
292         page.createFieldSet();
293 
294         PageElement fieldset = elementFinder.find(By.tagName("fieldset"));
295         PageElement username = fieldset.findAll(By.tagName("input")).get(0);
296         PageElement button = fieldset.findAll(By.tagName("input")).get(1);
297         PageElement message = fieldset.find(By.id("messageSpan"));
298 
299         username.type("Tester");
300         button.click();
301         assertEquals("Hello Tester!", message.getText());
302 
303         //recreate the fields
304         page.createFieldSet();
305         username.type("Developer");
306         button.click();
307         assertEquals("Hello Developer!", message.getText());
308     }
309 
310     @Test
311     public void shouldRebindElementsIfStale_whenLocatingByParentFindAllGivenParentAlwaysExists()
312     {
313         DynamicPage page = product.visit(DynamicPage.class);
314         PageElementFinder elementFinder = page.getElementFinder();
315 
316         page.createFieldSet();
317 
318         PageElement mainDiv = elementFinder.find(By.id("placeHolderDiv"));
319         // needs timeout more than 3 seconds
320         PageElement username = mainDiv.findAll(By.tagName("input")).get(0).withTimeout(TimeoutType.AJAX_ACTION);
321         PageElement button = mainDiv.findAll(By.tagName("input")).get(1).withTimeout(TimeoutType.AJAX_ACTION);
322         PageElement message = mainDiv.find(By.id("messageSpan")).withTimeout(TimeoutType.AJAX_ACTION);
323 
324         username.type("Tester");
325         button.click();
326         assertEquals("Hello Tester!", message.getText());
327 
328         //recreate the fields
329         page.createFieldSetSlowly();
330         username.type("Developer");
331         button.click();
332         waitUntilEquals("Hello Developer!", message.timed().getText());
333     }
334 
335     @Test
336     public void testExecuteScriptOnAnElement()
337     {
338         product.visit(ElementsPage.class);
339         PageElement button = elementFinder.find(By.id(("test11_button")));
340 
341         assertFalse(button.hasClass("test"));
342 
343         button.javascript().execute("$(arguments[0]).addClass('test')");
344         Poller.waitUntilTrue(button.timed().hasClass("test"));
345 
346         assertEquals(Boolean.TRUE, button.javascript().execute("return $(arguments[0]).hasClass('test')"));
347     }
348 }