Friday, 15 August 2014

Configure testng.xml In Eclipse For Create single or multiuple class run under single or multiple test.



We can create any number of class may run under in any number of test in same execution by using testng.xml file.


For Example:


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 {

    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");
  }
}




We can create above two class as Classone.java and ClassTwo.java. Then we create testng.xml file as below for run under same test or multiple test.


Configure testng.xml to run two classes in one test

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


When test execuion get complete we can see the both class are run under same test.



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>

No comments:

Post a Comment