/** * {@inheritDoc} * This implementation will show the label attribute before the * content of the tag if the attribute exists. */ // we need to preserve this method as it is there since many versions with the above documentation. @Override public String asText() { return super.asText(); }
/** * {@inheritDoc} * This implementation will show the label attribute before the * content of the tag if the attribute exists. */ // we need to preserve this method as it is there since many versions with the above documentation. @Override public String asText() { return super.asText(); }
/** * Return true if a given string is contained within the specified element. * * @param element org.w3c.com.Element to inspect. * @param text text to check for. */ private boolean isTextInElement(HtmlElement element, String text) { return element.asText().indexOf(text) >= 0; }
HtmlElement e = page.getFirstByXPath("//li[@data-role='list-divider']"); System.out.println(e.asText());
/** * {@inheritDoc} */ @Override public String getInnerText() { return getDomNodeOrDie().asText(); }
@Override public String getElementTextByXPath(String xpath) { HtmlElement e = getHtmlElementByXPath(xpath); if (e == null) { return null; } return e.asText(); }
/** * {@inheritDoc} */ @Override public String getInnerText() { return getDomNodeOrDie().asText(); }
/** * Returns the table caption text or an empty string if a caption wasn't specified. * * @return the caption text */ public String getCaptionText() { for (final HtmlElement element : getChildElements()) { if (element instanceof HtmlCaption) { return element.asText(); } } return null; }
/** * Returns the table caption text or an empty string if a caption wasn't specified. * * @return the caption text */ public String getCaptionText() { for (final HtmlElement element : getChildElements()) { if (element instanceof HtmlCaption) { return element.asText(); } } return null; }
/** * Returns the table caption text or an empty string if a caption wasn't specified. * * @return the caption text */ public String getCaptionText() { for (final HtmlElement element : getChildElements()) { if (element instanceof HtmlCaption) { return element.asText(); } } return null; }
SimpleDateFormatter dateParser = ... List<DateAndTitle> result = new LinkedList(); Date lastDate = null; // for(HtmlElement node : table.getHtmlElementDescendants()) { for(HtmlElement node : findAllChildren(table)) { if( "ul".equals( node.getTagName() ) ) { String title = node.asText(); result.add(new DateAndTitle(lastDate, title); } if( "h3".equals( node.getTagName() ) ) { String dateString = node.asText(); lastDate = dateParser.parse(dateString); } }
/** * Verifies that the element on the specified page which matches the specified ID contains the * specified text. * * @param page the page to check * @param text the text to check for * @param id the ID of the element which is expected to contain the specified text */ public static void assertTextPresentInElement(final HtmlPage page, final String text, final String id) { try { final HtmlElement element = page.getHtmlElementById(id); if (element.asText().indexOf(text) == -1) { final String msg = "The element with ID '" + id + "' does not contain the text '" + text + "'."; throw new AssertionError(msg); } } catch (final ElementNotFoundException e) { final String msg = "Unable to verify that the element with ID '" + id + "' contains the text '" + text + "' because the specified element does not exist."; throw new AssertionError(msg); } }
/** * Verifies that the element on the specified page which matches the specified ID contains the * specified text. * * @param page the page to check * @param text the text to check for * @param id the ID of the element which is expected to contain the specified text */ public static void assertTextPresentInElement(final HtmlPage page, final String text, final String id) { try { final HtmlElement element = page.getHtmlElementById(id); if (element.asText().indexOf(text) == -1) { final String msg = "The element with ID '" + id + "' does not contain the text '" + text + "'."; throw new AssertionError(msg); } } catch (final ElementNotFoundException e) { final String msg = "Unable to verify that the element with ID '" + id + "' contains the text '" + text + "' because the specified element does not exist."; throw new AssertionError(msg); } }
/** * Verifies that the element on the specified page which matches the specified ID does not * contain the specified text. * * @param page the page to check * @param text the text to check for * @param id the ID of the element which is expected to not contain the specified text */ public static void assertTextNotPresentInElement(final HtmlPage page, final String text, final String id) { try { final HtmlElement element = page.getHtmlElementById(id); if (element.asText().contains(text)) { final String msg = "The element with ID '" + id + "' contains the text '" + text + "'."; throw new AssertionError(msg); } } catch (final ElementNotFoundException e) { final String msg = "Unable to verify that the element with ID '" + id + "' does not contain the text '" + text + "' because the specified element does not exist."; throw new AssertionError(msg); } }
/** * Verifies that the element on the specified page which matches the specified ID does not * contain the specified text. * * @param page the page to check * @param text the text to check for * @param id the ID of the element which is expected to not contain the specified text */ public static void assertTextNotPresentInElement(final HtmlPage page, final String text, final String id) { try { final HtmlElement element = page.getHtmlElementById(id); if (element.asText().contains(text)) { final String msg = "The element with ID '" + id + "' contains the text '" + text + "'."; throw new AssertionError(msg); } } catch (final ElementNotFoundException e) { final String msg = "Unable to verify that the element with ID '" + id + "' does not contain the text '" + text + "' because the specified element does not exist."; throw new AssertionError(msg); } }
/** * Verifies that the element on the specified page which matches the specified ID contains the * specified text. * * @param page the page to check * @param text the text to check for * @param id the ID of the element which is expected to contain the specified text */ public static void assertTextPresentInElement(final HtmlPage page, final String text, final String id) { try { final HtmlElement element = page.getHtmlElementById(id); if (element.asText().indexOf(text) == -1) { final String msg = "The element with ID '" + id + "' does not contain the text '" + text + "'."; throw new AssertionError(msg); } } catch (final ElementNotFoundException e) { final String msg = "Unable to verify that the element with ID '" + id + "' contains the text '" + text + "' because the specified element does not exist."; throw new AssertionError(msg); } }
/** * Return true if a given regexp is contained within the specified element. * * @param element org.w3c.com.Element to inspect. * @param regexp regexp to match. */ private boolean isMatchInElement(HtmlElement element, String regexp) { RE re = getRE(regexp); return re.match(element.asText()); }
public class HtmlUnitTest { public static void main(String[] args) throws FailingHttpStatusCodeException, MalformedURLException, IOException { final WebClient webClient = new WebClient(); final HtmlPage page = webClient.getPage("http://www.google.com"); System.out.println(page.getTitleText()); for (HtmlElement node : page.getHtmlElementDescendants()) { if (node.getTagName().toUpperCase().equals("IMG")) { System.out.println("NAME: " + node.getTagName()); System.out.println("WIDTH:" + node.getAttribute("width")); System.out.println("HEIGHT:" + node.getAttribute("height")); System.out.println("TEXT: " + node.asText()); System.out.println("XMl: " + node.asXml()); } } } }
void assertPaneDiffText(String msg, int expectedValue, Object paneObj) { assertTrue( "paneObj should be an HtmlElement, it was " + paneObj.getClass(), paneObj instanceof HtmlElement ); String paneText = ((HtmlElement) paneObj).asText(); if (expectedValue==0) { assertStringEmptyOrNull(msg, paneText); } else { String expectedString = (expectedValue >= 1 ? "+" : "-") + Math.abs(expectedValue); assertEquals(msg, expectedString, paneText); } }
@Override public String getPageText() { Page page = win.getEnclosedPage(); if (page instanceof HtmlPage) { return ((HtmlPage) page).getBody().asText(); } if (page instanceof TextPage) { return ((TextPage) page).getContent(); } if (page instanceof JavaScriptPage) { return ((JavaScriptPage) page).getContent(); } if (page instanceof XmlPage) { return ((XmlPage) page).getContent(); } if (page instanceof UnexpectedPage) { return ((UnexpectedPage) page).getWebResponse() .getContentAsString(); } throw new RuntimeException( "Unexpected error in getPageText(). This method need to be updated."); }