WebDriver 2.48 Released

The latest version of Selenium WebDriver, 2.48 has been released. You can get the direct link for the downloads for the Selenium Server and the Java and C# bindings below. The changelogs for each, as well as other language bindings can be found on the Selenium Downloads page

Downloads:

Advertisements

WebDriver 2.47 released

The latest version of Selenium WebDriver, 2.47.0 has been released. You can get the direct link for the downloads for the Selenium Server and the Java and C# bindings below. The changelogs for each, as well as other language bindings can be found on the Selenium Downloads page

Downloads:

Checking a checkbox with WebDriver

For something as simple as checking a checkbox you would think that all the different browser drivers would implement this in the same way. But sadly this is not the case. Firefox expects that you click on a checkbox to check or uncheck it whereas both the IEDriver and ChromeDriver expect you to type a space to do the same. This is all well and good for running a simple once off test, but we cant be expected to change code and re-build every time we want to test in a different browser.

Thankfully there is a solution at hand. Once we know which browser is currently running the test, we can decide which method to use. I have provided a C# and a Java code snippet below:

C#

IWebElement checkbox = driver.FindElement(By.Id("ElementID"));

if (((RemoteWebDriver)driver).Capabilities.BrowserName == "firefox")
{
    // Firefox
    checkbox.Click();
}
else
{
    // Chrome and IE
    checkbox.SendKeys(Keys.Space);
}

Java

WebElement checkbox = driver.findElement(By.id("idOfTheElement");

if (driver.Capabilities.BrowserName.Equals("firefox"))
{
    checkbox.Click();
}
else
{
    checkbox.SendKeys(Keys.Space);
}

If you come across any other discrepancies between the different driver implementations let us know in the comments.

Basic Java JUnit Test Setup

Here is a sample WebDriver Java test using the JUnit framework:

package WebDriverTest1;

import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class WebDriverTest1
{
  private WebDriver driver;

  @Before
  public void setup() throws Exception 
  {
    driver = new FirefoxDriver();
  }

  @After
  public void teardown() throws Exception 
  {
    driver.quit();
  }

  @Test
  public void WebDriverTips() 
  {
    driver.get("http://www.webdrivertips.com");
    String title = driver.getTitle();
    assertEquals("Title mismatch", "WebDriver Tips", title);
  }
}

As we can see from my earlier C# Example, there isn’t a world of difference between the two. There is no corresponding Test Fixture attribute but there are attributes for Before, After and Test. Similar to NUnit, the Before method gets executed before every test case, and the After method gets executed after every test case. Each test case is indicated with the @Test annotation.

Setting up your Mac for Java WebDriver Tests

There are a couple of setup steps required before you can run a Java JUnit WebDriver test on your Mac. As always there are a number of different ways this can be done but I will outline one that has worked well for me. I’ll just outline the steps involved first and then go through each of them in a little more detail after:

  1. Download and install “Eclipse IDE for Java EE Developers”
  2. Download and install Java
  3. Download JUnit
  4. Download and install Apache Ant
  5. Download Selenium WebDriver
  6. Create Project and lib folders
  7. Configure the build path (in Eclipse)

(1) Download and install “Eclipse IDE for Java EE Developers”

Go to http://www.eclipse.org and click on the downloads link. From this page download and install “Eclipse IDE for Java EE Developers”
Download Eclipse for Java EE Developers

(2) Download and install Java

  • Setup $JAVAHOME
  • Setup $CLASSPATH
  • Verify your Java installation by using
    java -version

(3) Download JUnit

Download the following JAR files from http://www.junit.org/ :

  • JUnit.jar
  • hamcrest.jar

(4) Download and install Apache Ant

Download and install Apache Ant. For further details on this please see my earlier post on Installing Ant. You can also find a link to Apache Ant page from my Resources page.
Don’t forget to verify your installation with

ant -version

(5) Download Selenium WebDriver

Download the following two JAR file from Selenium HQ Downloads: (see link in the Resources page.)

  • WebDriver jar file
  • Java Bindings jar file

(6) Create Project and lib folders

Eclipse uses a folder called a “workspace” in which all of its projects are located. On first opening Eclipse you will be prompted to specify a default workspace.

Inside this folder create a folder for your project. Inside this project folder create a “lib” folder and add the 4 JAR files you have downloaded earlier. Note you will have to unzip the Java Bindings zip file to get the seelnium-java JAR file.

(7) Configure the build path (in Eclipse)

Right click on the project name, and select Build Path > Configure Build Path. When the dialog is displayed, click on the button for Add External JARs.
ConfigureJavaBuildPath
From here select the 4 JAR files (hamcrest, junit, selenium-java & selenium-server-standalone) we have added to the /Workspace/Project/lib folder in the previous step.

And that concludes the basic setup required to run Java/JUnit Selenium WebDriver tests on your Mac. You are now ready to create and run a Selenium WebDriver Java test using the JUnit framework. In the next post I will create a simple test and execute it from within Eclipse and also from the Command Line/Terminal using Ant.