47

I am trying to understand testing framework better and been looking into Selenium. I've used HTMLUnit before, mainly when I needed to scrape some information off website or the likes.

In the context of writing test automation, what's the advantage / disadvantages of Selenium vs HTMLUnit? Looks to me Selenium is more complicated to set up than HTMLUnit, although at the same time there's a HTMLUnitDriver for Selenium which I think behave the exact same way as in HTMLUnit itself?

Selenium obviously provides more robust framework, it has the Selenium RC for pararel testing, it also has different browser drivers that can be used - although when you used the browser drivers, the test will actually open/close a browser application rather than headless.

May be I am not understanding Selenium correctly. Some directions and pointers would be great!

On another note - a separate question - I am also looking at doing automated testing on mobile browser, I see that Selenium has an IPhoneDriver for it, but then this is not a headless testing either as it requires actual iOS simulator.

Is there anyway to do headless testing on mobile sites? Would changing user-agent be sufficient? I've seen a couple posts around changing user-agent that seem to have their own challenges, eg. Set user-agent in Selenium RC

Thanks a lot!

1
  • 1
    Have you thought of using PhantomJS or "Headless Chrome" instead ?
    – djangofan
    Commented Mar 6, 2014 at 15:57

6 Answers 6

70

well, would try to explain differences in detail.

Speaking about parallel testing, it better to use selenium grid. Basic concept of selenium RC and selenium grid. traditional selenium setup selenium grid setup selenium grid:Requesting a Specific Environment You can get into more details here

Some words about selenium webDriver:

The primary new feature in Selenium 2.0 is the integration of the WebDriver API. WebDriver is designed to providing an simpler, more concise programming interface along with addressing some limitations in the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.

How Does WebDriver ‘Drive’ the Browser Compared to Selenium-RC?

Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. How these direct calls are made, and the features they support depends on the browser you are using. Information on each ‘browser driver’ is provided later in this chapter. For those familiar with Selenium-RC, this is quite different from what you are used to. Selenium-RC worked the same way for each supported browser. It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser. WebDriver does not use this technique. Again, it drives the browser directly using the browser’s built in support for automation.

WebDriver and the Selenium-Server

You may, or may not, need the Selenium Server, depending on how you intend to use Selenium-WebDriver. If you will be only using the WebDriver API you do not need the Selenium-Server. If your browser and tests will all run on the same machine, and your tests only use the WebDriver API, then you do not need to run the Selenium-Server; WebDriver will run the browser directly. There are some reasons though to use the Selenium-Server with Selenium-WebDriver.

  • You are using Selenium-Grid to distribute your tests over multiple machines or virtual machines (VMs).
  • You want to connect to a remote machine that has a particular browser version that is not on your current machine.
  • You are not using the Java bindings (i.e. Python, C#, or Ruby) and would like to use HtmlUnit Driver

Selenium-WebDriver’s Drivers WebDriver is the name of the key interface against which tests should be written, but there are several implementations. These include:

HtmlUnit Driver This is currently the fastest and most lightweight implementation of WebDriver. As the name suggests, this is based on HtmlUnit. HtmlUnit is a java based implementation of a WebBrowser without a GUI. For any language binding (other than java) the Selenium Server is required to use this driver.

Pros

  • Fastest implementation of WebDriver
  • A pure Java solution and so it is platform independent.
  • Supports JavaScript

Cons

  • Emulates other browsers’ JavaScript behaviour (see below)

JavaScript in the HtmlUnit Driver None of the popular browsers uses the JavaScript engine used by HtmlUnit (Rhino). If you test JavaScript using HtmlUnit the results may differ significantly from those browsers. When we say “JavaScript” we actually mean “JavaScript and the DOM”. Although the DOM is defined by the W3C each browser has its own quirks and differences in their implementation of the DOM and in how JavaScript interacts with it. HtmlUnit has an impressively complete implementation of the DOM and has good support for using JavaScript, but it is no different from any other browser: it has its own quirks and differences from both the W3C standard and the DOM implementations of the major browsers, despite its ability to mimic other browsers. With WebDriver, we had to make a choice; do we enable HtmlUnit’s JavaScript capabilities and run the risk of teams running into problems that only manifest themselves there, or do we leave JavaScript disabled, knowing that there are more and more sites that rely on JavaScript? We took the conservative approach, and by default have disabled support when we use HtmlUnit. With each release of both WebDriver and HtmlUnit, we reassess this decision: we hope to enable JavaScript by default on the HtmlUnit at some point.

To investigate deeper into webDriver's setUp see this

From HtmlUnit documentation: HtmlUnit is not a generic unit testing framework. It is specifically a way to simulate a browser for testing purposes and is intended to be used within another testing framework such as JUnit or TestNG.

So to conclude Selenium and HtmlUnit difference: HtmlUnit is a java based implementation of a WebBrowser without a GUI and a way to simulate a browser for testing purposes and Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. we can see that HtmlUnit provides API without GUI possibility for automation whereas WebDriver provides internal browsers' possibilities for automation.

Speaking about mobile automation, Selenium also has an iPhone Driver iPhone Driver wiki article and Android Driver Android Driver wiki article

See also this presentation

Unfortunately I can not give you my working experience evaluation of mobile drivers as I deal with web automation (no mobile). Also know that Cucumber (automation tool) is popular among mobile automators. see this and this.

Hope it come a lil bit more clear for you now =)

3
  • Thanks a lot for the explanation! ... So the browser-specific drivers (rather than HTMLUnitDriver) is better because they test out exactly how the browser will behave - but this comes at a cost that browser pops in and out on your screen (just a minor annoyance when you are running your test locally you see this browser app pops in and out by itself) - There's no way around not having the actual browser load up (other than using HTMLUnitDriver) ?
    – TS-
    Commented Oct 10, 2012 at 18:03
  • there is one. If you find it annoying, you can get remote machine (VM) and get e.g. selenium tests run on remote machine. To share approach I use on current project: add new task on hudson -jenkins to run the set of tests on daily builds and get e-mail notification on test status. We should not forget the fundamental aim of automated test sets: no matter what kind of driver in use, we should be completely confident that functionality of application is stable and e.g ready for release. The details (HTMLUnitDriver or other choice) is up to you depending upon requirements and other dependencies. Commented Oct 10, 2012 at 23:03
  • A few years ago I used the JavaService tool to install Selenium RC as a Windows Service so that I could leave it running unattended as a headless process without being logged in to the machine. Jenkins would start the Windows Service via an ANT exec task and upon test suite completion another ANT task would stop the Service. I followed this bit.ly/pWS7yR to create a custom bat file to perform the service installation, for example pastebin.com/zvivguaP Commented Nov 6, 2013 at 21:27
3

Selenium and HTMLUnit are somewhat similar in concept, but Selenium is more mature/robust and has a lot more features.

Note that Selenium encompasses the recording (IDE) plugin for Firefox, which allows you to record tests and the RC/WebDriver automation framework which essentially drives a browser. The two can be used together to make test creation very easy.

The only advantage I could see to using HTMLUnit is that it is less resource intensive, so you could potentially run tests on less hardware, but with Selenium's parallel support even that isn't really true anymore.

2

When running tests from Jenkins overnight, you typically have no access to a windowing system such as X11 or Windows in which to run the web browser. I therefore see a benefit of using the HTMLUnit web driver in that case since it doesn't require access to a windowing system.

2
  • 2
    you can use Xvbf to run any gui browsers with Jenkins
    – ALex_hha
    Commented May 20, 2016 at 14:50
  • Selenium is depend on system too much, so I vote for HTMLUnit
    – dalmate
    Commented Aug 31, 2018 at 11:05
1

In my experience, HtmlUnit does a fine job with browsing automation, but might get a bit buggy when dealing with Javascript. I actually came to a case in which I wasn't able to automate an image download with HtmlUnit, and had to turn to Selenium, which performed beyond my expectations. The case is actually registered in an SO thread.

1

I have used Selenium WebDriver for automation. There is a very easy method to causing the browser to be headless. Simply apply ChromeOptions (In my case, other DriverOptions are available) ChromeOptions("Headless")

There are many proficient methods that can come from using Options or Services, as another example This will stop the Driver/CommandPrompt window from 'appearing' so it will remain 'silent' and unexposed. ChromeDriverServices ("Silent")

0

At least at UX systems you can use for example Xvfb and point the browsers to that display to make them "headless" See also http://infiniteundo.com/post/54014422873/headless-selenium-testing-with-firefox-and-xvfb or How do I run Selenium in Xvfb?

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