@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()); }
/** 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 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 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"); }
/** 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"); }