selenium tutorial testing tools

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.

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.
maven Testng Java project

The below is the example program to execute testng.xml file using maven.

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";

	@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">
<suite name="Example test run">
  <test name="Simple Test">
    <classes>
      <class name="com.google.tests.GoogleHomePageTest"/>
    </classes>
  </test>
</suite>

You can add the classes that you want to execute. And below is the pom.xml file.

<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>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.

Build Tools: 

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.