Execute selenium webdriver testng.xml using Maven

We can execute our tests using maven Surefire plug-in. This plug-in is used during the test phase of software build lifecycle to execute tests. To configure Surefile Plug-in, we need to add the snippet as below in pom.xml file. And also we need to add TestNG dependency to the pom.xml file.

To get started with TestNG, we need to include the following dependency in our project configuration file Pom.xml

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.9.x</version>
<scope>test</scope>
</dependency>
To run selenium tests, we need to include following dependency to pom file :

<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.xx.x</version>
</dependency>
After adding dependencies, navigate to project directory and run maven command from command-line which downloads all dependencies and adds them to the project.
mvn clean install

We have to include, maven-compiler-plugin and maven-surefire-plugin to the configuration file Pom.xml

Maven-compiler-plugin: Compiler-plugin is used to compile the sources of our project

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>${jdk.level}</source>
<target>${jdk.level}</target>
</configuration>
</plugin>
Maven-surefire-plugin: Surefire-plugin is responsible for running tests that are placed in test source directory /src/test/java.

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.19.1</version>
<configuration>
<suiteXmlFiles>
<!– TestNG suite XML files –>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
By default, Surefire-plugin runs all tests that matches with filename pattern such as *Test.java in test source directory src/test/java . To use a different naming scheme, we can configure Surefire Plugin which includes parameter and specify the tests that we want to include.

Let us now create a simple example using selenium, testNG and execute with the help of Maven.

Step 1: First create a maven project and name it as ‘FirstDemo’.
Step 2: Create a class ‘GoogleHomePageTest.java’
Step 3: Add Tests in ‘GoogleHomePageTest.java’ class.
Step 4: Add TestNg and Selenium Dependencies to maven pom.xml file.
Step 5: Now add maven Surefire Plug-in to pom.xml
Step 6: Execute tests using ‘mvn test’ from command prompt.

The below is the Project structure after creating as above steps.

mavenprojectstructure

package com.google.tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GoogleHomePageTest {

private WebDriver driver;
String appURL = “http://google.com&#8221;;

@BeforeClass
public void testSetUp() {

driver = new FirefoxDriver();
}

@Test
public void verifyGooglePageTittle() {
driver.navigate().to(appURL);
String getTitle = driver.getTitle();
Assert.assertEquals(getTitle, “Google”);
}

@AfterClass
public void tearDown() {
driver.quit();
}

}
We have a test ‘verifyGooglePageTitle()’ in the above class. Below is the testng.xml file which we will include in pom.xml file.

<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”&gt;
<suite name=”Example test run”>
<test name=”Simple Test”>
<classes>
<class name=”com.google.tests.GoogleHomePageTest”/>
</classes>
</test>
</suite>

After adding all the dependencies, we need to add the classes that we want to execute. And below is the pom.xml file looks like.

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>FirstDemo</groupId>
<artifactId>FirstDemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<jre.level>1.7</jre.level>
<jdk.level>1.7</jdk.level>
</properties>

<build>
<plugins>
<!– Compiler plug-in –>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${jdk.level}</source>
<target>${jdk.level}</target>
</configuration>
</plugin>
<!– Below plug-in is used to execute tests –>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<suiteXmlFiles>
<!– TestNG suite XML files –>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
<!– Include the following dependencies –>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.45.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
</dependency>
</dependencies>

</project>
After executing the above program, the report will be generated in your project folder under target\surefire-reports. You can checkout default testng html reports.

Hope the above example works for you. Please let us know if you face any problem.

Advertisements

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