4

I came across an unusual issue with my automation tests while using the HtmlUnitDriver. I have been using the junit parameterized class to iterate over a list of urls to run some of my tests. The test runs just fine with other WebDriver implementations - InternetExplorerDriver, FirefoxDriver and ChromeDriver without any issues. However, with HtmlUnitDriver the test just terminates without any error message. While Debugging, I tried to print my data source (list of urls ) all I got was an empty array in case of HtmlUnitDriver but I get the urls in other WebDriver implementations. So, I am kind of confused how and why is this happenning.

My CoursePage pageobject method returns the urls in an ArrayList.

public  ArrayList<String[]> getPageURLs() throws IOException {

    ArrayList<String[]> forumURLs= new ArrayList<String[]>();
    //forums is a List<WebElement> field on my PageObject that returns a list of elements    
    for(WebElement forum : forums)
    {
        String pageURLs = forum.getAttribute("href");
        //I filter out some urls with if else statements and add only those that are required
        forumURLs.add(new String[]{pageURLs});
    }

    return forumURLs;
}

This is the JUnit method that gets the data:

@Parameters
public static ArrayList<String[]> getURLs() throws IOException{
    startBrowser(Browser.HTMLUNIT);
    LoginPage loginPage =goToLoginPage();
    HomePage homepage =loginPage.loginAs(username, password);
    CoursePage coursepage = homepage.goToCourse();
    return coursepage.getPageURLs();
}
3
  • 1
    I have a theory, but first, have you tried adding print statements to narrow down exactly where it fails?
    – user246
    Commented Sep 26, 2012 at 18:51
  • yes, it fails in the last line of the junit method shown above. Doesn't return anything for the coursepage.getPageURLs(); But does return the urls for all other webdriver implementatiosn. Would you please say something about your views?
    – StatusQuo
    Commented Sep 26, 2012 at 19:06
  • Have you made sure you've got the HTMLUnitDriver configured with javascript explicitly enabled? If you're running in the default mode without javascript, that could explain why a lot of browser behaviours are different. Commented Jul 24, 2013 at 10:41

1 Answer 1

2

First, you did not mention why you are using HtmlUnitDriver, but I have never had a lot of luck using it. It does not seem to behave enough like any of the supported browsers, so it I could not use my HtmlUnitDriver results to infer anything about whether a test would actually pass or fail on on real browser.

Second, does your webapp use Javascript to add elements to the page? It is possible that the Javascript does not behave as expected on HtmlUnitDriver, and consequently your course page does not contain what you think it contains.

2
  • This is my first time using the HtmlUnitDriver. After reading about how fast it is I wanted to give it a try so that I can , in the long run, manage all regression tests with HtmlUnitDriver. I do have other tests that run OK with HtmlUnitDriver but just this one that falters.So, it's the getPageURls() method that is in confilct with HtmlUnitDriver for sure but I am totally confused how and why.I do have the javascript to true while instantiating the HtmlUnitDriver.
    – StatusQuo
    Commented Sep 26, 2012 at 19:39
  • 1
    @StatusQuo Javascript interpretation depends on the browser; if you have a javascript issue on your home page (or HTMLUnit cannot parse the javascript, it might be correct but HTMLUnit might not be capable of running it), there may not be any URLs on the page. No URLs to scrape means no data to parameterize. Commented Jun 2, 2014 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.