SUBSCRIBE HERE TO GET POST UPDATES VIA EMAIL :

Delivered by FeedBurner

testng.xml : Creating WebDriver Test Suite Using Classes From Different Packages

Now you are already aware about HOW TO CREATE testng.xml FILE to configure and run your webdriver test. The main reason behind popularity of TestNG framework for webdriver is we can configure our test as per our requirements. I have listed some Similarities/Differences between Junit and TestNG framework In THIS POST. In my previous post, We have already seen example of how to configure testng.xml file to run single/multiple webdriver test classes of same package. Now let me show you an example of how to create test suite using classes from different packages.

First of all let we create new packages and classes as described in bellow given 3 steps.

Step 1. Create package = "TestNGOnePack" with classes = BaseClassOne.java, ClassOne.java and ClassTwo.java exactly as described in my PREVIOUS POST.

Step 2. Create package = "TestNGTwoPack" under same project and add ClassOne.java and ClassTwo.java files with bellow given code lines.

ClassOne.java
package TestNGTwoPack;

import org.testng.annotations.Test;

public class ClassOne extends TestNGOnePack.BaseClassOne{
  
 @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");
  }
}

ClassTwo.java
package TestNGTwoPack;

import org.testng.annotations.Test;

public class ClassTwo extends TestNGOnePack.BaseClassOne{
  
 @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 given both classes are inherited from TestNGOnePack.BaseClassOne to use webdriver instance, initialization and closing the webdriver.

Step 3. Create package = "TestNGThreePack" under same project and add ClassOne.java and ClassTwo.java files with above given code lines. You need to change package name in both class(TestNGTwoPack to TestNGThreePack) to use them in "TestNGThreePack" package.

So now, my package and class hierarchy is as bellow for project TestNgOne.


Now supposing I do not want to execute all class of all packages and I wants to execute only few of them like TestNGOnePack.ClassOne, TestNGTwoPack.ClassTwo, TestNGThreePack.ClassOne and TestNGThreePack.ClassTwo. How can we do that? You need to configure your testng.xml file as bellow.
<suite name="Suite One">
 <test name="Test One" >
  <classes>
   <class name="TestNGOnePack.ClassOne" />
   <class name="TestNGTwoPack.ClassTwo" />
   <class name="TestNGThreePack.ClassOne" />
   <class name="TestNGThreePack.ClassTwo" />  
  </classes>
 </test> 
</suite>

Above given file will run only described 4 classes out of total 6 classes under single test (Test One). You can divide them in multiple tests too as described in my previous post.

Now execute above given testng.xml file and verify result. It will looks like as shown in bellow given image.


This way we can configure our test suite using only specific class from different packages.

No comments:

Post a Comment