HtmlUnit – A quick introduction

HtmlUnit is an open source java library for creating HTTP calls which imitate the browser functionality. HtmlUnit is mostly used for integration testing upon Unit test frameworks such as JUnit or TestNG. This is done by requesting web pages and asserting the results. Simple Example WebClient As you can see in the example, the WebClient is the starting point. It is the browser simulator. WebClient.getPage() is just like typing an address in the browser. It returns an HtmlPage object. HtmlPage HtmlPage represents a single web page along with all of it’s client’s data (HTML, JavaScript, CSS …). The HtmlPage lets you access to many of a web page content: Page source You can receive the page source as text or as XML. HTML Elements HtmlPage lets you ability to access any of the page HTML elements and all of their attributes and sub elements. This includes tables, images, input fields, divs or any other Html element you may imagine. Use the function getHtmlElementById() to get any of the page elements. Anchors Anchor is the representation of the Html tag <a href=”…” >link</a>. Use the functions getAnchorByName(), getAnchorByHref() and getAnchorByText() to easily access any of the anchors in the page. Dom Continue reading HtmlUnit – A quick introduction