public static HtmlPage trainStrength(HtmlPage page, int amount) throws IOException { System.out.println("Training " + amount + " strength..."); for (DomNode n : page.getElementById("divStrength").getDescendants()) { // System.out.println(n.toString()); HtmlTextInput textInput; HtmlSubmitInput submitInput; if (n.toString() .contains("HtmlTextInput[<input type=\"text\" value=\"1\" id=\"t\" name=\"t\">]")) { textInput = (HtmlTextInput) n; textInput.setValueAttribute(Integer.toString(amount)); System.out.println(textInput.toString()); } else if (n.toString() .contains("HtmlSubmitInput[<input type=\"submit\" value=\"Train\">]")) { submitInput = (HtmlSubmitInput) n; page = submitInput.click(); System.out.println(submitInput.toString()); } } /* System.out.println(this.user.getUsername()+"-"+"Training "+amount+" strength..."); HtmlTextInput amountInput = (HtmlTextInput) page.getByXPath("/html/body/div[4]/table/tbody/tr/td[2]/center/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[2]/form/table/tbody/tr/td[1]/input[1]").get(0); amountInput.setValueAttribute(Integer.toString(amount)); HtmlElement e = (HtmlElement) page.getByXPath("/html/body/div[4]/table/tbody/tr/td[2]/center/div[2]/div/div[2]/div[3]/div[1]/div[1]/div[2]/form/table/tbody/tr/td[3]/input").get(0); e.click(); */ System.out.println(page.asText()); return page; }
/** * Tests XPath where results are attributes. * * @throws Exception if test fails */ @Test @SuppressWarnings("unchecked") public void listAttributesResult() throws Exception { final String content = "<html><body>\n" + "<img src='1.png'>\n" + "<img src='2.png'>\n" + "<img src='3.png'>\n" + "</body></html>"; final HtmlPage page = loadPage(content); final List<?> nameList = page.getByXPath("//img/@src"); final List<?> valueList = new ArrayList<Object>(nameList); final String[] expectedNames = {"src", "src", "src"}; final List<String> collectedNames = new ArrayList<String>(); for (final DomNode node : (List<DomNode>) nameList) { collectedNames.add(node.getNodeName()); } assertEquals(expectedNames, collectedNames); final String[] expectedValues = {"1.png", "2.png", "3.png"}; final List<String> collectedValues = new ArrayList<String>(); for (final DomNode node : (List<DomNode>) valueList) { collectedValues.add(node.getNodeValue()); } assertEquals(expectedValues, collectedValues); }
/** * Test that the elements are in the right order. * * @throws Exception if test fails */ @Test @SuppressWarnings("unchecked") public void elementOrder() throws Exception { final String content = "<html><head><title>First</title><script>\n" + "</script></head><body>\n" + "</body></html>"; final HtmlPage page = loadPage(content); final List<?> list = page.getByXPath("//*"); final String[] expected = {"html", "head", "title", "script", "body"}; final List<String> actualNames = new ArrayList<String>(); for (final DomNode node : (List<DomNode>) list) { actualNames.add(node.getNodeName()); } assertEquals(expected, actualNames); }
/** {@inheritDoc} */ @Override public void setDomNode(final DomNode domNode) { super.setDomNode(domNode); if (domNode.getPage().getWebClient().getOptions().isAppletEnabled()) { try { createAppletMethodAndProperties(); } catch (final Exception e) { throw new RuntimeException(e); } } }
/** * Imports a node from another document to this document. The source node is not altered or * removed from the original document; this method creates a new copy of the source node. * * @param importedNode the node to import * @param deep Whether to recursively import the subtree under the specified node; or not * @return the imported node that belongs to this Document */ @JsxFunction({@WebBrowser(FF), @WebBrowser(value = IE, minVersion = 11), @WebBrowser(CHROME)}) public Object importNode(final Node importedNode, final boolean deep) { DomNode domNode = importedNode.getDomNodeOrDie(); domNode = domNode.cloneNode(deep); domNode.processImportNode(this); for (final DomNode childNode : domNode.getDescendants()) { childNode.processImportNode(this); } return domNode.getScriptObject(); }
private void toText(final DomNode node, final StringBuilder buffer) { switch (node.getNodeType()) { case Node.DOCUMENT_TYPE_NODE: case Node.NOTATION_NODE: return; case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: case Node.COMMENT_NODE: case Node.PROCESSING_INSTRUCTION_NODE: buffer.append(node.getNodeValue()); break; default: } for (final DomNode child : node.getChildren()) { switch (child.getNodeType()) { case ELEMENT_NODE: toText(child, buffer); break; case TEXT_NODE: case CDATA_SECTION_NODE: buffer.append(child.getNodeValue()); break; default: } } }
/** * Create a new DOM text node with the given data. * * @param newData the string value for the text node * @return the new text node or NOT_FOUND if there is an error */ @JsxFunction public Object createTextNode(final String newData) { Object result = NOT_FOUND; try { final DomNode domNode = new DomText(getDomNodeOrDie().getPage(), newData); final Object jsElement = getScriptableFor(domNode); if (jsElement == NOT_FOUND) { if (LOG.isDebugEnabled()) { LOG.debug( "createTextNode(" + newData + ") cannot return a result as there isn't a JavaScript object for the DOM node " + domNode.getClass().getName()); } } else { result = jsElement; } } catch (final ElementNotFoundException e) { // Just fall through - result is already set to NOT_FOUND } return result; }
/** * Creates a new event instance. * * @param domNode the DOM node that triggered the event * @param type the event type */ public Event(final DomNode domNode, final String type) { this((SimpleScriptable) domNode.getScriptObject(), type); setDomNode(domNode, false); }
@Test public void testAll() throws Exception { HtmlPage page = (HtmlPage) this.client.getContentPage(); try { // Welcome page assertTrue(page.asText().contains("Welcome to Forge")); // Create an Owner page = page.getAnchorByText("Owner").click(); assertTrue(page.asText().contains("Search Owner entities")); page = page.getAnchorByText("Create New").click(); HtmlForm form = page.getFormByName("create"); assertTrue(page.asText().contains("Create a new Owner")); form.getInputByName("create:ownerBeanOwnerFirstName").setValueAttribute("Owner Firstname #1"); form.getInputByName("create:ownerBeanOwnerLastName").setValueAttribute("Owner Lastname #1"); page = page.getAnchorByText("Save").click(); // Edit the Owner assertTrue(page.asText().contains("Search Owner entities")); HtmlTable table = (HtmlTable) page.getHtmlElementById("search:ownerBeanPageItems"); assertEquals("Owner Firstname #1", table.getCellAt(1, 0).getTextContent()); assertEquals("Owner Lastname #1", table.getCellAt(1, 1).getTextContent()); page = page.getAnchorByText("Owner Firstname #1").click(); assertTrue(page.asText().contains("View existing Owner")); assertEquals( "Owner Firstname #1", page.getHtmlElementById("ownerBeanOwnerFirstName").getTextContent()); assertEquals( "Owner Lastname #1", page.getHtmlElementById("ownerBeanOwnerLastName").getTextContent()); assertEquals("", page.getHtmlElementById("ownerBeanOwnerAddress").getTextContent()); page = page.getAnchorByText("Edit").click(); form = page.getFormByName("create"); form.getInputByName("create:ownerBeanOwnerAddress").setValueAttribute("Owner Address #1"); page = page.getAnchorByText("Save").click(); assertTrue(page.asText().contains("View existing Owner")); assertEquals( "Owner Address #1", page.getHtmlElementById("ownerBeanOwnerAddress").getTextContent()); page = page.getAnchorByText("View All").click(); assertTrue(page.asText().contains("Search Owner entities")); // Create a Pet and associate it with the Owner page = page.getAnchorByText("Pet").click(); assertTrue(page.asText().contains("Search Pet entities")); page = page.getAnchorByText("Create New").click(); form = page.getFormByName("create"); form.getInputByName("create:petBeanPetName").setValueAttribute("Pet #1"); form.getInputByName("create:petBeanPetType").setValueAttribute("2"); form.getInputByName("create:petBeanPetSendReminders").setChecked(true); form.getSelectByName("create:petBeanPetOwner").setSelectedAttribute("1", true); page = page.getAnchorByText("Save").click(); // Click through from the Pet to the Owner table = (HtmlTable) page.getHtmlElementById("search:petBeanPageItems"); assertEquals("Pet #1", table.getCellAt(1, 0).getTextContent()); assertEquals("2", table.getCellAt(1, 1).getTextContent()); DomNode booleanNode = table.getCellAt(1, 2).getChildNodes().get(0).getChildNodes().get(0); assertEquals("span", booleanNode.getNodeName()); assertEquals("", booleanNode.getTextContent()); assertEquals( "boolean-true", booleanNode.getAttributes().getNamedItem("class").getNodeValue()); assertEquals( "Owner firstName: Owner Firstname #1, lastName: Owner Lastname #1, address: Owner Address #1", table.getCellAt(1, 3).getTextContent()); page = page.getAnchorByText("Pet #1").click(); assertTrue(page.asText().contains("View existing Pet")); page = page.getAnchorByText( "Owner firstName: Owner Firstname #1, lastName: Owner Lastname #1, address: Owner Address #1") .click(); assertTrue(page.asText().contains("View existing Owner")); // Create a new Owner page = page.getAnchorByText("Create New").click(); form = page.getFormByName("create"); form.getInputByName("create:ownerBeanOwnerFirstName").setValueAttribute("Owner Firstname #2"); form.getInputByName("create:ownerBeanOwnerLastName").setValueAttribute("Owner Lastname #2"); page = page.getAnchorByText("Save").click(); table = (HtmlTable) page.getHtmlElementById("search:ownerBeanPageItems"); assertEquals("Owner Firstname #1", table.getCellAt(1, 0).getTextContent()); assertEquals("Owner Lastname #1", table.getCellAt(1, 1).getTextContent()); assertEquals("Owner Firstname #2", table.getCellAt(2, 0).getTextContent()); assertEquals("Owner Lastname #2", table.getCellAt(2, 1).getTextContent()); // Search for a Pet by Owner page = page.getAnchorByText("Pet").click(); assertTrue(page.asText().contains("Search Pet entities")); table = (HtmlTable) page.getHtmlElementById("search:petBeanPageItems"); assertEquals("Pet #1", table.getCellAt(1, 0).getTextContent()); form = page.getFormByName("search"); form.getSelectByName("search:petBeanExampleOwner").setSelectedAttribute("3", true); page = page.getAnchorByText("Search").click(); table = (HtmlTable) page.getHtmlElementById("search:petBeanPageItems"); assertEquals("", table.getCellAt(1, 0).getTextContent()); form = page.getFormByName("search"); form.getSelectByName("search:petBeanExampleOwner").setSelectedAttribute("1", true); page = page.getAnchorByText("Search").click(); page = page.getAnchorByText("Pet #1").click(); // Delete a Pet assertTrue(page.asText().contains("View existing Pet")); page = page.getAnchorByText("Edit").click(); assertTrue(page.asText().contains("Edit existing Pet")); page = page.getAnchorByText("Cancel").click(); assertTrue(page.asText().contains("View existing Pet")); page = page.getAnchorByText("Edit").click(); page = page.getAnchorByText("Delete").click(); assertTrue(page.asText().contains("Search Pet entities")); table = (HtmlTable) page.getHtmlElementById("search:petBeanPageItems"); assertEquals("", table.getCellAt(1, 0).getTextContent()); } catch (Throwable t) { t.printStackTrace(); throw new RuntimeException(t); } finally { System.out.println(page.asXml()); } }