WebDrivers in XLT: How to Run Test Cases in Multiple Browsers

Update: Before you continue reading, here is a new version of this test suite and article: Multi-Browser-Suite for Test Automation. It makes things a lot easier.

Have you heard of XLT Script Developer yet? If you have, you’ll probably agree that it’s a convenient tool to record and run automated test cases. However, with it being a Firefox plugin, you’re basically bound to run your test cases in Firefox. Wouldn’t it be nice to reuse Script Developer test cases in multiple browsers? You can actually do so by taking advantage of the WebDriver API that is part of the XLT framework.

If you don’t know much about WebDrivers, you should continue reading this article – the first one of a little article series on WebDrivers that hopefully gives you a good introduction to the topic.

WebDriver is a tool for automating web application testing that was originally introduced by Google. The API permits control of real web browsers, such as Firefox, Internet Explorer, Chrome, Safari, and the HtmlUnit headless browser.

Let’s start with the basics: How to use WebDrivers in the XLT context.

Script Developer test cases are stored in XML files. If you want to execute your test cases in multiple browsers, you need to run them as part of an XLT testsuite in your Java IDE, e.g. Eclipse. You can do so in two ways:

  1. Reuse the original Script Developer file (XML) within a JUnit wrapper class
  2. Export the test case to Java

Both options differ in the opportunities they offer: while the first one forces you to stick with Script Developer for test case editing and maintenance, the second option lets you make full use of Java.

If you go for the first option, select Generate JUnit wrapper class for test cases in the Script Developer Settings and you end up with a JUnit wrapper class for each test case.

XLT Script Developer Menu
XLT Script Developer Menu
XLT Script Developer Settings
XLT Script Developer Settings

You can now find this generated java class as part of your XLT testsuite in a folder defined by your Script Developer package structure.

In the following, this generated Java class needs manual editing. To prevent that your changes will be overwritten, uncheck “Generate JUnit wrapper class for test cases” because the wrappers are automatically regenerated after each modification in the related test case.

You can also make a copy of the wrapper class right after its creation and edit the copy. In that case, you don’t need to uncheck the checkbox after generating the wrappers.

The original wrapper looks like this:

You can now execute the test by selecting “Run as JUnit test” from the Eclipse context menu of this specific file. By default, the XltDriver is used to run the test in a headless HtmlUnit-based browser.

Now, here comes the interesting part: to set your preferred WebDriver, add a constructor to the wrapper class. The example below makes use of the Chrome WebDriver:

To make this work smoothly, you need to pay attention to the WebDrivers for Chrome and Internet Explorer. Download and install the driver files from the locations given above. Also make sure you’ve installed the latest versions of these browsers. Last not least, a system property setting is required right before calling the setWebDriver method.

Use this code line for Chrome:

System.setProperty("webdriver.chrome.driver",PATH TO DRIVER);

Use this one for Internet Explorer:

System.setProperty("webdriver.ie.driver",PATH TO DRIVER);

The placeholder PATH TO DRIVER has to be replaced with the location of the driver file. In Linux and MAC OSX systems, the path ends with /chromedriver, in Windows with /chromedriver.exe or /IEDriverServer.exe.

Pretty easy, isn’t it? But what about the second option? Why export test cases to Java code? The commands in Script Developer test cases are strictly sequential and thus may at times restrict your programming ambitions. If you want to take advantage of all the opportunities offered by Java, such as implementing loops, adding randomization, or forcing a conditional execution, you should convert your test case into Java code.

Right-click your test case or test package in Script Developer, choose “Export” from the context menu, select XLT Scripting API from the dropdown, and then confirm the export. Again, you’ll find the exported java class as part of your XLT testsuite in a folder defined by your Script Developer package structure.

An exported Java class looks like this:

In the given example, the XltDriver is called by default (at line 5). You can change this into new InternetExplorerDriver() to use Internet Explorer WebDriver or one of the other available ones instead (Firefox, Chrome, Opera, iPhone, Android, Safari).

To help you understand the whole issue, the example below illustrates the complete procedure for the JUnit wrapper class, including the required imports, the property setting, and a useful way to automatically close the browser after test execution.

Now that you know how to run automated Script Developer test cases in all major browsers, you’re well prepared for the next part of this series: the challenges of test case design in different WebDrivers.