Skip to content

November 15, 2011

7

Thucydides Release 0.4.26 – HTMLUnit support, and using different drivers in the same test case

by jfsmart

Thucydides 0.4.26 has just been released. Some of the new features in this release include HTMLUnit support and the ability to specify the browser you want to run tests in at both the test case and the individual test level, as well as a few bug fixes and under-the-hood improvements.

HtmlUnit support

You can now run tests in HTMLUnit, in addition to Firefox, Chrome and Internet Explorer. HTMLUnit is a fast, browserless driver that . The main advantage of HTMLUnit is speed – it will run much faster than any of the browser-based drivers. However HTMLUnit is not without it’s disadvantages. Firstly, since HTMLUnit does not open a browser, tests run in HTMLUnit will not have screenshots (although the HTML source code for each page is stored), which limits its value for ATDD reporting. And secondly, although HTMLUnit does have some Javascript support, it does not fully match the behaviour of a real browser. Nevertheless, there are times when you prefer fast, more low-level tests, and in this case HTMLUnit can be a useful option. To activate HTMLUnit, just provide “htmlunit” as the webdriver.driver system property, or define the driver for a test case or test as described in the following section.

Test-specific driver support

The @Managed annotation now supports the driver attribute, which lets you specify what driver you want to use for a particular test case. Current supported values are “firefox”, “iexplorer”, “chrome” and “htmlunit”. This lets you override the system-level default driver for specific requirements. For example, the following test case will run in Chrome, regardless of the webdriver.driver system property value used:

@RunWith(ThucydidesRunner.class)
@Story(Application.Search.SearchByKeyword.class)
public class SearchByFoodKeywordStoryTest {

    @Managed(uniqueSession = true, driver="chrome")
    public WebDriver webdriver;

    @ManagedPages(defaultUrl = "http://www.google.co.nz")
    public Pages pages;

    @Steps
    public EndUserSteps endUser;

    @Test
    public void searching_by_keyword_pears_should_display_the_corresponding_article() {
        endUser.is_the_google_home_page();
	endUser.enters("pears");
        endUser.starts_search();
	endUser.should_see_article_with_title_containing("Pear");
    }

    @Test
    @WithDriver("firefox")
    public void searching_by_keyword_pineapples_should_display_the_corresponding_article() {
        endUser.is_the_google_home_page();
	endUser.enters("pineapples");
        endUser.starts_search();
        endUser.should_see_article_with_title_containing("Pineapple");
    }
}

You can also use the @WithDriver annotation to specify a driver for an individual test. This will override both the system-level driver and the @Managed annotation’s driver attribute, if provided. For example, the following test will always run in Firefox:

	@Test
	@WithDriver("firefox")
	public void searching_by_keyword_pineapples_should_display_the_corresponding_article() {
	    endUser.is_the_google_home_page();
	    endUser.enters("pineapples");
	    endUser.starts_search();
	    endUser.should_see_article_with_title_containing("Pineapple");
	}

Advertisements
Read more from Tips and Tricks
7 Comments Post a comment
  1. Steph
    Nov 22 2012

    Hi and thanks for your tutorial.

    you wrote we can use @WithDriver(“firefox”) for a specific test or @Managed(uniqueSession = true, driver=”firefox”) for the driver itself.
    I tried to give the full path to executable because I have this error: Could not instantiate class org.openqa.selenium.firefox.FirefoxDriver

    firefox is installed, but not in PATH… because we have more than one firefox executable on the computer.
    Do you know a way to specify the path of the desired firefox exe?

    Thanks

    Reply
    • Steph
      Nov 22 2012

      Hi, again 🙂
      If I comment the annotation Managed and I set explicitely a new instance of WebDriver, I have the same issue.

      public WebDriver webdriver = new FirefoxDriver(new FirefoxBinary(new File(
      Config.getFirefoxPath())),
      Config.getFirefoxProfile());

      org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.

      If I do the same for a Junit test in another Selenium project, I can run the mvn test.

      Reply
      • Nov 23 2012

        Thucydides needs the @Managed annotation, and needs to manage the driver instance itself, so not a good idea to mess with this field. Have you tried using the webdriver.firefox.bin environment variable?

      • Steph
        Nov 23 2012

        Thanks for your answer.

        I tryied with

        static {
        System.setProperty(“webdriver.firefox.bin”,
        Config.getFirefoxPath());

        }

        @Managed(uniqueSession = true)
        public WebDriver webdriver;

        and as I had the same error I asked an administrator to set the webdriver.firefox.bin property in the system properties (we are under Win7), but after a restart I had still the same error; a WebDriverException because FirefoxDriver is not able to locate the executable specified in the system property.

      • Steph
        Nov 29 2012

        Feedback: after putting both webdriver.firefox.bin and webdriver.firefox.driver system variables with the path to firefox exe as value, all is working as expected.
        Tks

  2. Ralf77
    Nov 26 2012

    I see 4 messages, but the tile is “5 Comments”

    Reply

Trackbacks & Pingbacks

  1. Рубрика «Полезное чтиво». Выпуск 11 « XP Injection

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: