0
点赞
收藏
分享

微信扫一扫

htmlunit最具有参考意义项目

### HtmlUnit What?

- 项目 ​​https://gitee.com/dgwcode/SimulationFang​​

这两个项目,是最新Htmlunit包下的新项目,很多东西在国内博客,百度,技术网站都找不到例子,有时间可以看看。

Introduction

The ​​dependencies page​​ lists all the jars that you will need to have in your classpath.

The class com.gargoylesoftware.htmlunit.WebClient is the main starting point. This simulates a web browser and will be used to execute all of the tests.

Most unit testing will be done within a framework like ​​JUnit​​ so all the examples here will assume that we are using that.

In the first sample, we create the web client and have it load the homepage from the HtmlUnit website. We then verify that this page has the correct title. Note that getPage() can return different types of pages based on the content type of the returned data. In this case we are expecting a content type of text/html so we cast the result to an com.gargoylesoftware.htmlunit.html.HtmlPage.

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient()){finalHtmlPage=.getPage("http://htmlunit.sourceforge.net");Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit",.getTitleText());finalString=.asXml();Assert.assertTrue(pageAsXml.contains("<body class=\"composite\">"));finalString=.asText();Assert.assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));}}

Submitting a form

Frequently we want to change values in a form and submit the form back to the server. The following example shows how you might do this.

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient()){// Get the first pagefinalHtmlPage=.getPage("http://some_url");// Get the form that we are dealing with and within that form, // find the submit button and the field that we want to change.finalHtmlForm=.getFormByName("myform");finalHtmlSubmitInput=.getInputByName("submitbutton");finalHtmlTextInput=.getInputByName("userid");// Change the value of the text field.type("root");// Now submit the form by clicking the button and get back the second page.finalHtmlPage=.click();}}

Imitating a specific browser

Often you will want to simulate a specific browser. This is done by passing a com.gargoylesoftware.htmlunit.BrowserVersion into the WebClient constructor. Constants have been provided for some common browsers but you can create your own specific version by instantiating a BrowserVersion.

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient(BrowserVersion.FIREFOX_52)){finalHtmlPage=.getPage("http://htmlunit.sourceforge.net");Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit",.getTitleText());}}

Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.

Finding a specific element

Once you have a reference to an HtmlPage, you can search for a specific HtmlElement by one of 'get' methods, or by using XPath or CSS selectors.

Traversing the DOM tree

Below is an example of finding a 'div' by an ID, and getting an anchor by name:

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient()){finalHtmlPage=.getPage("http://some_url");finalHtmlDivision=.getHtmlElementById("some_div_id");finalHtmlAnchor=.getAnchorByName("anchor_name");}}

A simple way for finding elements might be to find all elements of a specific type.

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient()){finalHtmlPage=.getPage("http://some_url");NodeList=.getElementsByTagName("input");finalIterator<E>=.iterator();// now iterate}}

There is rich set of methods usable to locate page elements e.g.

  • HtmlPage.getAnchors(); HtmlPage.getAnchorByHref(String); HtmlPage.getAnchorByName(String); HtmlPage.getAnchorByText(String)
  • HtmlPage.getElementById(String); HtmlPage.getElementsById(String); HtmlPage.getElementsByIdAndOrName(String);
  • HtmlPage.getElementByName(String); HtmlPage.getElementsByName(String)
  • HtmlPage.getFormByName(String); HtmlPage.getForms()
  • HtmlPage.getFrameByName(String); HtmlPage.getFrames()

You can also start searching from the document element (HtmlPage.getDocumentElement()) and then traverse the dom tree

  • HtmlElement.getElementsByAttribute(String, String, String)
  • DomElement.getElementsByTagName(String); DomElement.getElementsByTagNameNS(String, String)
  • DomElement.getChildElements(); DomElement.getChildElementCount()
  • DomElement.getFirstElementChild(); DomElement.getLastElementChild()
  • HtmlElement.getEnclosingElement(String); HtmlElement.getEnclosingForm()
  • DomNode.getChildNodes(); DomNode.getChildren(); DomNode.getDescendants(); DomNode.getDomElementDescendants(); DomNode.getFirstChild(); DomNode.getHtmlElementDescendants() DomNode.getLastChild(); DomNode.getNextElementSibling(); DomNode.getNextSibling(); DomNode.getPreviousElementSibling(); getPreviousSibling()

XPath queries

XPath is the suggested way for more complex searches, a brief tutorial can be found in ​​W3Schools​​

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient()){finalHtmlPage=.getPage("http://htmlunit.sourceforge.net");//get list of all divsfinalList<?>=.getByXPath("//div");//get div which has a 'name' attribute of 'John'finalHtmlDivision=(HtmlDivision).getByXPath("//div[@name='John']").get(0);}}

CSS Selectors

You can also use CSS selectors

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient()){finalHtmlPage=.getPage("http://htmlunit.sourceforge.net");//get list of all divsfinalDomNodeList<DomNode>=.querySelectorAll("div");for(DomNode:){....}//get div which has the id 'breadcrumbs'finalDomNode=.querySelector("div#breadcrumbs");}}

Using a proxy server

The last WebClient constructor allows you to specify proxy server information in those cases where you need to connect through one.

@Testpublicvoid()throwsException{try(finalWebClient=newWebClient(BrowserVersion.FIREFOX_52,"myproxyserver",)){//set proxy username and password finalDefaultCredentialsProvider=(DefaultCredentialsProvider).getCredentialsProvider();.addCredentials("username","password");finalHtmlPage=.getPage("http://htmlunit.sourceforge.net");Assert.assertEquals("HtmlUnit - Welcome to HtmlUnit",.getTitleText());}}

Specifying this BrowserVersion will change the user agent header that is sent up to the server and will change the behavior of some of the JavaScript.

举报

相关推荐

0 条评论