@Test public void testWithXpath() { IElement element = getElementByXPath("//body"); assertNotNull(element); assertEquals("body", element.getName()); // get first input children IElement input = element.getElement("input"); assertNotNull(input); assertEquals("input", input.getName()); assertEquals("element_name", input.getAttribute("name")); assertEquals("test3", input.getAttribute("value")); // get all input children List<IElement> inputs = element.getElements("input"); assertEquals(4, inputs.size()); // there should be two assertEquals("test3", inputs.get(0).getAttribute("value")); assertEquals("Do nothing", inputs.get(1).getAttribute("value")); assertEquals("initial", inputs.get(2).getAttribute("value")); assertEquals("unchanged", inputs.get(3).getAttribute("value")); // test regexps assertMatch("init.+", inputs.get(2).getAttribute("value")); assertNotMatch("^xinitial", inputs.get(2).getAttribute("value")); assertMatch("test regexp with message", "init.+", inputs.get(2).getAttribute("value")); assertNotMatch("test regexp with message", "$xinitial", inputs.get(2).getAttribute("value")); // get parent through xpath IElement parent = element.getElement(".."); assertNotNull(parent); assertEquals("html", parent.getName()); }
@Test public void testSimple() { // test an element that exists IElement element = getElementByXPath("//input[@id='test']"); assertNotNull(element); assertEquals(element.getName(), "input"); assertEquals(element.getAttribute("name"), "element_name"); assertEquals(element.getAttribute("id"), "test"); assertEquals(element.getAttribute("value"), "test3"); }
/** Test that {@link IElement#equals(Object)} is implemented correctly. */ @Test public void testIElementEquals() { // through getElementById IElement container1 = getElementById("container"); // through IElement.getElement IElement span = getElementByXPath("//span[@class='inline']"); IElement container2 = span.getElement(".."); // through getByXPath IElement container3 = null; for (IElement e : getElementsByXPath("//div")) { if ("container".equals(e.getAttribute("id"))) { container3 = e; } } // should have found all of these assertNotNull(container1); assertNotNull(container2); assertNotNull(container3); // check equality assertEquals(container1, container2); assertEquals(container2, container3); assertEquals(container1, container3); }
/** * Test preceding element XPath. preceding: "Selects everything in the document that is before the * start tag of the current node" */ @Test public void testPreceding() { IElement element = getElementById("first"); // li // should get the first <input>, which is // <input id="test" name="element_name" value="test3"> IElement preceding = element.getElement("preceding::input"); assertEquals(preceding.getName(), "input"); assertEquals(preceding.getAttribute("name"), "element_name"); }
/** change the element and make sure XPath has changed */ @Test public void testChanging() { { IElement element = getElementByXPath("//input[@id='test']"); assertNotNull(element); assertEquals(element.getName(), "input"); assertEquals(element.getAttribute("name"), "element_name"); assertEquals(element.getAttribute("id"), "test"); assertEquals(element.getAttribute("value"), "test3"); // the element should also be available through the normal method assertFormElementPresent("element_name"); } String testingText = new Date().toString(); setTextField("element_name", testingText); assertTextFieldEquals("element_name", testingText); // should still work { IElement element = getElementByXPath("//input[@id='test']"); assertNotNull(element); assertEquals(element.getName(), "input"); assertEquals(element.getAttribute("name"), "element_name"); assertEquals(element.getAttribute("id"), "test"); assertEquals(element.getAttribute("value"), testingText); // should have changed // the element should also be available through the normal method assertFormElementPresent("element_name"); } }
/** * Test that setting attributes manually (e.g setAttribute("value") properly calls any attached * Javascript. */ @Test public void testAttributeJavascript() { String testingText = new Date().toString(); { IElement js1 = getElementById("js1"); IElement js2 = getElementById("js2"); assertEquals("initial", js1.getAttribute("value")); assertEquals("unchanged", js2.getAttribute("value")); // change js1's value js1.setAttribute("value", testingText); } // refresh the elements and check they have changed { IElement js1 = getElementById("js1"); IElement js2 = getElementById("js2"); assertEquals(testingText, js1.getAttribute("value")); assertEquals(testingText, js2.getAttribute("value")); } }
/** Test parent, child methods */ @Test public void testChildrenAndParent() { assertElementPresent("first"); IElement element = getElementById("first"); assertEquals(element.getName(), "li"); assertEquals(element.getTextContent(), "one"); assertEquals(element.getAttribute("id"), "first"); // parent should be an <ol> IElement parent = element.getParent(); assertEquals(parent.getName(), "ol"); // it should have four children List<IElement> children = parent.getChildren(); assertEquals(children.size(), 4); assertEquals(children.get(0).getTextContent(), "one"); assertEquals(children.get(1).getTextContent(), "two"); assertEquals(children.get(2).getTextContent(), "three"); assertEquals(children.get(3).getTextContent(), "four"); // they are all <li>'s for (IElement e : children) assertEquals(e.getName(), "li"); }