• Jobs
  • About
  • Getting started with HtmlUnit April 19, 2010

    As mentioned in the Introduction to HtmlUnit post, HtmlUnit is a great test automation tool for driving simulated browsers for testing web applications. This post describes:

    • Downloading and setting up HtmlUnit
    • Writing your first test in Java.

    If you have an environment similar to the one defined in the Creating your Java project workspace post, it will be a little easier to setup htmlunit for your project.

    Downloading and setting up HtmlUnit

    At the time of this post, the latest release of HtmlUnit is version 2.7. Follow these steps to add HtmlUnit in your project.

    1. Download the latest HtmlUnit from sourceforge. At this time the latest version is 2.7 which is distributed as a zip file. Save this in your Downloads directory … on unix lets assume its located at ~/Downloads
    2. Unzip the file:
      unzip htmlunit-2.7.zip
      This creates a htmlunit-2.7 directory.
    3. Now you need to have all of htmlunit lib jars if your lib directory. There are actually two choices:
      • Copy ~/Downloads/htmlunit-2.7/lib contents in your project lib (testautomation/lib/.) directory or,
      • Copy ~/Downloads/htmlunit-2.7/lib contents in a new directory under your project lib (testautomation/lib/htmlunit/.) directory

    Writing your first test in Java

    Create a class org.qaautomation.examples.GoogleHtmlUnitTest.java

    package net.qaautomation.examples;
    
    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    /**
     * A simple Google search test using HtmlUnit.
     *
     * @author Rahul Poonekar
     * @since Apr 18, 2010
     */
    public class GoogleHtmlUnitTest {
        static final WebClient browser;
    
        static {
            browser = new WebClient();
            browser.setJavaScriptEnabled(false);
        }
    
        public static void main(String[] arguments) {
            boolean result;
            try {
                result = searchTest();
            } catch (Exception e) {
                e.printStackTrace();
                result = false;
            }
    
            System.out.println("Test " + (result? "passed." : "failed."));
            if (!result) {
                System.exit(1);
            }
        }
    
        private static boolean searchTest() {
            HtmlPage currentPage;
    
            try {
                currentPage = (HtmlPage) browser.getPage("http://www.google.com");
            } catch (Exception e) {
                System.out.println("Could not open browser window");
                e.printStackTrace();
                return false;
            }
            System.out.println("Simulated browser opened.");
    
            try {
                ((HtmlTextInput) currentPage.getElementByName("q")).setValueAttribute("qa automation");
                currentPage = currentPage.getElementByName("btnG").click();
                //System.out.println("contents: " + currentPage.asText());
                return containsPattern(currentPage.asText(), "About .* results");
            } catch (Exception e) {
                System.out.println("Could not search");
                e.printStackTrace();
                return false;
            }
        }
    
        public static boolean containsPattern(String string, String regex) {
            Pattern pattern = Pattern.compile(regex);
    
            // Check for the existence of the pattern
            Matcher matcher = pattern.matcher(string);
            return matcher.find();
        }
    }
    

    Do
    ant build
    and
    ant run-example -Dexample=GoogleHtmlUnitTest
    to run the example.

    package net.qaautomation.examples;

    import com.gargoylesoftware.htmlunit.WebClient;
    import com.gargoylesoftware.htmlunit.html.HtmlPage;
    import com.gargoylesoftware.htmlunit.html.HtmlTextInput;

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    /**
    * A simple Google search test using HtmlUnit.
    *
    * @author Rahul Poonekar
    * @since Apr 18, 2010
    */
    public class GoogleHtmlUnitTest {
    static final WebClient browser;

    static {
    browser = new WebClient();
    browser.setJavaScriptEnabled(false);
    }

    public static void main(String[] arguments) {
    boolean result;
    try {
    result = searchTest();
    } catch (Exception e) {
    e.printStackTrace();
    result = false;
    }

    System.out.println(“Test ” + (result? “passed.” : “failed.”));
    if (!result) {
    System.exit(1);
    }
    }

    private static boolean searchTest() {
    HtmlPage currentPage;

    try {
    currentPage = (HtmlPage) browser.getPage(“http://www.google.com”);
    } catch (Exception e) {
    System.out.println(“Could not open browser window”);
    e.printStackTrace();
    return false;
    }
    System.out.println(“Simulated browser opened.”);

    try {
    ((HtmlTextInput) currentPage.getElementByName(“q”)).setValueAttribute(“qa automation”);
    currentPage = currentPage.getElementByName(“btnG”).click();
    //System.out.println(“contents: ” + currentPage.asText());
    return containsPattern(currentPage.asText(), “Results .* for qa automation”);
    } catch (Exception e) {
    System.out.println(“Could not search”);
    e.printStackTrace();
    return false;
    }
    }

    public static boolean containsPattern(String string, String regex) {
    Pattern pattern = Pattern.compile(regex);

    // Check for the existence of the pattern
    Matcher matcher = pattern.matcher(string);
    return matcher.find();
    }
    }

    Posted by Rahul Poonekar in : HtmlUnit

    Leave a Reply

    Your email address will not be published. Required fields are marked *