How To Enable/Disable @Test Method In Selenium WebDriver Using TestNG

TestNG has very good feature to enable or disable selenium webdriver @Test method. During test execution, If you do not wants to execute specific @Test method from test class then you can directly disable It using TestNG property enabled = false. It Is something like excluding @Test method from execution as described In THIS POST.

Let us try to Implement It practically. In bellow given test case, I have used @Test(priority=1,enabled = false) with method testCaseOne_Test_One(). Here, enabled = false property Is used for disabling that @Test method from execution.

Create bellow given test class and testng.xml file In your eclipse and run It.

1. Test_One.java
package Testing_Pack;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_One {
 WebDriver driver;
 WebElement dragElementFrom;

 @BeforeTest
 public void setup() throws Exception {
  System.out.println("In @BeforeTest Of Test_One.");
  driver =new FirefoxDriver();     
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  driver.get("http://only-testing-blog.blogspot.com/2014/04/calc.html");
 }
 
 @Test(priority=1,enabled = false)
 public void testCaseOne_Test_One() {
  System.out.println("Executing testCaseOne_Test_One.");
  driver.findElement(By.xpath("//input[@id='2']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='6']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseOne_Test_One = "+Result);
 }
 
 @Test(priority=2)
 public void testCaseTwo_Test_One() {
  System.out.println("Executing testCaseTwo_Test_One.");
  driver.findElement(By.xpath("//input[@id='Resultbox']")).clear();
  driver.findElement(By.xpath("//input[@id='3']")).click();
  driver.findElement(By.xpath("//input[@id='plus']")).click();
  driver.findElement(By.xpath("//input[@id='7']")).click();
  driver.findElement(By.xpath("//input[@id='equals']")).click();
  String Result = driver.findElement(By.xpath("//input[@id='Resultbox']")).getAttribute("value");
  System.out.println("Result of testCaseTwo_Test_One = "+Result);
 }
}


testng.xml
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Parallel Class Suite" parallel="classes" thread-count="2">
  <test name="Parallel Class Test" >
    <classes>
      <class name="Testing_Pack.Test_One"/>      
    </classes>
  </test>
</suite>

Test execution result will looks like bellow. If you can see In bellow Image, only  testCaseTwo_Test_One() method has been executed because testCaseOne_Test_One() @Test method Is disabled so TestNG will exclude It from execution.


No comments:

Post a Comment