TestNG-Maven
Maven
1) Maven is used to define project structure, dependencies, build, and test management.
2) Using pom.xml(Maven) you can configure dependencies needed for building testing and running code.
3) Maven automatically downloads the necessary files from the repository while building the project.
We created a Maven project with next structure:
src/main/java – contains packages with Page Objects. Each package represents separate application and can contain packages with general methods and objects of the framework.
Src/main/resources – here we store config files and other resources that are used in our tests.
Src/test/java – contains packages with tests. Each package contains tests related only to one application or a test flow that goes through the couple of applications.
Steps to use install Maven and use it with TestNG Selenium
Step1) In Eclipse IDE, select Help | Install New Software from Eclipse Main Menu.
Step 2) On the Install dialog, select Work with and m2e plugin as shown in the following screenshot:
Step 3) Click on Next button and finish installation.
Configure Eclipse with Maven
Step 1) In Eclipse IDE, create a new project by selecting File | New | Other from Eclipse menu.
Step 2) On the New dialog, select Maven | Maven Project and click Next
Step 3) On the New Maven Project dialog select the Create a simple project and click next
Note: if you want your project to specific location uncheck the ‘Use Default Workspace location’ and specify the path where you want create.
Step 4) Enter TestOne in Group Id: and Artifact Id: and click finish
Step 5) Eclipse will create TestOne with following structure:
Step 6) Run the pom.xml file for store/load jar files to repository before that we need to update pom.xml file for required jar files
By default pom.xml file is look like this
We need to add required dependencies to pom.xml file,Add the Selenium and TestNG, dependencies to pom.xml in the <project> node:
<projectxmlns="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>TestOne</groupId>
<artifactId>TestOne</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>
</plugins>
</build>
<!-- Include the following dependencies -->
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.48.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
</dependency>
</dependencies>
</project>
Run the pom.xml file, for that Right-click on pom.xml file then click on Run As and click on Maven Install
AfterInstalling you will get “BUILD SUCCESS” message
Step 7) Create a Class MavenExample, Right click on src/test/java
Create a package (mvn.testng)
Then create a class
Java file
package mvn.testng;
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;
publicclass MavenExample {
private WebDriver driver;
String appURL = "http://google.com";
@BeforeClass
publicvoidtestConfig() {
driver = new FirefoxDriver();
}
@Test
publicvoidverifyTittle() {
driver.navigate().to(appURL);
String getTitle = driver.getTitle();
Assert.assertEquals(getTitle, "Google");
}
@AfterClass
publicvoidtearDown() {
driver.quit();
}
}
Step8) Create TestNG file , Right-click on TestOne then Click on TestNG then click on Convert to TestNG
Window looks like
Then click on Finish button
Now structure is like this
Now add maven Surefire Plug-in to pom.xml for run your testng file from pom.xml file
Now entire code is
<projectxmlns="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>TestOne</groupId>
<artifactId>TestOne</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.48.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
</dependency>
</dependencies>
</project>
Now you reached last step to run your test , two way you can run your test
1)from pom.xml.
2) from Main Project
1) Right-click on pom.xml file then click on RunAs then click on Maven test
2) Right-click on your project(here Testone) then click on RunAs then click on Maven test
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.
You want to see the report click on index.html
Comments
Post a Comment