Selenium Web Driver Continuous Integration with Maven


In this article, let me showcase on how to execute a simple Selenium Web Driver based Test Script from Maven.

Pre-Condition: You need to have an Eclipse IDE with Maven integration. Here I would be demonstrating using Eclipse Neon Release (4.6.0).

Maven & Eclipse IDE can be used to build your Selenium Web Driver test framework from a single window. Another important benefit of using Maven is that you can get all the Selenium library files and their dependencies by configuring the pom.xml file. Maven automatically downloads the necessary files from the repository for you while building the project.

To know more about configuring Selenium Web Driver in Eclipse ID please refer to my earlier article on the same here.

Follow the steps below to configure and run Selenium Web Driver Test Script from Maven.

Create a Maven Project in Eclipse IDE by selecting File --> New --> Project… --> Maven Project

clip_image002

Select “Create a simple project (skip archetype selection)” checkbox and click on Next Button.

clip_image004

In the New Maven Project Window, provide the Group Id & Artifact Id details as shown below and click on Finish Button.

clip_image006

Eclipse would create a Maven Demo project structure as shown below.

clip_image008

Create a Class under src/test/java folder as shown below.

clip_image010

clip_image012

Now add the below code snippet.

package com.selenium.examples;
import static org.junit.Assert.assertEquals;
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.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
public class GoogleSearchTest {
private WebDriver driver;
@Before
public void setUp() {
// Launch a new Firefox instance
driver = new FirefoxDriver();
// Maximize the browser window
driver.manage().window().maximize();
// Navigate to Google
driver.get("http://www.google.com");
}
@Test
public void testGoogleSearch() {
// Find the Search Box Text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Clear the existing text value
element.clear();
// Enter the Search Keyword
element.sendKeys("Software Testers Forum - Suntaragali");
// Submit the Search Query
element.submit();
// Wait for the page to get rendered, timeout after 10 seconds
new WebDriverWait(driver, 10).until(new
ExpectedCondition<Boolean>() {
public Boolean apply(WebDriver d) {
return d.getTitle()
.startsWith("Software Testers Forum - Suntaragali");
}
});
assertEquals("Software Testers Forum - Suntaragali - Google Search",driver.getTitle());
}
@After
public void tearDown() throws Exception {
// Close the browser
driver.quit();
}
}

Open pom.xml from Package Explorer and select the pom.xml tab in the Editor as shown below.

clip_image014

Add the WebDriver and JUnit dependencies highlighted in the following code snippet to pom.xml in the <project> node:

<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>

Now your POM XML file should look like below.

clip_image016

You can get the latest dependency details for Selenium Web Driver from http://docs.seleniumhq.org/download/maven.jsp , JUnit from https://maven.apache.org/surefire/maven-surefire-plugin/examples/junit.html and suppose if you are using TestNG instead of JUnit Unit Testing Framework then you can find it in http://testng.org/doc/maven.html.

Now that we have configured the pom.xml file, let us execute the Test Script. In order to do that, just right click on the pom.xml from the Package Explorer. Select Run As --> Maven test

clip_image018

You should be able to execute the Selenium Test Script successfully from Maven. In the console you should be able to see the Build Successful message and also reports will be generated and should be available in the Package Explorer as highlighted below.

clip_image020

In my next article, I would be writing about “How to generate HTML reports for Selenium Test Scripts from Maven”. Until then Happy Testing..!!

Selenium Web Driver Continuous Integration with Maven Selenium Web Driver Continuous Integration with Maven Reviewed by Suntaragali The Smart Techie on August 11, 2016 Rating: 5

2 comments: