Getting Started: Selenium (ChromeDriver) based UI automated test using Java, JUNIT and Maven – Example

This post provides a basic and simple example for an automated acceptance test using Java, JUNIT and Selenium based ChromeDriver.

Selenium WebDriver is an open source tool for developing automated tests for web applications using Java.

Other testing tutorials:
How to mock static, private methods (Powermock tutorial)
Unit testing void methods using verify and ArgumentCaptor
Mockito Deep Stubs Example
Selenium based automated test using Java, JUNIT and Maven – Example

This example uses ChromeDriver

  • an implementation of Selenium WebDriver.
  • developed by Selenium in collaboration with Chromium team.

SimpleAcceptanceTest.java


package com.idodevjobs.example.acceptance;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import static org.junit.Assert.assertEquals;
import static org.openqa.selenium.support.ui.ExpectedConditions.visibilityOfElementLocated;

public class SimpleAcceptanceTest {
    private WebDriver webDriver = new ChromeDriver();

    @Before
    public void setup() {
        System.getProperties().setProperty("webdriver.chrome.driver", "chromedriver");
    }

    @Test
    public void shouldOpenBlogPostWithProperContent() throws Exception {
        webDriver.get("https://idodevjobs.wordpress.com/2015/04/13/key-java-5-6-7-8-features-flash-card/");
        WebDriverWait wait = new WebDriverWait(webDriver, 5);
        WebElement firstParagraph = wait.until(visibilityOfElementLocated(By.xpath("//*[@id='post-310']/div[@class='entry-content']/p[1]")));
        String firstParagraphText = firstParagraph.getText();
        assertEquals("The text in first paragraph is incorrect.", "Some key Java 5, 6, 7 & 8 features – Flash card.", firstParagraphText);
    }

    @After
    public void tearDown() {
        webDriver.quit();
    }
}

[pom.xml]


<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.idodevjobs.example</groupId>
  <artifactId>bdd-acceptance-tests-junit-selenium-example</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>bdd-acceptance-tests-junit-selenium-example</name>
  <url>http://maven.apache.org</url>

  <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>2.47.1</version>
          <scope>test</scope>
      </dependency>
  </dependencies>
</project>

The objective of this test to open a blogpost and check for the presence of a particular text.

This expects ChromeDriver to be present in the root directory of the project. The ChromeDriver executable can be downloaded from http://chromedriver.storage.googleapis.com/index.html?path=2.16/.

The test can be executed on any IDE – For example, on Intellij IDEA by right clicking the class and clicking ‘Run SimpleAcceptanceTest’.

Advertisements
This entry was posted in java and tagged , , , , . Bookmark the permalink.

3 Responses to Getting Started: Selenium (ChromeDriver) based UI automated test using Java, JUNIT and Maven – Example

  1. Pingback: Mockito Example – Unit testing void methods using verify and ArgumentCaptor | iDoDevJobs

  2. Pingback: Mockito Deep Stubs Example | iDoDevJobs

  3. Pingback: Powermock – How to mock static, private methods (Powermock tutorial) | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s