Quick Contact

    What is TestNG?

    TestNG is an open source automated testing framework; where NG of TestNG
    means Next Generation. TestNG is similar to JUnit (a unit testing framework for Java programming language) but it is much more powerful than JUnit but still it’s inspired by JUnit. It is designed to be better than JUnit, especially when testing integrated classes. Cedric Beust is the creator of TestNG.

    TestNG eliminates most of the limitations of the older framework and gives the developer the ability to write more flexible and powerful tests with help of easy annotations, grouping, sequencing & parameterizing.

    Features of TestNG

    The features of TestNG are:

    • Supports annotations.
    • TestNG uses more Java and object oriented features.
    • Supports testing integrated classes
    • Separates compile-time test code from run-time configuration/data info.
    • Flexible runtime configuration.
    • Supports Dependent test methods, parallel testing, load testing, and partial failure.
    • Flexible plug-in API.
    • Support for multi-threaded testing.
    Benefits of TestNG

    There are number of benefits but from Selenium perspective, major advantages of TestNG are :

      • Simplified annotations: Annotations have been simplified, making it easier for testers to understand them.
      • Results in HTML: It generates reports in HTML.
      • Multiple test-cases at once: Using TestNG Suite (testing.xml), which is essentially an xml file, testers can execute a number of test cases at a time. In this xml file, users need to mention the number of classes and test-cases they wish to execute.
      • Runs failed test-cases: TestNG can also be used to run failed test-cases. This is one of most important advantages of TestNG over JUnit.
      • Allows grouping: TestNG allows supports grouping. Using this feature, testers can group test-cases without too much effort, which was not possible with JUnit.
      • Allows running on multiple browsers: TestNG allows testers to execute and run one script in multiple browsers.
      • Parametric testing: Most of the time, testers have to execute a large number of varied tests, mainly due to the nature of their business logic. However, this lengthy process can be eliminated as parametric testing allows to run the same test a number of times by simply

    changing the values. It allows to pass parameters to the test methods in two ways: parameter and @dataprovider.

    • Bypassing or ignoring test-cases: This feature is useful if a particular testcase(s)is not execute. In such instances, TestNG, with the help of annotation @Test(enabled = false), allows to disable or bypass the particular test-case(s).
    • Reporter class (it generates logs): In TestNG, with the help of the reporter class, users can log messages for the test. This could be just surface or in-depth information, depending on the need
    • Expected exceptions: It allows to trace the exception handling of the code. While writing a code, there may be situations where testers want to verify if an exception is being presented when executed. This method will give the details of the exceptions that are expected to be presented by that particular method. Use this method with @Test annotation.
    • Dependent on Method : TestNG supports the ‘dependence’ method. It can have dependence as an attribute of a method, for example, if one method is dependent on another. This is not available in JUnit.
    • TestCase priority: The test cases can be execute in a particular order. To accomplish this, define the order in @Test Annotation.
    TestNG Installation in Eclipse

    The steps to Download and install TestNG on eclipse are:

    1. Launch eclipse IDE -> Click on the Help option within the menu -> Select “Eclipse Marketplace..” option within the dropdown.
    2. Enter the keyword “TestNG” in the search textbox and click on “Go” button as shown below.
    3. Click on the “Go” button, the results matching to the search string would be displayed. Now click on the Install button to install TestNG.
    4. Click on the Install button, you will get prompt with a window to confirm the installation. Click on “Confirm” button.
    5. In the next step, the application would prompt you to accept the license and then click on the “Finish” button.
    6. The installation is initiated now and the progress can be seen as follows:
    7. Restart eclipse so as to reflect the changes made.
    8. Upon restart, verify the TestNG installation by navigating to “Preferences” from “Window” option in the menu bar. Refer the following figure for the same.
    Creation of Sample TestNG project
    1. Click on the File option within the menu -> Click on New -> Select Java Project.
    2. Enter the project name as “DemoTestNG” and click on “Next” button. As a concluding step,
      click on the “Finish” button and your Java project is ready.
    3. The next step is to configure the TestNG library into the newly created Java project. For the same, click on the “Libraries” tab under Configure Build Path. Click on “Add library” as shown below.
    4. Select TestNG and click on the “Next” button as shown below in the image. In the end, click
      on the “Finish” button.
    5. The TestNG is now added to the Java project and the required libraries can be seen in the package explorer upon expanding the project.
    Creating TestNG class
    1. Expand the “DemoTestNG” project and traverse to “src” folder. Right-click on the “src” package and navigate to New -> Other.
    2. Expand TestNG option and select “TestNG” class option and click on the “Next” button.
    3. Furnish the required details as following. Specify the Source folder, package name and
      the TestNG class name and click on the Finish button.
    4. The above mentioned TestNG class would be created with the default schema.
    Test Scenario

    Consider a test scenario where we:

    • Launch the browser and open “gmail.com”.
    • Verify the title of the page and print the verification result.
    • Enter the username and Password.
    • Click on the Sign in button.
    • Close the web browser.
    Code:
    package TestNG;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver; 
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.Assert;
    import org.testng.annotations.Test; 
    public class DemoTestNG {
    public WebDriver driver = new FirefoxDriver();
    
    String appUrl = "https://accounts.google.com";
    
    @Test
    
    public void gmailLogin() {
    
    // launch the firefox browser and open the application url driver.get("https://gmail.com");
     
    // maximize the browser window driver.manage().window().maximize();
    
    // declare and initialize the variable to store the expected
    
    //title //of the webpage.
    
    String expectedTitle = " Sign in - Google Accounts "
    
    // fetch the title of the web page and save it into a string
    //variable
    
    String actualTitle = driver.getTitle(); 
    Assert.assertEquals(expectedTitle,actualTitle);
    // enter a valid username in the email textbox
    
    WebElement username=driver.findElement(By.id("Email")
    );
    
    username.clear();
    
    username.sendKeys("TestSelenium");
    
    // enter a valid password in the password textbox
    
    WebElement password = driver.findElement(By.id("Passwd"));
    
    password.clear();
    
    password.sendKeys("password123");
    
    // click on the Sign in button
    
    WebElement SignInButton = driver.findElement(By.id("signIn"));
    
    SignInButton.click();
    
    // close the web browser
    
    driver.close();
    
    }
    
    }
    
    Code Explanation with respect to TestNG

    @Test: @Test is one of the TestNG annotations. This annotation lets the program execution to know that method annotated as @Test is a test method. To be able to use different TestNG annotations, we need to import the package “import org.testng.annotations.*”.

    There is no need of main() method while creating test scripts using TestNG. The program execution is done on the basis of annotations.

    In a statement, we used Assert class while comparing expected and the actual value. Assert class is used to perform various verifications. To be able to use different assertions, we are required to
    import “import org.testng.Assert”.

    Executing the TestNG script
    1. Right click anywhere inside the class within the editor or the java class within the package explorer, select “Run As” option and click on the “TestNG Test”.

    TestNG result is displayed into two windows:

    • Console Window
    • TestNG Result Window

    Refer the below screencasts for the result windows:

    HTML Reports

    TestNG comes with a great capability of generating user readable and comprehensible HTML reports for the test executions. These reports can be viewed in any of the browsers and it can also be viewed using Eclipse’s build –in browser support.

    To generate the HTML report, follow the below steps:

    1. Execute the newly created TestNG class. Refresh the project containing the TestNG class by right-clicking on it and selecting “Refresh” option.
    2. A folder named as “test-output” shall be generated in the project at the “src” folder
      level. Expand the “test-output” folder and open on the “emailable-report.html” file with the Eclipse browser. The HTML file displays the result of the recent execution.
    3. The HTML report shall be opened within the eclipse environment. Refer the below
      image for the same.
    Annotations in TestNG

    Annotations were formally added to the Java language in JDK 5, and TestNG made the choice to use annotations to annotate test classes.

    Here is the list of annotations that TestNG supports

    Annotation & Description
    @BeforeSuite

    The annotated method will be run only once before all tests in this suite have run.

    @AfterSuite

    The annotated method will be run only once after all tests in this suite have run.

    @BeforeClass

    The annotated method will be run only once before the first test method in the current class is invoked.

    @AfterClass

    The annotated method will be run only once after all the test methods in the current class have run.

    @BeforeTest

    The annotated method will be run before any test method belonging to the classes inside the
    <test> tag is run.

    @AfterTest

    The annotated method will be run after all the test methods belonging to the classes inside the
    <test> tag have run.

    @BeforeGroups

    The list of groups that this configuration method will run before. This method is guaranteed to run shortly before the first test method that belongs to any of these groups is invoked.

    @AfterGroups

    The list of groups that this configuration method will run after. This method is guaranteed to run shortly after the last test method that belongs to any of these groups is invoked.

    @BeforeMethod

    The annotated method will be run before each test method.

    @AfterMethod

    The annotated method will be run after each test method.

    @DataProvider

    Marks a method as supplying data for a test method. The annotated method must return an Object[ ][ ], where each Object[ ] can be assigned the parameter list of the test method. The @Test method that wants to receive data from this DataProvider needs to use a dataProvider name equals to the name of this annotation.

    @Factory

    Marks a method as a factory that returns objects that will be used by TestNG as Test classes. The method must return Object[ ].

    @Listeners

    Defines listeners on a test class.

    @Parameters

    Describes how to pass parameters to a @Test method.

    @Test

    Marks a class or a method as a part of the test.

    Following are some of the benefits of using annotations

    • TestNG identifies the methods it is interested in, by looking up annotations. Hence, method names are not restricted to any pattern or format.
    • Additional parameters can be pass to annotations.
    • Annotations are strongly typed, so the compiler will flag any mistakes right away.
    • Test classes no longer need to extend anything (such as TestCase, for JUnit 3).
    How to Run Test Suite in TestNg

    A test suite is a collection of test cases intended to test a behavior or a set of behaviors of software program. In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file, as suite is the feature of execution. It also allows flexible configuration of the tests to be run. A suite can contain one or more tests and is defined by the <suite> tag. <suite> is the root tag of your testng.xml. It describes a test suite, which in turn is made of several <test> sections.

    The following table lists all the legal attributes that <suite> accepts.

    Attribute & Description
    name

    The name of this suite. It is a mandatory attribute.

    verbose

    The level or verbosity for this run.

    parallel

    Whether TestNG should run different threads to run this suite.

    thread-count

    The number of threads to use, if parallel mode is enabled (ignored other-wise).

    annotations

    The type of annotations you are using in your tests.

    time-out

    The default timeout that will be used on all the test methods found in this test.

    Let’s take an example having two test classes, Test1 & Test2, to run together using Test Suite.

    Create a Class

    Create a java class to be tested, say, MessageUtil.java in C:\>JUNIT_WORKSPACE.

    /*
    *	This class prints the given message on console.
    */
    public class MessageUtil { private String message;
    
    // Constructor
    
    // @param message to be printed 
    public MessageUtil(String message) {
    this.message = message;
    
    }
    
    // prints the message
    public String printMessage() { 
    System.out.println(message);
    
    return message;
    }
    
    
    // add "Hi!" to the message
    public String salutationMessage() {
    message = "Hi!" + message; 
    System.out.println(message); 
    return message;
    }
    }
    
    Create Test Case Classes
    • Create a java class file named Test1.java in C:\>TestNG_WORKSPACE.
      import org.testng.Assert;
      import org.testng.annotations.Test;
      
      public class Test1 {
      String message = "Manisha";
      MessageUtil messageUtil = new MessageUtil(message);
      
      @Test
      public void testPrintMessage() {
      System.out.println("Inside testPrintMessage()"); 
      Assert.assertEquals(message,
      messageUtil.printMessage());
      }
      }
      
    • Create a java class file named Test2.java in C:\>TestNG_WORKSPACE.
      import org.testng.Assert;
      import org.testng.annotations.Test;
      
      public class Test2 {
      String message = "Manisha";
      MessageUtil messageUtil = new MessageUtil(message);
      
      @Test
      public void testSalutationMessage() { 
      System.out.println("Inside testSalutationMessage()"); 
      message = "Hi!" + "Manisha";
      
      Assert.assertEquals(message,messageUtil.salutationMessage());
      }
      }
      
    • Now, let’s write the testng.xml in C:\>TestNG_WORKSPACE, which would contain the
      <suite> tag as follows

      <?xml version = "1.0" encoding = "UTF-8"?>
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      
      <suite name = "Suite1">
      
      <test name = "exampletest1">
      <classes>
      <class name = "Test1" />
      </classes>
      </test>
      
      <test name = "exampletest2">
      <classes>
      <class name = "Test2" />
      </classes>
      </test>
      
      </suite>
      
    • uite1 includes exampletest1 and exampletest2. Compile all java classes using javac.
      C:\TestNG_WORKSPACE>javac MessageUtil.java Test1.java Test2.java
    • Now, run the testng.xml, which will run the test case defined in the provided Test Case class.
      C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml

    Output

    Inside testPrintMessage() 
    	Manisha
    Inside testSalutationMessage() 
    Hi!Manisha
    
    ===============================================
    Suite1
    Total tests run: 2, Failures: 0, Skips: 0
    ===============================================
     
    

    The test-output folder can also be check. Under the Suite1 folder, two html files can be seen which are created, exampletest1.html and exampletest2.html, which would look as follows

    Groups in TestNG

    Group test is a new innovative feature in TestNG, which doesn’t exist in JUnit framework. It permits to dispatch methods into proper portions and perform sophisticated groupings of test methods.
    Group tests provide maximum flexibility in how the tests are partition, and doesn’t require to recompile anything if it want to run two different sets of tests back to back.

    Groups are specified in the testng.xml file using the <groups> tag. It can be found either under the
    <test> or <suite> tag. Groups specified in the <suite> tag apply to all the <test> tags underneath. Now, let’s take an example to see how group test works.

    Create a Class
    • Create a java class to be tested, say, MessageUtil.java in C:\> TestNG_WORKSPACE.
      /*
      *	This class prints the given message on console.
      */
      public class MessageUtil { private String message;
      
      // Constructor
      // @param message to be printed public MessageUtil(String message) {
      this.message = message;
      }
       
      
      // prints the message
      public String printMessage() { System.out.println(message); return message;
      }
      
      // add "tutorialspoint" to the message public String salutationMessage() {
      message = "tutorialspoint" + message; System.out.println(message);
      return message;
      }
      
      // add "www." to the message public String exitMessage() {
      message = "www." + message; System.out.println(message); return message;
      }
      }
      
    • Create Test Case Class, GroupTestExample.java.
    • Add test methods, testPrintMessage() and
      testSalutationMessage(), to your test class.
    • Group the test method in two categories
      • Check-in tests (checkintest): These tests should be run before you submit new code. They should typically be fast and just make sure no basic functionality is broken.
      • Functional tests (functest): These tests should cover all the functionalities of your software and be run at least once a day, although ideally you would want to run them continuously.
    • Create the java class file
      named GroupTestExample.java in C:
              	\>TestNG_WORKSPACE. 
      import org.testng.Assert;
      import org.testng.annotations.Test;
      
      public class GroupTestExample { 
      String message = ".com";
      MessageUtil messageUtil = new MessageUtil(message);
      
      @Test(groups = { "functest", "checkintest" })
      
      public void testPrintMessage() { 
      System.out.println("Inside testPrintMessage()"); 
      message = ".com";
      Assert.assertEquals(message, messageUtil.printMessage());
      }
      
      @Test(groups = { "checkintest" })
      
      public void testSalutationMessage() { 
      System.out.println("Inside testSalutationMessage()"); 
      message = "tutorialspoint" + ".com"; 
      Assert.assertEquals(message,
      messageUtil.salutationMessage());
      }
      
      @Test(groups = { "functest" })
      
      public void testingExitMessage() { 
      System.out.println("Inside testExitMessage()"); 
      message = "www." + "tutorialspoint"+".com";
      Assert.assertEquals(message, messageUtil.exitMessage());
      
      }
      }
      
    • Create testng.xml

      Create testng.xml in C:\> TestNG_WORKSPACE, to execute test case(s). Here, we would be executing only those tests that belong to the group functest.

      <?xml version = "1.0" encoding = "UTF-8"?>
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      
      
      <suite name = "Suite1">
      <test name = "test1">
      
      
      <groups>
      <run>
      <include name = "functest" />
      </run>
      </groups>
      
      
      <classes>
      <class name = "GroupTestExample" />
      </classes>
      
      
      </test>
      </suite>
      
    • Compile the MessageUtil, Test case classes using javac.
      C:\TestNG_WORKSPACE>javac MessageUtil.java GroupTestExample.java
    • Now, run the testng.xml, which will run only the method testPrintMessage(), as it belongs to the group functest.
      C:\TestNG_WORKSPACE>java -cp "C:\TestNG_WORKSPACE" org.testng.TestNG testng.xml
    • Verify the output. Only the method testPrintMessage() is executed.
      Inside testPrintMessage()
      .com
      Inside testExitMessage() www..com
      
      ===============================================
      Suite1
       
      
      Total tests run: 2, Failures: 1, Skips: 0
      =======================================
      
    Depend-on in TestNG

    Sometimes, you may need to invoke methods in a Test case in a particular order or you want to share some data and state between methods. This kind of dependency is supported by TestNG as it supports the declaration of explicit dependencies between test methods.

    TestNG allows you to specify dependencies either with:

    Using attributes dependsOnMethods in @Test annotations OR 
    Using attributes dependsOnGroups in @Test annotations.
    
    Take a look over the below example:
    
    import org.testng.annotations.Test; 
    public class Dependent {
    @Test (dependsOnMethods = { "OpenBrowser" }) 
    public void SignIn() {
    System.out.println("This will execute second (SignIn)");
    }
    @Test
    public void OpenBrowser() {
    System.out.println("This will execute first (Open Browser)");
    }
    @Test (dependsOnMethods = { "SignIn" }) 
    public void LogOut() {
    System.out.println("This will execute third (Log Out)");
    }
    

    The output will be like this:

    Test Case Sequencing in TestNG

    After building & validating the testing models several test cases are generated. The next biggest task is to decide the priority for executing them by using some systematic procedure.

    The process begins with identification of “Static Test Cases” and “Dynamic Test Runs”, brief introduction of which is as under.

    Test case: It is a collection of several items and corresponding information, which enables a test to be executed or performing a test run.

    Test Run: It is a dynamic part of the specific testing activities in the overall sequence of testing on some specific testing object.

    Every time we invoke a static test case, we in-turn perform an individual dynamic test run. Hence we can say that, every test case can correspond to several test runs.

    Why & how do we prioritize?

    Out of a large cluster of test cases in our hand, we need to scientifically decide their priorities of execution based upon some rational, non-arbitrary, criteria. We carry out the prioritization activity with an objective to reduce the overall number of test cases in the total testing feat.

    If test priority is not defined while, running multiple test cases, TestNG assigns all @Test a priority as zero(0).

    Example:

    We have assigned the Priority to each test case means test case will the lower priority value will be executed first.

    Priority in testNG in action
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.testng.Assert;
    import org.testng.annotations.Test;
    
    public class Priority_In_testNG { 
    WebDriver driver;
    
    // Method 1: Open Browser say Firefox 
    @Test (priority=1)
    public void openBrowser() { driver = new FirefoxDriver();
    }
    
    
    // Method 2: Launch Google.com 
    @Test (priority=2)
    public void launchGoogle() { 
    driver.get("http://www.google.co.in");
    
    }
    
    
    // Method 3: Perform a search using "Facebook" 
    @Test (priority=3)
    public void peformSeachAndClick1stLink() {
    
    driver.findElement(By.xpath(".//*[@title='Search']")).sendKeys("Face book");
    }
    
    
    // Method 4: Verify Google search page title. 
    @Test (priority=4)
    public void FaceBookPageTitleVerification() throws Exception {
    
    driver.findElement(By.xpath(".//*[@value='Search']")).click();
    
    Thread.sleep(3000); 
    Assert.assertEquals(driver.getTitle().contains("Facebook -
    Google Search"), true);
    
    }
    }
    
    Code Explanation

    After assigning priority to each testcases, run the above code using testNG as shown in Video-2 mentioned below.

    Here, you can see that test cases are prioritized. Test case having lower priority are executed first i.e. now there is a sequential execution according to priority in the test cases. Hence, all test cases are passing now.

    Output

    PASSED: openBrowser 
    	PASSED: launchGoogle
    PASSED: peformSearchAndClick1stLink 
    PASSED: FaceBookPageTitleVerification
     
    
    TestNG Asserts

    Asserts helps to verify the conditions of the test and decide whether test has failed or passed. A test is considered successful ONLY if it is completed without throwing any exception.

    Here it is verifying if the page title is equal to ‘Google’ or not. If the page title is not matching with the text / title that we provided, it will fail the test case.

    To explain what assertion is, lets us look into below code sample

    @Test
    public void testCaseVerifyHomePage() { 
    driver= new FirefoxDriver();
    driver.navigate().to("http://google.com"); 
    Assert.assertEquals("Google", driver.getTitle());
    }
    

    First create a Firefox driver, next navigate to the Google page and then the third line here is called Assertion.

    It can also be written as below Assert.assertEquals(“Goooogle”, driver.getTitle(), “Title not matching”);

    TestNG supports assertion of a test using the Assert class and Assertion plays an important role when testing an application.

    The page title will change in the below code and see the result after executing it.

    @Test
    public void testCaseVerifyHomePage() { 
    driver= new FirefoxDriver();
    
    driver.navigate().to("http://google.com"); 
    Assert.assertEquals("Gooooogle", driver.getTitle());
    }
    

    The above code will throw you an Assertion error as below: java.lang.AssertionError: expected [Google] but found [Gooooogle]

    Like wise there are many Assertions provided by the TestNG. The below are the few which are used commonly.

    • assertEqual(String actual,String expected): It takes two string arguments and checks whether both are equal, if not it will fail the test.
    • assertEqual(String actual,String expected, String message): It takes three string arguments and checks whether both are equal, if not it will fail the test and throws the message which we provide.
    • assertEquals(boolean actual,boolean expected): It takes two boolean arguments and checks whether both are equal, if not it will fail the test.
    • assertEquals(java.util.Collection actual, java.util.Collection expected, java.lang.String message): Takes two collection objects and verifies both collections contain the same elements and with the same order. if not it will fail the test with the given message.
    • assert.assertTrue(condition): It takes one boolean arguments and checks that a condition is true, If it isn’t, an AssertionError is thrown.
    • assert.assertTrue(condition, message): It takes one boolean argument and String message. It asserts that a condition is true. If it isn’t, an AssertionError, with the given message, is thrown.
    • assert.assertFalse(condition): It takes one boolean arguments and checks that a condition is false, If it isn’t, an AssertionError is thrown.
    • assert.assertFalse(condition, message): It takes one boolean argument and String message. It Asserts that a condition is false. If it isn’t, an AssertionError, with the given message, is thrown.
    TestNG Parameters

    Everybody knows the importance of Parameterization in testing and in automation testing. It allows to automatically run a test case multiple times with different input and validation values. As Selenium Webdriver is more an automated testing framework than a ready-to-use tool, it will have to put in some effort to support data driven testing in your automated tests. It usually prefer to use Microsoft Excel as the format for storing my parameters but so many of my followers have requested to write an article on TestNG Data Provider.

    TestNG again gives us another interesting feature called TestNG Parameters. TestNG usually pass parameters directly to the test methods with the testng.xml.

    Let me take a very simple example of LogIn application, where the username and password is required to clear the authentication.

      1. Create a test on my demo OnlineStore application to perform LogIn which takes the two string argument as username & password.
      2. Provide Username & Password as parameter using TestNG Annotation.
    import java.util.concurrent.TimeUnit; 
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.testng.annotations.Test;
    import org.testng.annotations.Parameters; 
    public class TestngParameters {
    private static WebDriver driver; 
    @Parameters({ "sUsername", "sPassword" })
    public void test(String sUsername, String sPassword) { 
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.store.demoqa.com"); 
    driver.findElement(By.xpath(".//*[@id='account']/a")).click();
    driver.findElement(By.id("log")).sendKeys(sUsername); 
    driver.findElement(By.id("pwd")).sendKeys(sPassword); 
    driver.findElement(By.id("login")).click();
    
    driver.findElement(By.xpath(".//*[@id='account_logout']/a")).click()
    ;
    driver.quit();
     
    }
    }
    
      1. The parameter would be passed values from testng.xml which will see in the next step.
    <suite name="Suite">
    
    <test name="ToolsQA">
    
    <parameter name="sUsername" value="testuser_1"/>
    
    <parameter name="sPassword" value="Test@123"/>
    
    <classes>
    
    <class name="automationFramework.TestngParameters" />
    
    </classes>
    
    </test>
    
    </suite>
    
    1. Now, run the testng.xml, which will run the parameterTest method. TestNG will try to find a parameter named sUsername & sPassword.
    Skipping Test Cases

    Let’s see how to skip TestNG test deliberately. Sometimes we may face a situation where our test cases might not be ready and it need to skip those tests from running. One way of skipping a test method is by using throw new SkipException() exception.

    Scenario 1: Skip TestNG Test, if a condition met else continue execution.

    Let see a sample WebDriver test case example where SkipException()is placed inside if condition to Intentionally skip that test. Once SkipException() thrown, remaining part of that test method will not be executed and control will goes directly to next test method execution.

    import org.testng.annotations.Test; 
    import org.testng.SkipException; 
    public class SkipTestCase {
    public void  aSkipTest(){ 
    String condition ="Skip Test";
    if(condition.equals("Skip Test")){
    
    //	throw new SkipException("Skipping - This is not ready for testing ");
    
    }else{
    System.out.println("I am in else condition");
    }
    System.out.println("I am out of the if else condition");
    }
    public void  nonSkipTest(){ System.out.println("No need to skip this test");
    }
    }
    

    Output:

    [TestNG] Running:
    
    
    I am out of the if else condition No need to skip this test
     
    
    ===============================================
    Default suite
    Total tests run: 2, Failures: 0, Skips: 0
    ===============================================
    

    Scenario 2: On uncomment the “throw new SkipException()” in the if condition.

    import org.testng.annotations.Test; 
    import org.testng.SkipException;
    
    public class SkipTestCase { 
    public void aSkipTest(){
    String a ="Skip Test"; 
    if(a.equals("Skip Test")){
    throw new SkipException("Skipping - This is not ready for testing ");
     
    
    }else{
    System.out.println("I am in else condition");
    }
    System.out.println("I am out of the if else condition");
    }
    
    public void  nonSkipTest(){ 
    System.out.println("No need to skip this test");
    }
    
    }
    

    Output

    [TestNG] Running:
    
    
    No need to skip this test PASSED: nonSkipTest SKIPPED: aSkipTest
    
    ===============================================
    Default suite
    Total tests run: 2, Failures: 0, Skips: 1
    ===============================================
    

    Skip exception thrown and the remaining part of the first test method “aSkipTest” not executed and control reached to second test method “nonSkipTest” and printed the value as “No need to skip this test”.

    Multi Browser Testing in TestNG

    In every project it is required to perform multi-browser testing to make sure that the functionality is working as expected with every browser to give equal user experience to all of the wide range of audience. It takes a considerable time to test everything on every browser, when used automation to reduce the testing efforts then do the multi-browser testing using automation. TestNG gives us functionality to perform same test on different browsers in a simple and easy way.

      • Create your Script to test a LogIn application using TestNG class.
      • Pass ‘Browser Type’ as parameters using TestNG annotations to the before method of the TestNG class. This method will launch only the browser, which will be provided as parameter.
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver; 
    import org.openqa.selenium.ie.InternetExplorerDriver; 
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass; 
    import org.testng.annotations.Parameters; 
    import org.testng.annotations.Test;
    public class MultiBrowser { public WebDriver driver;
    @Parameters("browser") @BeforeClass
    // Passing Browser parameter from TestNG xml public void beforeTest(String browser) {
    // If the browser is Firefox, then do this if(browser.equalsIgnoreCase("firefox")) {
    driver = new FirefoxDriver();
    
    // If browser is IE, then do this
    
    }else if (browser.equalsIgnoreCase("ie")) {
    
    // Here I am setting up the path for my IEDriver System.setProperty("webdriver.ie.driver",
    "D:\ToolsQA\OnlineStore\drivers\IEDriverServer.exe"); 
    driver = new InternetExplorerDriver();
    }
    
    // Doesn't the browser type, lauch the Website 
    driver.get("http://www.store.demoqa.com");
    }
    
    // Once Before method is completed, Test method will start 
    @Test public void login() throws InterruptedException {
    driver.findElement(By.xpath(".//*[@id='account']/a")).cli
    ck();
    
    driver.findElement(By.id("log")).sendKeys("testuser_1"); 
    driver.findElement(By.id("pwd")).sendKeys("Test@123"); 
    driver.findElement(By.id("login")).click();
    }
    
    @AfterClass public void afterTest() { 
    driver.quit();
    }
    
    }
    
    • Create a TestNG XML for running your test. Configure the TestNG XML for passing parameters i.e. to tell which browser should be used for running the Test.
      <?xml version="1.0" encoding="UTF-8"?>
      
      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
      
      <suite name="Suite" parallel="none">
      
      <test name="FirefoxTest">
      
      <parameter name="browser" value="firefox" />
      
      <classes>
      
      <class name="automationFramework.MultiBrowser" />
      
      </classes>
      
      </test>
       
      
      <test name="IETest">
      
      <parameter name="browser" value="ie" />
      
      <classes>
      
      <class name="automationFramework.MultiBrowser" />
      
      </classes>
      
      </test>
      
      </suite>
      

      Note: It can set any number of Browsers here and just for the example purpose it has set up only two main browsers.

    • Now it’s time to run the xml. Run the test by right click on the testng.xml file and select Run As > TestNG Suite.
    Parallel Testing in TestNG

    Using the feature provided by TestNG for Parallel Executions. just take the above example for Sign In application with two different browsers. This time all it is to execute test in both browsers simultaneously.

    Now just set the ‘parallel‘ attribute to ‘tests‘ in the above used xml and give a run again. This time it will notice that the both browsers will open almost simultaneously and the test will run in parallel.

    XHTML
    <?xml version="1.0" encoding="UTF-8"?>
     
    
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="tests">
    <test name="FirefoxTest">
    <parameter name="browser" value="firefox" />
    <classes>
    <class name="automationFramework.MultiBrowser" />
    </classes>
    </test>
    <test name="IETest">
    <parameter name="browser" value="ie" />
    <classes>
    <class name="automationFramework.MultiBrowser" />
    </classes>
    </test>
    </suite>
    
    TestNG Listeners

    Listener is defined as interface that modifes the default TestNG’s behavior. As the name suggests Listeners “listen” to the event defined in the selenium script and behave accordingly. It is used in selenium by implementing Listeners Interface. It allows customizing TestNG reports or logs. There are many types of TestNG listeners available.

    Types of Listeners in TestNG

    There are many types of listeners which allows you to change the TestNG’s behavior. Below are the few TestNG listeners:

    • IAnnotationTransformer
    • IAnnotationTransformer2
    • IConfigurable
    • IConfigurationListener
    • ExecutionListener
    • IHookable
    • IInvokedMethodListener
    • IInvokedMethodListener2
    • IMethodInterceptor
    • IReporter
    • ISuiteListener
    • ITestListener

    Above Interface are called TestNG Listeners. These interfaces are used in selenium to generate logs or customize the Testing reports.

    ITestListener has following methods

    • OnStart- OnStart method is called when any Test starts.
    • onTestSuccess- onTestSuccess method is called on the success of any Test.
    • onTestFailure- onTestFailure method is called on the failure of any Test.
    • onTestSkipped- onTestSkipped method is called on skipped of any Test.
    • onTestFailedButWithinSuccessPercentage- method is called each time Test fails but is within success percentage.
    • onFinish- onFinish method is called after all Tests are executed.
    Example

    Steps used

    • Create a ‘New Class’ file and give it a name ‘Listener‘, by right click on the Package and select New > Class.
    • Now Implements ISuiteListener, ITestListener and IInvokedMethodListener to this newly created class. For implementing a listener class, the class has to implement the org.testng.ITestListener interface. These classes are notified at runtime by TestNG when the test starts, finishes, fails, skips, or passes.
    package utility;
    
    import org.testng.IInvokedMethod;
    
    import org.testng.IInvokedMethodListener;
    
    import org.testng.ISuite;
    
    import org.testng.ISuiteListener;
    
    import org.testng.ITestContext;
    
    import org.testng.ITestListener;
    
    import org.testng.ITestNGMethod;
    
    import org.testng.ITestResult;
    
    import org.testng.Reporter;
    
    public class Listener implements ITestListener, ISuiteListener, IInvokedMethodListener {
    
    // This belongs to ISuiteListener and will execute before the Suite start
    
    @Override
    
    public void onStart(ISuite arg0) {
    
    Reporter.log("About to begin executing Suite " + arg0.getName(), true);
    
    }
    
    // This belongs to ISuiteListener and will execute, once the Suite is finished
    
    @Overrid
    
    public void onFinish(ISuite arg0) {
    
    Reporter.log("About to end executing Suite " + arg0.getName(), true);
    
    }
    
    // This belongs to ITestListener and will execute before starting of Test set/batch
    
    public void onStart(ITestContext arg0) {
    
    Reporter.log("About to begin executing Test " + arg0.getName(), true);
    
    }
    
    // This belongs to ITestListener and will execute, once the Test set/batch is finished
    
    public void onFinish(ITestContext arg0) {
    
    Reporter.log("Completed executing test " + arg0.getName(), true);
    
    }
    
    // This belongs to ITestListener and will execute only when the test is pass
    
    public void onTestSuccess(ITestResult arg0) {
    
    
    // This is calling the printTestResults method
    
    
    printTestResults(arg0);
    
    
    }
    
    
    // This belongs to ITestListener and will execute only on the event of fail test
    
    
    public void onTestFailure(ITestResult arg0) {
    
    // This is calling the printTestResults method
    
    printTestResults(arg0);
    
    }
     
    // This belongs to ITestListener and will execute before the main test start (@Test)
    
    public void onTestStart(ITestResult arg0) {
    
    System.out.println("The execution of the main test starts
    now");
    
    }
    
    // This belongs to ITestListener and will execute only if any of the main test(@Test) get skipped
    
    public void onTestSkipped(ITestResult arg0) {
    
    printTestResults(arg0);
    
    }
    
    public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {
    
    }
    
    // This is the method which will be executed in case of test pass or fail
    
    // This will provide the information on the test
    
    private void printTestResults(ITestResult result) {
    
    Reporter.log("Test Method resides in " + result.getTestClass().getName(), true);
    
    if (result.getParameters().length != 0) {
    
    String params = null;
    
    for (Object parameter : result.getParameters()) {
    
    params += parameter.toString() + ",";
    
    }
    
    Reporter.log("Test Method had the following parameters : " + params, true);
    
    }
    
    String status = null;
    
    switch (result.getStatus()) {
    
    case ITestResult.SUCCESS:
    
    status = "Pass";
    
    break;
    case ITestResult.FAILURE:
    
    status = "Failed";
    
    break;
    
    case ITestResult.SKIP:
    status = "Skipped";
    }
    
    Reporter.log("Test Status: " + status, true);
    
    }
    // This belongs to IInvokedMethodListener and will execute before every method including 
    @Before 
    @After 
    @Test
    
    public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1) {
    
    String textMsg = "About to begin executing following method : " + returnMethodName(arg0.getTestMethod());
    
    Reporter.log(textMsg, true);
    
    }
    
    // This belongs to IInvokedMethodListener and will execute after every method including 
    @Before 
    @After 
    @Test
    
    public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {
    
    String textMsg = "Completed executing following method : " + returnMethodName(arg0.getTestMethod());
    
    Reporter.log(textMsg, true);
    
    }
    
    // This will return method names to the calling function
    
    private String returnMethodName(ITestNGMethod method) {
    
    return method.getRealClass().getSimpleName() + "." + method.getMethodName();
    
    }
    
    }
    
    package automationFramework;
    
    
    import org.testng.annotations.AfterMethod;
    
    
    import org.testng.annotations.BeforeMethod;
    
    
    import org.testng.annotations.Test;
    
    
    // This code will implement TestNG listeners
    
    
    @Listeners(PackageName.ListenerClassName)
     
    
    // For e.g. @Listeners(utility.Listener.class)
    
    public class TestListener {
    
    @Test
    
    public void main() {
    }
    
    }
    

    Listener tag in TestNG xml: Although approach 1 is more than enough to get you started, it’s not an “elegant” way of using Listeners, because you are forced to add this @Listeners section to each of your classes, which you perhaps won’t want. So what you do is, you create a TestNG Suite xml and then add up the listeners section to this suite xml file. That way, all of your tests would essentially leverage the listener that you wrote.

    <suite name="Suite-Listeners" parallel="none">
    
    
    <listeners>
    <listener class-name="utility.Listener"></listener>
    </listeners>
    <test name="Batch-Listeners">
    <classes>
    <class name="automationFramework.TestListener" />
    </classes>
    </test>
    </suite>
    

    Output of the test case will look like this:

    Summary
    • TestNG is an open source automated testing framework
    • TestNG eliminates most of the limitations of the older framework and gives the developer the ability to write more flexible and powerful tests with help of easy annotations, grouping, sequencing & parameterizing
    • TestNG can also be used to run failed test-cases
    • TestNG allows testers to execute and run one script in multiple browsers
    • In TestNG, we cannot define a suite in testing source code, but it is represented by one XML file, as suite is the feature of execution
    • Asserts helps to verify the conditions of the test and decide whether test has failed or passed

    Copyright 1999- Ducat Creative, All rights reserved.