1 package com.atlassian.webdriver.it.tests;
2
3 import com.atlassian.pageobjects.browser.Browser;
4 import com.atlassian.webdriver.it.AbstractFileBasedServerTest;
5 import com.atlassian.webdriver.it.pageobjects.page.ByDataAttributePage;
6 import com.atlassian.webdriver.testing.annotation.IgnoreBrowser;
7 import com.atlassian.webdriver.utils.by.ByDataAttribute;
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.openqa.selenium.WebDriver;
11 import org.openqa.selenium.WebElement;
12
13 import java.util.List;
14
15 import static com.atlassian.webdriver.utils.element.WebElementMatchers.containsAtLeast;
16 import static com.atlassian.webdriver.utils.element.WebElementMatchers.tagNameEqual;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertThat;
19
20
21
22
23
24
25 public class TestByDataAttribute extends AbstractFileBasedServerTest
26 {
27
28 ByDataAttributePage byDataAttributePage;
29 WebDriver driver;
30
31 @Before
32 public void init()
33 {
34 byDataAttributePage = product.visit(ByDataAttributePage.class);
35 driver = product.getTester().getDriver();
36 }
37
38
39 @Test
40 @IgnoreBrowser(value = {Browser.HTMLUNIT, Browser.HTMLUNIT_NOJS}, reason = "SELENIUM-166 HtmlUnit has partial support for data- attributes")
41 public void shouldFindAllByAttributeName()
42 {
43 final List<WebElement> elements = driver.findElements(ByDataAttribute.byData("type"));
44 assertEquals(6, elements.size());
45 assertThat(elements, containsAtLeast(tagNameEqual("li"), 4));
46 assertThat(elements, containsAtLeast(tagNameEqual("ul"), 1));
47 assertThat(elements, containsAtLeast(tagNameEqual("div"), 1));
48
49 final List<WebElement> listElements = driver.findElements(ByDataAttribute.byData("ordering"));
50 assertEquals(4, listElements.size());
51 assertThat(listElements, containsAtLeast(tagNameEqual("li"), 4));
52 }
53
54 @Test
55 public void shouldFindByAttributeNameAndValue()
56 {
57 final WebElement firstListItem = driver.findElement(ByDataAttribute.byData("ordering", "1"));
58 assertIsListElementWithText(firstListItem, "a");
59 }
60
61 @Test
62 @IgnoreBrowser(value = {Browser.HTMLUNIT, Browser.HTMLUNIT_NOJS}, reason = "SELENIUM-166 HtmlUnit has partial support for data- attributes")
63 public void shouldFindByTagNameAndData()
64 {
65 final List<WebElement> listElements = driver.findElements(ByDataAttribute.byTagAndData("li", "type"));
66 assertEquals(4, listElements.size());
67 assertThat(listElements, containsAtLeast(tagNameEqual("li"), 4));
68
69 final WebElement mainDiv = driver.findElement(ByDataAttribute.byTagAndData("div", "type"));
70 assertEquals("div", mainDiv.getTagName());
71 assertEquals("main container", mainDiv.getAttribute("data-type"));
72 }
73
74 @Test
75 public void shouldFindByTagNameAndDataAndValue()
76 {
77 final WebElement lastListItem = driver.findElement(ByDataAttribute.byTagAndData("li", "ordering", "4"));
78 assertIsListElementWithText(lastListItem, "d");
79 }
80
81 @Test
82 @IgnoreBrowser(value = {Browser.HTMLUNIT, Browser.HTMLUNIT_NOJS}, reason = "SELENIUM-166 HtmlUnit has partial support for data- attributes")
83 public void shouldFindByWeirdDataAttributeValues()
84 {
85 final WebElement weirdOne = driver.findElement(ByDataAttribute.byTagAndData("li", "a-attribute"));
86 assertIsListElementWithText(weirdOne, "a");
87
88 final WebElement weirdOneAgain = driver.findElement(ByDataAttribute.byTagAndData("li", "a-attribute", "==A is awesome=="));
89 assertIsListElementWithText(weirdOneAgain, "a");
90
91 final WebElement weirdTwo = driver.findElement(ByDataAttribute.byTagAndData("li", "b-attribute"));
92 assertIsListElementWithText(weirdTwo, "b");
93
94 final WebElement weirdTwoAgain = driver.findElement(ByDataAttribute.byTagAndData("li", "b-attribute", "==B is cool=="));
95 assertIsListElementWithText(weirdTwoAgain, "b");
96 }
97
98 private void assertIsListElementWithText(WebElement firstListItem, String text)
99 {
100 assertEquals(firstListItem.getTagName(), "li");
101 assertEquals(firstListItem.getText(), text);
102 }
103 }