Friday, 15 August 2014

How To Skip WebDriver Test In TestNG





We can skip the execution from perticular test by using throw new SkipException() exception. THis is TestNG exception.

Sometimes you need to check some condition like If some condition match then skip test else perform some action In your webdriver test. In this kind of situation, you can use SkipException() exception.


Consider below example




package TestNG_Package;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class TestNG_Test1 {
WebDriver driver1=new FirefoxDriver();
@BeforeMethod
public void OpenBrowser(){
driver1.get("http://google.com");
driver1.manage().window().maximize();
}
@AfterMethod
public void CloseBrowser(){
driver1.quit();
}
@Test
public void MethodOne() {

String a= "Selenium";
if(a.equals("Selenium")
throw new SkipException("Skipped");

String title = driver1.getTitle();
System.out.println("title"+title);   
}



@Test
public void MethodTwo() {
String title = driver1.getTitle();
System.out.println("title"+title);   
}


}



o/p

MethodOne Skipped due to SkipException()
MethodTwo executed


From above example if execution throw SkipExeption() means the rest of the commands inside the method wont get executed. Its skipped from current method and control goes to next method.

No comments:

Post a Comment