Bundling ChromeDriver with your test code

This post will just exemplify one way of maintaining the chrome drivers used for running automated tests with WebDriver and the Chrome browser without having to update and install the ChromeDriver to all possible nodes where tests will be running. The ChromeDriver will simply be bundled with the running tests and put under source control as for all other test ware.

Downloading links
http://code.google.com/p/selenium/wiki/ChromeDriver

Bundling
Start by putting the the drivers under the resource folder so that they will be picked up by Maven per default.

<project root>/src/main/resources/chromedriver/mac/chromedriver
<project root>/src/main/resources/chromedriver/windows/chromedriver.exe

Implementation
There are of course different drivers for different OS types and this needs to be handled using the os.name system property.

As per the ChromeDriver usage instructions (here) a system property has to be set pointing to the ChromeDriver server to use for the Chrome browser bridging. We will not point to a fixed location in the file system, instead well get the path by using the Class.getResource which will enable us to bundle ChromeDriver inside our test framework even if it is bundled into a jar file.

Basically what should be done are the following steps.

  • Determine OS type
  • Get the Chrome Driver resource and make sure it is executable using File.setExecutable(true). This is due to when packaged in a jar the execution attributes ‘x’ will be removed on Mac (and assumed on Linux too).
  • Set the “web driver.chrome.driver” system property.
  • Check that a Chrome installation exists in the default location [OPTIONAL]
private static WebDriver driver = null;
// The ChromeDriver locations under the resource folder
private static String MAC_DRIVER = "/chromedriver/mac/chromedriver";
private static String WINDOWS_DRIVER = "/chromedriver/windows/chromedriver.exe";

public static void setupChromeDriver() {
   // OS type
   if (System.getProperty("os.name").contains("Mac")) {
      File cDriver = new File(Tester.class.getResource(MAC_DRIVER).getFile());

      // Is it executable
      if (!cDriver.canExecute()) {
         cDriver.setExecutable(true);
      }
      System.setProperty("webdriver.chrome.driver", Tester.class.getResource(MAC_DRIVER).getFile());

      // Now checking for existence of Chrome executable.'
      if (!new File("/Applications/Google Chrome.app/Contents/MacOS/Google Chrome").exists()) {
         throw new RuntimeException(errorMessage);
      }
   } else {
      System.setProperty("webdriver.chrome.driver", Tester.class.getResource(WINDOWS_DRIVER).getFile());

      // Now checking for existence of Chrome executable.'
      if (!new File(System.getProperty("user.home") + "/AppData/Local/Google/Chrome/Application/chrome.exe").exists()) {
         throw new RuntimeException(errorMessage);
      }
   }

   ChromeOptions options = new ChromeOptions();
   options.addArguments("--start-maximized");
   options.addArguments("--ignore-certificate-errors");
   driver = new ChromeDriver(options);
}

Test case example
Pretty straight on from here, setup WebDriver through the implemented method above and run a simple open page test to see that things worked out.

private static WebDriver driver = null;
public static void setupChromeDriver(){
   ...
}

@BeforeClass
public static void setupTestClass() throws Exception {
   setupChromeDriver();
}

@Test
public void demoTestCase() throws Exception {
   driver.get("http://code.google.com/p/selenium/wiki/ChromeDriver");
   Thread.sleep(1000);
}
Advertisements

WebDriver tricks of the trade

A post to be used as a reference for items/issues that has surfaced when using WebDriver with Java.

.

Muting WebDriver logs

At points where WebDriver is really verbose it is quite convenient to mute the output or at least certain levels of it.

// Java
// Set it before creating the WebDriver instance to avoid having any messages generated from the
// constructor call
RemoteWebDriver.setLogLevel(Level.WARNING); // java.util.logging.Level
WebDriver driver = new FirefoxDriver(profile);

.

Avoiding untrusted SSL cerificate errors when using Firefox

Typical test environment issue. A simple workaround that worked for us was to simply set the profile used to accept and assume any certificate issues.

// Java
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
profile.setAssumeUntrustedCertificateIssuer(false);
WebDriver driver = new FirefoxDriver(profile);

.

Running FireFox in full screen

Especially handy when combining Sikuli and WebDriver, you need to be absolutely sure that as much as possible is visible on the screen.

((FirefoxDriver)driver).getKeyboard().pressKey(Keys.F11);

.

Relative xpaths from the current WebElement

Very useful when working with complex HTML-pages whose elements are very deep down and can only be found using xpath. Maintaining these long xpaths in multiple places in the test code base is a pain. The solution is to do the xpathing in parts to minimize the effort needed to maintain the xpath-strings.

<html>
  <body>
    <table>
	  <tbody>
	    <th></th>
        <tr>
		  <td>cell to find</td><td></td>
		</tr>
		<tr>
		  <td></td><td></td>
		</tr>
      </tbody>
	</table>
  </body>
</html>

Starting the xpath-string with the DOT is equivalent to starting the search from the current node.

WebElement tableElement = driver.findElement(By.xpath("//html/body/table"));
// Using relative xpath from the beginning of the table to find the first cell.
// Note that it has to start with the '.', DOT
WebElement cell = driver.findElement(By.xpath("./tbody/tr[1]/td[1]"));

.

Switching between open windows

Based on words in the page title.

Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
  driver.switchTo().window(window);
  if (driver.getTitle().contains(pageTitle)) {
    break;
  }
}

.

Dismissing/accepting alert dialog windows

Whenever an alert window pops-up it has to be accepted or dismissed to avoid blocking the test execution, this is simply handled using Alert class (Selenium docs).

// WebDriver driver = new FirefoxDriver();
Alert alert = driver.switchTo().alert();
alert.accept();
// OR
// alert.dismiss();