<?xml version="1.0"?>
<document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">

    <properties>
        <title>Getting Started with HtmlUnit</title>
        <author email="mbowler@GargoyleSoftware.com">Mike Bowler</author>
    </properties>

    <body>
        <section name="Introduction">
            <p>
                The <a href="dependencies.html">dependencies page</a> lists all the jars that you will need
                to have in your classpath.
            </p>
            <p>
                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.
            </p>
            <p>
                Most unit testing will be done within a framework like <a href="http://www.junit.org">JUnit</a>
                so all the examples here will assume that we are using that.
            </p>
            <p>
                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.
            </p>
            <source><![CDATA[
@Test
public void homePage() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    final String pageAsXml = page.asXml();
    assertTrue(pageAsXml.contains("<body class=\"composite\">"));

    final String pageAsText = page.asText();
    assertTrue(pageAsText.contains("Support for the HTTP and HTTPS protocols"));

    webClient.closeAllWindows();
}]]></source>
        </section>

        <section name="Imitating a specific browser">
            <p>
                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.
            </p>
            <source><![CDATA[
@Test
public void homePage_Firefox() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2);
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    webClient.closeAllWindows();
}]]></source>
            <p>
                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.
            </p>
        </section>

        <section name="Finding a specific element">
            <p>
                Once you have a reference to an HtmlPage, you can search for a specific HtmlElement by one of
                'get' methods, or by using XPath.
            </p>
            <p>
                Below is an example of finding a 'div' by an ID, and getting an anchor by name:
            </p>
            <source><![CDATA[
@Test
public void getElements() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://some_url");
    final HtmlDivision div = page.getHtmlElementById("some_div_id");
    final HtmlAnchor anchor = page.getAnchorByName("anchor_name");

    webClient.closeAllWindows();
}]]></source>
            <p>
                XPath is the suggested way for more complex searches, a brief tutorial can be found in
                <a href="http://www.w3schools.com/XPath/default.asp">W3Schools</a>
            </p>
            <source><![CDATA[
@Test
public void xpath() throws Exception {
    final WebClient webClient = new WebClient();
    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");

    //get list of all divs
    final List<?> divs = page.getByXPath("//div");

    //get div which has a 'name' attribute of 'John'
    final HtmlDivision div = (HtmlDivision) page.getByXPath("//div[@name='John']").get(0);

    webClient.closeAllWindows();
}]]></source>
        </section>

        <section name="Using a proxy server">
            <p>
                The last WebClient constructor allows you to specify proxy server information in those cases
                where you need to connect through one.
            </p>
            <source><![CDATA[
@Test
public void homePage_proxy() throws Exception {
    final WebClient webClient = new WebClient(BrowserVersion.FIREFOX_2, "http://myproxyserver", myProxyPort);

    //set proxy username and password 
    final DefaultCredentialsProvider credentialsProvider = (DefaultCredentialsProvider) webClient.getCredentialsProvider();
    credentialsProvider.addProxyCredentials("username", "password");

    final HtmlPage page = webClient.getPage("http://htmlunit.sourceforge.net");
    assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

    webClient.closeAllWindows();
}]]></source>
            <p>
                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.
            </p>
        </section>

        <section name="Submitting a form">
            <p>
                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.
            </p>
            <source><![CDATA[
@Test
public void submittingForm() throws Exception {
    final WebClient webClient = new WebClient();

    // Get the first page
    final HtmlPage page1 = webClient.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.
    final HtmlForm form = page1.getFormByName("myform");

    final HtmlSubmitInput button = form.getInputByName("submitbutton");
    final HtmlTextInput textField = form.getInputByName("userid");

    // Change the value of the text field
    textField.setValueAttribute("root");

    // Now submit the form by clicking the button and get back the second page.
    final HtmlPage page2 = button.click();

    webClient.closeAllWindows();
}]]></source>

        </section>
    </body>
</document>
