public String getTitle() { Page page = lastPage(); if (page == null || !(page instanceof HtmlPage)) { return null; // no page so there is no title } return ((HtmlPage) page).getTitleText(); }
public WebElement findElementByTagName(String name) { if (!(lastPage() instanceof HtmlPage)) throw new IllegalStateException("Cannot find element by name for " + lastPage()); NodeList allElements = ((HtmlPage) lastPage()).getElementsByTagName(name); if (allElements.getLength() > 0) { return newHtmlUnitWebElement((HtmlElement) allElements.item(0)); } throw new NoSuchElementException("Cannot find element with name: " + name); }
@Override public WebElement findElementByName(String name) { if (!(lastPage() instanceof HtmlPage)) { throw new IllegalStateException("Unable to locate element by name for " + lastPage()); } List<DomElement> allElements = ((HtmlPage) lastPage()).getElementsByName(name); if (!allElements.isEmpty()) { return newHtmlUnitWebElement(allElements.get(0)); } throw new NoSuchElementException("Unable to locate element with name: " + name); }
public WebElement findElementByName(String name) { if (!(lastPage() instanceof HtmlPage)) throw new IllegalStateException("Cannot find element by name for " + lastPage()); List<HtmlElement> allElements = ((HtmlPage) lastPage()).getElementsByName(name); if (allElements.size() > 0) { return newHtmlUnitWebElement(allElements.get(0)); } throw new NoSuchElementException("Cannot find element with name: " + name); }
@Override public WebElement findElementByTagName(String name) { if (!(lastPage() instanceof HtmlPage)) { throw new IllegalStateException("Unable to locate element by name for " + lastPage()); } NodeList allElements = ((HtmlPage) lastPage()).getElementsByTagName(name); if (allElements.getLength() > 0) { return newHtmlUnitWebElement((HtmlElement) allElements.item(0)); } throw new NoSuchElementException("Unable to locate element with name: " + name); }
private URL getRawUrl() { // TODO(simon): I can see this being baaad. Page page = lastPage(); if (page == null) { return null; } return page.getUrl(); }
@Override public WebElement findElementById(String id) { if (!(lastPage() instanceof HtmlPage)) { throw new NoSuchElementException("Unable to locate element by id for " + lastPage()); } try { HtmlElement element = ((HtmlPage) lastPage()).getHtmlElementById(id); return newHtmlUnitWebElement(element); } catch (ElementNotFoundException e) { throw new NoSuchElementException("Unable to locate element with ID: " + id); } }
public WebElement findElementByXPath(String selector) { if (!(lastPage() instanceof HtmlPage)) throw new IllegalStateException("Cannot find element by xpath for " + lastPage()); Object node = ((HtmlPage) lastPage()).getFirstByXPath(selector); if (node == null) throw new NoSuchElementException("Cannot locate a node using " + selector); if (node instanceof HtmlElement) return newHtmlUnitWebElement((HtmlElement) node); throw new NoSuchElementException(String.format("Cannot find element with xpath %s", selector)); }
public WebElement findElementByPartialLinkText(String using) { if (!(lastPage() instanceof HtmlPage)) { throw new IllegalStateException("Cannot find links for " + lastPage()); } List<HtmlAnchor> anchors = ((HtmlPage) lastPage()).getAnchors(); for (HtmlAnchor anchor : anchors) { if (anchor.asText().contains(using)) { return newHtmlUnitWebElement(anchor); } } throw new NoSuchElementException("No link found with text: " + using); }
private String getDomainForCookie(Cookie cookie) { URL current = lastPage().getWebResponse().getRequestUrl(); if (current.getPort() == 80) { return current.getHost(); } return String.format("%s:%s", current.getHost(), current.getPort()); } }
public WebElement findElementById(String id) { if (!(lastPage() instanceof HtmlPage)) throw new NoSuchElementException("Cannot find element by id for " + lastPage()); try { HtmlElement element = ((HtmlPage) lastPage()).getHtmlElementById(id); return newHtmlUnitWebElement(element); } catch (ElementNotFoundException e) { throw new NoSuchElementException("Cannot find element with ID: " + id); } }
@Override public WebDriver frame(int index) { Page page = lastPage(); if (page instanceof HtmlPage) { try { currentWindow = ((HtmlPage) page).getFrames().get(index); } catch (IndexOutOfBoundsException ignored) { throw new NoSuchFrameException("Cannot find frame: " + index); } } return HtmlUnitDriver.this; }
@Override public List<WebElement> findElementsByPartialLinkText(String using) { List<HtmlAnchor> anchors = ((HtmlPage) lastPage()).getAnchors(); List<WebElement> elements = new ArrayList<>(); for (HtmlAnchor anchor : anchors) { if (anchor.asText().contains(using)) { elements.add(newHtmlUnitWebElement(anchor)); } } return elements; }
@Override public String getPageSource() { Page page = lastPage(); if (page == null) { return null; } if (page instanceof SgmlPage) { return ((SgmlPage) page).asXml(); } WebResponse response = page.getWebResponse(); return response.getContentAsString(); }
public void addCookie(Cookie cookie) { Page page = lastPage(); if (!(page instanceof HtmlPage)) { throw new WebDriverException("You may not set cookies on a page that is not HTML"); } // Cookies only make sense if the page is String domain = getDomainForCookie(cookie); verifyDomain(cookie, domain); webClient.getCookieManager().addCookie(new org.apache.commons.httpclient.Cookie(domain, cookie.getName(), cookie.getValue(), cookie.getPath(), cookie.getExpiry(), cookie.isSecure())); }