Monday, September 23, 2019

Post#111.How to execute Multiple Classes and Multiple Test using testng.xml File


How to execute Multiple Classes and Multiple Test using testng.xml File
In testng.xml file we can specify multiple names which need to be executed.
In a project there may be many classes, but we want to execute only the selected classes.
If say suppose, you have two/multiple classes in your test suite then how will you run them? We can run both the classes in same test as well in 2 different tests.

Let’s understand this with below example.
First of all, Create 3 classes under project = DemoTestNG and package = TestNGPack1 as bellow.

"BaseClass" will be used for initializing and closing WebDriver instance, 
"Program1" and "Program2" will be used as test classes.




BaseClass.Java

package TestNGPack1;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;

public class BaseClass {
              //Declared as public static to use same webdriver instance publicly
    public static WebDriver driver = new FirefoxDriver();

    //@BeforeSuite annotation describes this method has to run before all suites
    @BeforeSuite
    public void setup() throws Exception {
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
         driver.get("http://www.testingbar.com/");
    }
    //@AfterSuite annotation describes this method has to run after execution of all suites
    @AfterSuite
         public void tearDown() throws Exception {
         driver.quit();
    }
}
Above class will be used as base class to initialize and close WebDriver instance.

Program1.Java

package TestNGPack1;
import org.testng.annotations.Test;

public class Program1 extends BaseClass {
              //@Test annotation describes this method as a test method
               @Test
                public void testmethod1() {
                  String title = driver.getTitle();
                  System.out.print("\nCurrent page title is : "+title);
                  String Workdir = System.getProperty("user.dir");
                  String Classpackname = this.getClass().getName();
                  System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethod1' has been executed successfully");
                }
}

Above Program1 is inherited from BaseClass.

Program2.java

package TestNGPack1;

import org.testng.annotations.Test;

public class Program2 extends BaseClass{
              //@Test annotation describes this method as a test method
               @Test
                public void testmethod2() {
                driver.navigate().to("http://www.testingbar.com/selenium/");
                String title = driver.getTitle();
                System.out.print("\nCurrent page title is : "+title);
                String Workdir = System.getProperty("user.dir");
                String Classpackname = this.getClass().getName();
                System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethod2' has been executed successfully");
                }
             

}

Now you are done with Class Files setup. It’s the time to configure testng.xml File.

Configure testng.xml to run two classes in one test
Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, both classes are included in single test (Test One).
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" />
   <class name="TestNGPack1.Program2" />
  </classes>
 </test>
</suite>

Once execution completed the execution result reports will looks like this.


Configure testng.xml to run two classes in two tests
Copy-paste the below code in your testng.xml file and then Run it as TestNg Suite. Here, both classes are included in separate test.
Here, "Program1" is included in 'Test One" test and "Program2" is included in 'Test Two" test. 
<suite name="Suite One" >
 <test name="Test One" >
  <classes>
   <class name="TestNGPack1.Program1" /> 
  </classes>
 </test>
 <test name="Test Two" >
  <classes>
   <class name="TestNGPack1.Program2" />
  </classes>
 </test>
</suite>
Once execution completed the execution result reports will looks like this.


Now compare both the results. In 1st example, both the classes are executed under same test but in 2nd example both classes are executed under separate tests. This way you can configure testng.xml file as per your requirement.



No comments:

Post a Comment