i

Selenium Step By Step Guide

Handling Multiple Test Suites

We can run multiple test suites using TestNG. We can create two different test suites and can keep the separate test cases in each. We need to create a testing.xml file and specify the test classes of each test suites. Let us see the steps below to start with it:

  1. Create a new project in eclipse.
  2. Create two packages.
  3. Create one test class in each package.
  4. Create a testing.xml file and save it in the project base directory.

Once we follow the above steps, then our project directory looks like this:

Let us see both the test class files of these test suites:

TestSuite1:

package suite1;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Optional;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

public class TestSuite1 {

      WebDriver driver;

      @BeforeMethod

      void setup()

      {

      System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/chromedriver.exe");

            driver= new ChromeDriver();

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

      }

     

     

      @Parameters({"username","password"})

      @Test

      void parameterized( @Optional("KcNew") String username, @Optional("admin")String password) throws InterruptedException

      {

            Thread.sleep(3000);

        driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input")).sendKeys(username);

        driver.findElement(By.xpath("//*[@id='admin_login']/div[2]/input")).sendKeys(password);

       

      }

     

      @AfterMethod

      void cleanup()

      {

//          driver.quit();

      }

 

}

 

TestSuite2:

package suite2;

 

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.Optional;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

public class TestSuite2 {

      WebDriver driver;

      @BeforeMethod

      void setup()

      {

      System.setProperty("webdriver.chrome.driver",System.getProperty("user.dir")+"/chromedriver.exe");

            driver= new ChromeDriver();

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

      }

     

 

     

      @Parameters({"username","password"})

      @Test

      void parameterized( @Optional("KcNew") String username, @Optional("admin")String password) throws InterruptedException

      {

            Thread.sleep(3000);

        driver.findElement(By.xpath("//*[@id='admin_login']/div[1]/input")).sendKeys(username);

        driver.findElement(By.xpath("//*[@id='admin_login']/div[2]/input")).sendKeys(password);

       

      }

     

      @AfterMethod

      void cleanup()

      {

//          driver.quit();

      }

 

}

 

Let us see how out testing.xml should look like to run both the test suites.

 

 

 

 

 

   

     

   

 

  

  

  

   

     

   

 

 

Both the suites are included as part of the TestNG suite tag in the testing.xml file, and this lets us execute both the tests by testing.xml itself.

Once we run the testing.xml as TestNG Suite, we can see the execution results of TestNG suite under “Results of running suite” section as below: