Selenium Chrome WebDriver Test Cases with JUnit in Java

In this example we will show you how to run Selenium Chrome WebDriver Test Cases with JUnit in Java. In order to run Selenium Chrome Test Cases you need the ChromeDriver which you can download here. You need to set the location as a system property namely: webdriver.chrome.driver. You can add this property as a system property or you can pass the value as an argument to maven which I explained below.

Maven Dependencies

You need to add the following dependencies to your project.

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.44.0</version>
</dependency>

Selenium Chrome WebDriver with JUnit

You need to create a ChromeDriver implementation of the WebDriver. You can do this by creating a new ChromeDriver(). This ChromeDriver needs to know where the executable file is located. You can tell this by setting the webdriver.chrome.driver either by a System Property or you can pass this as a Maven Argument. After you initialized the ChromeDriver you can do the actual testing. For simplicity we just acquire a web resource. After the test is completed we need to clean up the WebDriver.

package com.memorynotfound.test;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class ChromeSeleniumTest {

    private static WebDriver driver;

    @BeforeClass
    public static void setUp(){
        System.setProperty("webdriver.chrome.driver", "/selenium-driver/chromedriver");
        driver = new ChromeDriver();
    }

    @Test
    public void testChromeSelenium() {
        driver.get("http://www.memorynotfound.com/");
    }

    @AfterClass
    public static void cleanUp(){
        if (driver != null) {
            driver.close();
            driver.quit();
        }
    }

}

Executing Selenium Chrome WebDriver Test Cases with Maven

When you execute the Selenium Chrome WebDriver Test Cases you can see that chrome start up a new instance and that the page is reached.

mvn clean test

Info: If you don’t want to hard code the location of the chrome driver, you can also pass the location of chrome driver as an argument to Maven.

mvn clean test -Dwebdriver.chrome.driver=/selenium-driver/chromedriver

Output

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SELENIUM - chrome-example 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------ 
[...] 
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running com.memorynotfound.test.ChromeSeleniumTest
Starting Selenium Chrome WebDriver 2.14.313457 (3d645c400edf2e2c500566c9aa096063e707c9cf) on port 36247
Only local connections are allowed.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.492 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS (Selenium Chrome WebDriver)
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 7.483 s
[INFO] Finished at: 2015-02-06T19:22:13+01:00
[INFO] Final Memory: 15M/245M
[INFO] ------------------------------------------------------------------------

References

Download

You may also like...