i

Selenium Step By Step Guide

Introduction to TestNG and Annotations

TestNG?

  • TestNG is an automation testing framework based on Java. Junit inspires TestNG (Test Next Generation) as it uses annotations as well.
  • TestNG helps in generating proper reports, which gives a close idea to analyze the failed, passed, and skipped test cases.
  • TestNG helps in executing the failed test cases efficiently. If we have five tests to run and after running the test cases suppose four tests got passed, but one got failed, then by using TestNG, we can run only the failed test case instead of rerunning the entire suite.
  • After running the test, we see a testng-failed.xml, which we can run to execute the only failed test case.
  • We can do parallel testing as well as using TestNG.
  • TestNG has a rich set of annotations to create a reliable testing framework.
  • We can group multiple numbers of test cases in TestNG easily by converting them into a testing.xml file. We can set the priorities of each test case as well.
  • A single test can be run multiple times by using the "invocation count" keyword.
  • The integration of the TestNG framework is effortless with Maven, Jenkins, and other tools.
  • TestNG removes the dependency of having a static main method in the test class.

Test class structure without TestNG:

Test class structure with TestNG:

  • TestNG can generate the results of running class in a readable format, as shown below:

 

TestNG configuration in Project:

There are few things need to take care in order to use the TestNG in any automation project:

  • Add the maven dependency into pom.xml for the TestNG to import the jar files into the project.

     <dependency>

       <groupId>org.testnggroupId>

       <artifactId>testngartifactId>

       <version>6.14.3version>

       <scope>testscope>

     dependency>

  • Need to install the TestNG plugin for eclipse from Eclipse marketplace.

  • Once the plugin is installed we can run the TestNG test suite from eclipse by selecting “Run As> TestNG Test

TestNG Annotations

There are several annotations available in TestNG. Let us discuss some of the most used annotations of the TestNG.

  1. @Test: Method with this annotation runs as the test method.
  2. @BeforeTest: This method runs before the first test method inside the TestNG class.
  3. @AfterTest: This method runs after all the test method belongs to the TestNG class are run.
  4. @BeforeMethod: This method runs before every test method.
  5. @AfterMethod: This method runs after every test method.
  6. @BeforeClass: This annotated method runs before the first method of the class invoked.
  7. @AfterClass: This annotated method runs after all the methods of the class invoked are run.
  8. @BeforeSuite: The annotated method runs before all the tests of the suites have been run.
  9. @AfterSuite: The annotated method runs after all the tests of the suites have been run.
  10. @BeforeGroups: This method is run before the first method of the group invoked.
  11. @AfterGroups: This method is run after all the methods of the group invoked.

Let’s create a simple TestNG code consisting of two test methods with @Test,@BeforeClass, @AfterClass, @BeforeMethod, and @AfterMethod annotations.

package SeleniumTest.SeleniumTest;

import java.io.File;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.Date;

 

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.Assert;

import org.testng.ITestResult;

import org.testng.annotations.AfterClass;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeClass;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.Test;

import com.relevantcodes.extentreports.DisplayOrder;

import com.relevantcodes.extentreports.ExtentReports;

import com.relevantcodes.extentreports.ExtentTest;

import com.relevantcodes.extentreports.LogStatus;

 

public class TestNGTest {

     

    ExtentReports extent;

      ExtentTest logger;

      WebDriver driver ;

     

      @BeforeClass

      public void startReport()

      {

            extent=new ExtentReports(System.getProperty("user.dir")+"/test-output/ExtentReport.html",null, DisplayOrder.NEWEST_FIRST);

            extent.addSystemInfo("Host Name","Software Testing blog")

                  .addSystemInfo("Environment","Test Envt")

                  .addSystemInfo("User Name","Kamal Gairola");

            extent.loadConfig(new File(System.getProperty("user.dir")+"/extent-config.xml"));

           

      }

      @BeforeMethod

      public void startDriver()

      {

        driver=new ChromeDriver();

        this.driver.get("https://selflearning.io/");

           

      }

  @Test

  public void passTest() {

       

        logger=extent.startTest("passTest");

        Assert.assertTrue(true);

        logger.log(LogStatus.PASS, "passTest is passed");

       

  }

 

  @Test

 

  public void failTest()

  {

        logger=extent.startTest("failTest");

        Assert.assertTrue(false);

        logger.log(LogStatus.PASS, "failTest is passed");

  }

  @AfterMethod

  public void TestResult(ITestResult result) throws IOException

  {

        if(result.getStatus()==ITestResult.FAILURE)

        {

              logger.log(LogStatus.FAIL, "Test case"+result.getName());

              logger.log(LogStatus.FAIL, "Test case failed is "+result.getThrowable());

              String scrPath=CaptureScreenshot.getScreenshot(driver,result.getName());

              logger.log(LogStatus.FAIL,logger.addScreenCapture(scrPath));

              extent.endTest(logger);

        }

  }

 

  @AfterClass

  public void completeTest()

  {

       

        extent.flush();

        SendingMail.sendMail(System.getProperty("user.dir")+"/test-output/ExtentReport.html");

  }

}

 

We can run this test by selecting the “Run As> TestNG Test”. We can also generate the testing.xml from the TestNG suite. Follow below steps to generate the testing.xml:

  1. Select the TestNG class and right click on the same. Below option grid opens:

  1. Select “Convert to TestNG” and click on the finish button.

  1. “testing.xml” file gets created in the Project directory.