About Me

Page views

Powered by Blogger.

Followers

Saturday 9 May 2015

Testng.xml : Creating Single Or Multiple Tests For Multiple Classes In WebDriver 

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 too.

First of all, Create 3 classes under project = TestNGOne and package = TestNGOnePack as bellow.

  • "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 = 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(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.in/2013/11/new-test.html"); 
    } 
    //@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.

2. ClassOne.java
package TestNGOnePack;

import org.testng.annotations.Test;

public class ClassOne extends TestNGOnePack.BaseClassOne{
 
 //@Test annotation describes 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 from BaseClassOne.

3. 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://only-testing-blog.blogspot.in/2014/01/textbox.html");
  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 from BaseClassOne.

Now your class structure will be looks like bellow in eclipse.





Configure testng.xml to run two classes in one test
Now let me show you how to configure testng.xml to run both classes in single test. Copy-paste bellow given lines 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="TestNGOnePack.ClassOne" />
   <class name="TestNGOnePack.ClassTwo" /> 
  </classes>
 </test> 
</suite>

When execution completed, View execution result reports. It will looks like bellow.



Configure testng.xml to run two classes in two tests
Now if you wants to run both classes as separate test then you have to configure your testng.xml file as bellow.
<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 bellow given testng.xml file and verify result.


Now compare both the results. In 1st example, Both 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.

 

0 comments:

Post a Comment

Popular Posts

Copyright © Learn Selenium Yourself | Powered by Blogger
Design by Duan Zhiyan | Blogger Theme by NewBloggerThemes.com