What is TestNG in Selenium Webdriver?

Home/Selenium/What is TestNG in Selenium Webdriver?

What is TestNG in Selenium Webdriver?

TestNG is a test framework designed to cover all test categories: unit, functionality, end-to-end, integration, etc…

We can combine TestNG with Selenium and, through Java, we can write test cases in Eclipse.

Let’s start with TestNG!

Here is a small tutorial who sends quality control test samples to Selenium TestNG and calls Ant’s TestNG collection. You can use any programming language supported by Selenium 2. I used Java in the tutorial below.

Install this software before starting

(if you do not have one):

  1. JDK
  2. Eclipse
  3. TestNG: download TestNG from the Eclipse help menu.
  4. Download the Selenium server from http://docs.seleniumhq.org/download/

Follow-up steps:

Step 1: Use Eclipse. Create a new Java project.

Step 2: Add a Selenium server to a jar file as an external jar file in your project. To do this, right click on any file/folder in the project in Eclipse. Select Generate compilation -> Configure compile compilation. Select the “Libraries” tab. Click on “Add external socket”. Select the Selenium file that you have already downloaded.

Step 3: Use the TestNG library in the project: right-click on any file/folder in the project in Eclipse. Select Generate compilation -> Configure Compilation. Select the “Libraries” tab. Click on “Add library”. Select TestNG. Click on “OK”.

Step 4: TestNG Testsuite contains numerous tests. The TestNG “test” comprises one or more classes, and the TestNG class consists of several “test methods”.

Become a Selenium Expert with Certification in 25Hours

Add a new Java class “TestClass1” to the project. Below is a set of selenium test cases written as the TestNG method. In the code below, 4 test cases are added for demonstration purposes. Add the following code to the Java class.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterClass;
public class TestClass1 {
protected static WebDriver wd;
protected static String result;
@BeforeClass
public static void setup()  {
wd = new FirefoxDriver();
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
}
@Test
void Testcase1() {
wd.get("http://www.floraindia.com");
wd.findElement(By.id("kwsch")).sendKeys("Red");
wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();
result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();
Assert.assertEquals(result, "115");
}
@Test
void Testcase2() {
wd.get("http://www.floraindia.com");
wd.findElement(By.id("kwsch")).sendKeys("Blue");
wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();
result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();
Assert.assertEquals(result, "13");
}
@Test
void Testcase3() {
wd.get("http://www.floraindia.com");
wd.findElement(By.id("kwsch")).sendKeys("Yellow");
wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();
result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();
Assert.assertEquals(result, "27");
}
@Test
void Testcase4() {
wd.get("http://www.floraindia.com");
wd.findElement(By.id("kwsch")).sendKeys("Purple");
wd.findElement(By.xpath("//input[@src='images/go.gif']")).click();
result = wd.findElement(By.xpath("//font[text()='Total Items  :']//following::td[1]")).getText();
Assert.assertEquals(result, "10");
}
@AfterClass
public static void teardown()  {
wd.close();
wd.quit();
}
}

Step 5: Add TestNG test scores to your project as “TestNG.xml”

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="My sample Suite">
<test name="Search test">
<classes>
<class name="TestClass1" />
</classes>
</test>
</suite>

Step 6: Run the test point by right-clicking on TestNG.xml and select Run as -> TestNG suite. The test points are executed and a summary of the execution of the test is shown in the Eclipse console. TestNG automatically creates a test report. To view the report, go to the project directory and select the test results folder. Open idex.html. As shown in the report below, tests 1, 2 and 3 are passed. The test strip 4 failed.

Step 7: The TestNG suite was successfully implemented! Now we want to use Ant to start the TestNG test instead of the eclipses. We need to determine the location of the Selenium server and the Shrimp TestNG Ant. Create a new folder in the project and give the files a jar.

  1. Create a “lib” folder in your Java project.
  2. Copy the Selenium server to this “lib” folder.
  3. Also, copy testng.jar in the same folder. In the Eclipse directory, go to the add-on directory. There will be a directory, such as org.testng.eclipse_6.8.6.20130914_0724. In this directory, you will have testng.jar in the lib folder.

Become a Selenium Certified Expert in 25Hours

Step 8: Add an Ant script called build.xml to your project as follows.

 <?xml version="1.0" encoding="UTF-8"?>
 <project name="automation" default="main" basedir=".">
 <!--Define Path-->
 <path id="project-classpath">
    <fileset dir="./lib" includes="*.jar"/>
    <pathelement location="bin"/>
 </path>
            <target name="main" depends = "init, compile, run">
            </target>
 <!-- Initialization -->
            <target name="init">
                        <echo>starting init task</echo>
                        <delete dir="bin"/>
                        <mkdir dir="bin"/>
            </target>
 <!--Compile-->
            <target name="compile">
                        <javac srcdir="src" destdir="bin">
                                    <classpath>                        
                                        <fileset dir="./lib" includes="*.jar"/>
                                        <pathelement location="bin"/>                            
                                    </classpath>
                        </javac>
            </target>
 <!--Define TestNG task-->
 <taskdef name="testng" classname="org.testng.TestNGAntTask">
               <classpath>
                        <pathelement location="./lib/testng.jar"/>
               </classpath>
              </taskdef>
 <!--Execute TestNG testsuite -->
            <target name ="run">
               <testng outputdir="./test-output" classpathref="project-classpath"  useDefaultListeners="true">
       <xmlfileset dir="." includes="src/TestNG.xml"/>
               <!-- generate report title -->
                <sysproperty key="org.uncommons.reportng.title" value="Test Automation"/>
                <sysproperty key="org.uncommons.reportng.escape-output" value="false"/>
               </testng>
            </target>
 </project>

Step 9: Finish the Ant file. Go to the command line and go to the project directory. Enter the ant. In your project, you will start to run the ant build.xml script. Once the ant script has been executed successfully, see the report created by TestNG in the same location, that is, Files index.html in the test-output directory of your project.

Get Selenium online Training

Some of the features of the TestNG program are listed below

1 – Annotation: admits several notes at different levels, so we will discuss it in a separate workplace.

2- Support for data-based tests (with @DataProvider)

3- Support for the parameters.

4- Create automatic reports.

5- We cannot run a test case without success only with testng.xml if, in the case of a failure, it is not necessary to run the entire test package

6- Compatible with several tools and accessories (Eclipse, IDEA, Maven, etc.).

7- Default JDK functions for the time of implementation and registration (without dependency).

8- Dependent methods to test the application server.

To getting expert-level training for Data Science Training in your location –Selenium Training in Tambaram | Selenium Training in Chennai |  Selenium Training in Bangalore | Selenium Training in Pune

 

By | 2018-12-21T19:01:39+00:00 December 21st, 2018|Selenium|0 Comments

About the Author:

Leave A Comment