How to run TestNG test cases using Maven

In my previous post i have explained How to run selenium scripts in Firefox browser using GeckoDriver,in this post i am going to show you How to run TestNG test cases using Maven,Maven is a Software Management ,Comprehension tool based on Project Object Model i.e POM,Which is a Apache corporation developed build tool which is useful in Manage project builds,reporting purpose and documentation from a central piece of information.Maven is very easy to configure all your jar files in pom.xml file instead of configure build path in Eclipse for Selenium.

How to run TestNG test cases using Maven
How to run TestNG test cases using Maven

How to do it?

1)Create Maven project in Eclipse,please follow below steps to create.
  • Go to File menu - New-Others
  • Select Maven Project
  • Click on Next button
  • Select Create Simple Project from New Maven Project window
  • Click on Next button 
  • Now Enter Group Id,Artifact Id,Select Version.
  • Click on Finish Button
  • It will take some time to create complete Maven Project

2)After Creating the Maven Project,Now create any package name under src/test/java folder.For example com.mavenpractice.Testcases.

3)Now Create Class under  com.mavenpractice.Testcases and Before Creating any Class please add all your Maven dependencies to pom.xml files as below.

POM.xml Configuration

<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>com.sample.mavenpractice</groupId>
  <artifactId>mavenpractice</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Sample Maven Practice</name>
  <description>Maven Project Set up and practicing</description>
  <properties>
<jre.level>1.7</jre.level>
<jdk.level>1.7</jdk.level>
</properties>
  <dependencies>
  <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.48.2</version>
    </dependency>
    <dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-firefox-driver</artifactId>
<version>2.48.2</version>
</dependency>
    <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8</version>
  <scope>test</scope>
</dependency>
<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
     
<dependency>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>2.19.1</version>
  <type>maven-plugin</type>
</dependency>
</dependencies>
</project>

4)Now Right Click on POM.xml file and Run as-Maven Clean once and again Run as - Maven Install,in this step all the maven dependencies will download into your .m2 path and the default .m2 path is "C:\Users\xxxxx\.m2\repository" here all maven dependencies will download and as well as in Eclipse project all downloaded Maven dependencies will add into your project.You can see screenshots at last.
5)Once Maven clean and install is successful ,Now create Test Cases under com.mavenpractice.Testcases package.
6)I have created new TestNG test case in below script,Please check the same.


TestNG Test Script:



package com.mavenpractice.Testcases;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;


public class Firsttest {
 
 public static WebDriver driver;
 public static String baseUrl;
 
 @Test
 public void setup() throws IOException{
  driver = new FirefoxDriver();
  baseUrl="http://www.wikishown.com";
  driver.manage().window().maximize();
  driver.navigate().to(baseUrl); 
  driver.findElement(By.linkText("Selenium")).click();
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  
  WebElement elem = driver.findElement(By.xpath("//h1[contains(.,'Selenium')]"));
  Actions action = new Actions(driver);
  action.moveToElement(elem).doubleClick().perform();
  screenshot(); 
  
 }
 
 public void screenshot() throws IOException{
  //Take screenshot
  
    File screenshots = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screenshots, new File("E:\\Screenshots\\"+driver.getTitle().toUpperCase()+System.currentTimeMillis()+"-"+".jpg"));
    
  
 }
 
 @AfterTest
 public void teardown(){
  //driver.quit();
  //driver.close();
  
 }

}


Create testng.xml file



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="none">
  <test name="Test">
    <classes>
      <class name="com.mavenpractice.Testcases.Firsttest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->


In order to run the Selenium TestNG test scripts you need to configure testng.xml file in POM.xml file then only it will run properly in maven.

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>com.sample.mavenpractice</groupId>
  <artifactId>mavenpractice</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <name>Sample Maven Practice</name>
  <description>Maven Project Set up and practicing</description>
  <properties>
  <jre.level>1.7</jre.level>
  <jdk.level>1.7</jdk.level>
 </properties>
 
  <build>
      
      <plugins>
          
          <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>
          <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>
          
          
      </plugins>
      
  </build>

  <dependencies>
  <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.48.2</version>
    </dependency> 
    <dependency>
   <groupId>org.seleniumhq.selenium</groupId>
   <artifactId>selenium-firefox-driver</artifactId>
   <version>2.48.2</version>
  </dependency>
    <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8</version>
    <scope>test</scope>
 </dependency>
 <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.3.2</version>
        </dependency>
        
  <dependency>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <type>maven-plugin</type>
  </dependency>
 
 </dependencies>
 
</project>

7)Now Right Click on POM.xml file and Run as - Maven Test,it will run your Selenium Test scripts successfully .

In this way we need to run Selenium WebDriver Testng test scripts in Maven using POM.xml file and testng.xml files.

Output:

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building Sample Maven Practice 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ mavenpractice ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:compile (default-compile) @ mavenpractice ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ mavenpractice ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO] 
[INFO] --- maven-compiler-plugin:3.5.1:testCompile (default-testCompile) @ mavenpractice ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ mavenpractice ---

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
Starting ChromeDriver 2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4) on port 18697
Only local connections are allowed.
Browser is Maximized
Web Url is opened
Driver clicks on Selenium link
Taking Screenshot of screen is completed.
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 13.274 sec - in TestSuite

--------------------------------------------------------------------------------------------------------------




At last Reports will generate in target - Surefire-Reports as below.



In Webbrowser


Please provide your valuable comments and suggestions on this post and explanation.Thank you for reading my blog.

No comments:

Post a Comment