Include/Exclude Only Selected Test Methods In Selenium WebDriver-TestNg Test Suite Using testng.xml

If you are using selenium webdriver with TestNg framework then you can easily run your selected test methods from selected classes. Supposing you have a two classes in your package and first class have three test methods and second class have five test methods. Now you wants to run only one test method from first class and two test methods from second class then you can configure it very easily in testng.xml file. In sort, you can include or exclude selected test methods from execution. Let me show you how to do it with simple webdriver test configured by testng.xml file.

Configure TestNGOne Project In Eclipse
  1. First of all, Configure TestNGOne project in eclipse as described in PREVIOUS POST.
  2. Add testmethodtwo() bellow the testmethodone() as shown bellow in ClassOne.java and ClassTwo.java file of all three packages.
public class ClassTwo extends TestNGOnePack.BaseClassOne{
  
 @Test
  public void testmethodone() {
  driver.navigate().to("http://only-testing-blog.blogspot.com/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");
  }
 
        //Add testmethodtwo() here
 @Test
 public void testmethodtwo() {
     driver.findElement(By.xpath("//input[@value='female']"));
     String Workdir = System.getProperty("user.dir");
     String Classpackname = this.getClass().getName();
     System.out.print("\n'"+Workdir+" -> "+Classpackname+" -> testmethodtwo' has been executed successfully");
  }
}

Now your TestNGOne project's structure will looks like bellow.


Configure testng.xml file to Include and run selected webdriver test methods from few classes
Now supposing, I wants to run only

  • testmethodone() method from TestNGOnePack -> ClassOne.java
  • testmethodone() and testmethodtwo() methods from TestNGTwoPack -> ClassTwo.java
  • testmethodone() and testmethodtwo() methods from TestNGThreePack -> ClassOne.java
  • testmethodone() and testmethodtwo() methods from TestNGThreePack -> ClassTwo.java
To perform above action, I have to configure my testng.xml file as bellow.

<suite name="Suite One">
  <test name="Test One" >
    <classes>
        <class name="TestNGOnePack.ClassOne" />
          <methods>
             <include name="testmethodone" />
          </methods>
      <class name="TestNGTwoPack.ClassTwo" />        
    </classes>
       <packages>
             <package name="TestNGThreePack" />
       </packages>
    </test> 
</suite>

In above given testng.xml file, methods and include tags are new to learn. You can read about TestNg Framework's <suite>, <test>, <classes> and <class> tags in THIS POST and <packages>, <package> tags in THIS POST. <methods> tag defines the group of methods and <include> tag defines which method you wants to include in execution. Now execute this testng.xml file and verify the result report.


If you see in above result, Only test testmethodone() is executed from TestNGOnePack -> ClassOne.java. This way we can include any specific method in our test suite to execute from any class.

Configure testng.xml file to exclude specific test method from class
Sameway, To exclude specific test method from class, We can use <exclude> tag inside <methods> tag as bellow.

<suite name="Suite One">
  <test name="Test One" >
    <classes>
        <class name="TestNGOnePack.ClassOne" />
          <methods>
              <include name="testmethodone" />
          </methods>
       <class name="TestNGTwoPack.ClassTwo" />
         <methods>
              <exclude name="testmethodone" />
          </methods>        
    </classes>
       <packages>
             <package name="TestNGThreePack" />
       </packages>
    </test> 
</suite>

When you run above given testng.xml file, it will exclude testmethodone() test method of TestNGTwoPack -> ClassTwo.jav from execution and will execute only ClassTwo.java file as shown in bellow given test result report.


This way we can configure our testng.xml file to include or exclude any specific test method from execution.

2 comments:

  1. Bhai Bahut sikhaya hai tumne ..thank you so much and keep it up.

    ReplyDelete
  2. Hi this format is correct? I mean is possible use twice the methods tag in a class tag?








    ReplyDelete