banner



How To Create Testng Maven Project In Eclipse

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>        

To execute tests over multiple machines / virtual machines, we need to include Selenium server into your maven project, you need to add the following dependency to your pom.xml.

             <dependency>         <groupId>org.seleniumhq.selenium</groupId>         <artifactId>selenium-server</artifactId>         <version>3.0.1</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.

NOTE : - If you want to parametrize and choose which TestNG xml suites to run from command line, Please check article on Run TestNG xml suites from command line.

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>        

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

How To Create Testng Maven Project In Eclipse

Source: https://www.seleniumeasy.com/maven-tutorials/how-to-execute-selenium-webdriver-testng-xml-using-maven

Posted by: masseywicis1978.blogspot.com

0 Response to "How To Create Testng Maven Project In Eclipse"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel