Setting up Selenium WebDriver for Java

 

Getting set up with Selenium-WebDriver for Java is fairly straightforward, but since the release of version 3 there have been some changes that you have to be aware of. In this post we’re going to explore step-by-step what is required to set up and run a basic Selenium WebDriver test. So, whether you’ve set up an automation architecture with Selenium WebDriver before or this is your first time, it’s worth reading through for the practice.

At the time of writing, we will be using IntelliJ CE 2017.3, Java 1.8, Maven 3.5.2, Selenium WebDriver 3.11.0 and JUnit 4.12 for this tutorial.

Set up your Maven project

Our project will be managed via Maven so our first port of call is to create a new Maven project in IntelliJ. Simply create a new project by opening the IntelliJ project wizard and choosing a Maven project:

 Maven pro new project screenshot

Click on ‘Next’ and you will be asked to fill in your Maven project details:

 New project screenshot

Once you have entered your project details, set where you want to store your project locally and click 'Finish' to create your project:

 Finish new project screenshot


Set up your dependencies

With your Maven project set up, IntelliJ will have generated a pom.xml file to which we will add our dependencies, to install and run Selenium-WebDriver:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.11.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>24.1-jre</version>
    </dependency>
</dependencies>

Once complete, your pom.xml will look similar to the following:

 pom.xml screenshot

The junit dependency is the library we will be using to run the test. The selenium-java dependency is what we will be using to drive our browser and extract data from it. The guava dependency contains libraries that are used by selenium-java if you want to work with Google Chrome.


Create your First junit test

With Junit and Selenium WebDriver installed we can begin to create our first test. To follow the correct Maven pattern we will create a new class in the src/test/java folder named MyFirstTest, like so:

 Creating first junit test screenshot

With our class now created, let’s create our first test within the MyFirstTest class to execute:

@Test
public void testOne (){
    // Test code will be added here
}

The resulting test will look like this:

 resulting test screenshot

Finally, to confirm the Junit test is all set up correctly and that IntelliJ is configured to run your test, right click on the @Test annotation and select “Run ‘testOne()’”, like so:

 Run ‘testOne()’ screenshot


Setting up a browser driver

With our Junit code in place, we can now add in our Selenium WebDriver code; but before we dive straight into the code we need to install one last dependency. We need to install a browser driver for Selenium WebDriver to use.

Why do I need this?

When we use Selenium WebDriver, it talks to a browser ‘driver’ which translates your code calls into executable actions within the browser. In the past, the driver used to live inside the browser, but now for the sake of maintenance and to reduce browser file size, the drivers are separated from the browser and work like so:

 
 Your code - Browser Driver - Browser flow chart
 

So, to run your code against a browser we have to download and configure WebDriver to speak to a local copy of Browser Driver which will receive your calls from your code and carry actions against the browser. Failing to configure WebDriver with a Browser Driver will result in an IllegalStateException.

Each browser has its own driver; for example, we use ChromeDriver for Google Chrome and GeckoDriver for Mozilla Firefox.

As we are attempting to drive Google Chrome with Selenium WebDriver, we need to install the latest version of ChromeDriver and you can find the latest version of ChromeDriver here. Look for the latest version and download the version that works for your operating system. For this tutorial we are running this on a Mac, so we will download the _mac64.zip version.

Once you have downloaded the file and extracted, you have two options:

  1. You can hardcode the path of where your copy of ChromeDriver is stored locally. This is a bit of a hacky way of doing it, because if you move your code or the ChromeDriver to a different folder, it will break your code.

  2. The second option, which is a bit more work, is to add the folder to your PATH. WebDriver will then check for ChromeDriver on your PATH and if it is there, use it. For details on how to update your PATH, the Java website has a comprehensive guide for different platforms.

For now, we are going to go with 'quick and dirty' option 1 to keep the tutorial size down. So, with ChromeDriver installed and extracted we can add the following code in a 'Before' hook in Junit:

private WebDriver driver;

@Before

public void setup(){

String pathToChromeDriver = "lib/chromedriver";

System.setProperty("webdriver.chrome.driver", pathToChromeDriver);

driver = new ChromeDriver();

}

'Before' hooks are used to run methods before the test and are a good place to add in setup code. Add this all in and you will end up with the following code:

Adding in test code 5.2

Once you have all your code in place, again try running your test. If everything is correct with the path then an instance of Chrome will start up. If it doesn’t, check your path in the code OR try option 2 and add your ChromeDriver to your PATH.

Adding in your test code

Now that we have Selenium WebDriver set up, we can add in some code to drive our browser, like so:

driver.navigate().to("http://the-internet.herokuapp.com/login");

driver.findElement(By.id("username")).sendKeys("tomsmith");

driver.findElement(By.id("password")).sendKeys("SuperSecretPassword!");

driver.findElement(By.className("radius")).click();

String title = driver.findElement(By.cssSelector(".example h2")).getText();;

assertThat(title, is("Secure Area"));

Which results in the following code:

Adding in test code 5.2

With the code added in, run the test one more time to see the browser running, navigating to a page with a form and then filling in the details. Finally, you may notice that you have lots of instances of Chrome running now, so we can add in this final piece of code to close down the browser once the test is complete:

@After
public void teardown(){
    driver.close();
}

Which gives you this final code:

 Final code screenshot

And that’s you set up with Selenium-WebDriver. There is still a lot more to learn about how to use the tooling, but this should be enough to get you started.

Happy exploring!


You may also like…