/** * Verifies that all element IDs in the specified page are unique. * * @param page the page to check */ public static void assertAllIdAttributesUnique(final HtmlPage page) { final List<String> list = new ArrayList<>(); for (final HtmlElement element : page.getHtmlElementDescendants()) { final String id = element.getId(); if (id != null && !id.isEmpty()) { if (list.contains(id)) { throw new AssertionError("The element ID '" + id + "' is not unique."); } list.add(id); } } }
/** * Many HTML components can have an <tt>accesskey</tt> attribute which defines a hot key for * keyboard navigation. This method verifies that all the <tt>accesskey</tt> attributes on the * specified page are unique. * * @param page the page to check */ public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) { final List<String> list = new ArrayList<>(); for (final HtmlElement element : page.getHtmlElementDescendants()) { final String key = element.getAttributeDirect("accesskey"); if (key != null && !key.isEmpty()) { if (list.contains(key)) { throw new AssertionError("The access key '" + key + "' is not unique."); } list.add(key); } } }
/** * Verifies that all element IDs in the specified page are unique. * * @param page the page to check */ public static void assertAllIdAttributesUnique(final HtmlPage page) { final List<String> list = new ArrayList<>(); for (final HtmlElement element : page.getHtmlElementDescendants()) { final String id = element.getId(); if (id != null && !id.isEmpty()) { if (list.contains(id)) { throw new AssertionError("The element ID '" + id + "' is not unique."); } list.add(id); } } }
/** * Many HTML components can have an <tt>accesskey</tt> attribute which defines a hot key for * keyboard navigation. This method verifies that all the <tt>accesskey</tt> attributes on the * specified page are unique. * * @param page the page to check */ public static void assertAllAccessKeyAttributesUnique(final HtmlPage page) { final List<String> list = new ArrayList<>(); for (final HtmlElement element : page.getHtmlElementDescendants()) { final String key = element.getAttributeDirect("accesskey"); if (key != null && !key.isEmpty()) { if (list.contains(key)) { throw new AssertionError("The access key '" + key + "' is not unique."); } list.add(key); } } }
/** * Get an element with a certain attribute value. * * @param attributeName attribute name to search for * @param value value to search for * @return the element found, or null */ private HtmlElement getHtmlElementWithAttribute(String attributeName, String value) { for (HtmlElement e : getCurrentPage().getHtmlElementDescendants()) { if (e.getAttribute(attributeName).equals(value)) { return e; } } return null; }
/** * Returns all the HTML elements that are assigned to the specified access key. An * access key (aka mnemonic key) is used for keyboard navigation of the * page.<p> * * The HTML specification seems to indicate that one accesskey cannot be used * for multiple elements however Internet Explorer does seem to support this. * It's worth noting that Firefox does not support multiple elements with one * access key so you are making your HTML browser specific if you rely on this * feature.<p> * * Only the following HTML elements may have <tt>accesskey</tt>s defined: A, AREA, * BUTTON, INPUT, LABEL, LEGEND, and TEXTAREA. * * @param accessKey the key to look for * @return the elements that are assigned to the specified accesskey */ public List<HtmlElement> getHtmlElementsByAccessKey(final char accessKey) { final List<HtmlElement> elements = new ArrayList<>(); final String searchString = Character.toString(accessKey).toLowerCase(Locale.ROOT); for (final HtmlElement element : getHtmlElementDescendants()) { if (ACCEPTABLE_TAG_NAMES.contains(element.getTagName())) { final String accessKeyAttribute = element.getAttributeDirect("accesskey"); if (searchString.equalsIgnoreCase(accessKeyAttribute)) { elements.add(element); } } } return elements; }
/** * Returns all the HTML elements that are assigned to the specified access key. An * access key (aka mnemonic key) is used for keyboard navigation of the * page.<p> * * The HTML specification seems to indicate that one accesskey cannot be used * for multiple elements however Internet Explorer does seem to support this. * It's worth noting that Firefox does not support multiple elements with one * access key so you are making your HTML browser specific if you rely on this * feature.<p> * * Only the following HTML elements may have <tt>accesskey</tt>s defined: A, AREA, * BUTTON, INPUT, LABEL, LEGEND, and TEXTAREA. * * @param accessKey the key to look for * @return the elements that are assigned to the specified accesskey */ public List<HtmlElement> getHtmlElementsByAccessKey(final char accessKey) { final List<HtmlElement> elements = new ArrayList<>(); final String searchString = Character.toString(accessKey).toLowerCase(Locale.ROOT); for (final HtmlElement element : getHtmlElementDescendants()) { if (ACCEPTABLE_TAG_NAMES.contains(element.getTagName())) { final String accessKeyAttribute = element.getAttributeDirect("accesskey"); if (searchString.equalsIgnoreCase(accessKeyAttribute)) { elements.add(element); } } } return elements; }
for (final HtmlElement element : getHtmlElementDescendants()) { final String tagName = element.getTagName(); if (TABBABLE_TAGS.contains(tagName)) {
for (final HtmlElement element : getHtmlElementDescendants()) { final String tagName = element.getTagName(); if (TABBABLE_TAGS.contains(tagName)) {
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()); } } } }
@Override public boolean hasFormParameterNamed(String paramName) { for (HtmlElement e : getCurrentPage().getHtmlElementDescendants()) { if (e.getAttribute("name").equals(paramName)) { // set the working form if none has been set if (e.getEnclosingForm() != null && getWorkingForm() == null) { setWorkingForm(e.getEnclosingForm()); } return true; } } return false; }