1.

How to run multiple test suite in selenium by using testing?

Answer»

We can configure our WebDriver test or WebDriver test suites for software testing project in testng.xml file. If we have two/multiple classes in test suite we can run those classes in the same test as well as two different tests too. Let's take a simple example here:

Create 3 classes under project = TestNGOne and package = TestNGOnePack as below.

BaseClassOne will be used for initializing and closing WebDriver instance,

ClassOne and ClassTwo will be used as test classes.

  1. BaseClassOne.java:
package TestNGOnePack; 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 BaseClassOne {     //Declared as public static to use same WebDriver instance publicly     public static WebDriver driver = null;     //@BeforeSuite annotation describes this method has to run before all suites     @BeforeSuite      public void setup() throws Exception {           System.setProperty("WebDriver.gecko.driver", "D:\geckodriver.exe");   driver = new FirefoxDriver();          driver.manage().window().maximize();          driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);          driver.get("https://nikasio.com");      }      //@AfterSuite annotation - this method has to run after execution of all suites     @AfterSuite          public void TEARDOWN() throws Exception {           driver.quit();      }  }

The above class will be base class and be used to initialize and close the WebDriver instance. 

  1. ClassOne.java:
package TestNGOnePack; import org.testng.annotations.Test; public class ClassOne extends TestNGOnePack.BaseClassOne{  //@Test annotation - this method as a test method  @Test   public void testmethodone() {     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+" -> testmethodone' has been executed successfully");   } }

Above ClassOne is inherited or created from BaseClassOne.

  1. ClassTwo.java:
package TestNGOnePack; import org.testng.annotations.Test; public class ClassTwo extends TestNGOnePack.BaseClassOne{  //@Test annotation describes this method as a test method  @Test   public void testmethodone() {   driver.navigate().to("http://facebook.com");   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+" -> testmethodone' has been executed successfully");   } }

Above ClassTwo is also inherited or created from BaseClassOne.

Configure testng.xml to run two classes in one test:

Copy the below-given lines in the testng.xml file and run it as TestNg Suite.

<suite name="Suite One" >  <test name="Test One" >   <classes>    <class name="TestNGOnePack.ClassOne" />    <class name="TestNGOnePack.ClassTwo" />    </classes>  </test>  </suite>

When execution is completed, View execution results report.

Configure testng.xml to run two classes in two tests:

Now if we want to run both classes as SEPARATE test then we have to configure testng.xml file as below

<suite name="Suite One" >  <test name="Test One" >   <classes>    <class name="TestNGOnePack.ClassOne" />     </classes>  </test>   <test name="Test Two" >   <classes>    <class name="TestNGOnePack.ClassTwo" />    </classes>  </test>  </suite>

Here, "ClassOne" is included in 'Test One" test and "ClassTwo" is included in 'Test Two" test. Now run below given testng.xml file and verify results.

After comparing both results. In the 1st example, Both classes are executed under the same test but in the second example, both classes are executed under separate tests. This way you can configure the testng.xml file as per your requirement.



Discussion

No Comment Found