Sagar Jani
Sagar Jani

Reputation: 161

Selenium Automation: Skip execution of test based on a condition in @BeforeMethod

My code is like this:

@BeforeMethod
public void beforeMethod() {
   url= GetUrlBasedOnParameter(parameter);
   if(is.empty(url)) {
      SkipTest();
   } else {
      ExecuteTest();
   }
}

What condition can I use in SkipTest() so that without adding any additional parameters in @Test annotation, I can skip the test?

FYI: I tried driver.quit() and driver.close() but the @Test annotation is still executed.

Upvotes: 1

Views: 749

Answers (1)

Mate Mrše
Mate Mrše

Reputation: 8394

In TestNG, you can use

throw new SkipException("message");

So, your @BeforeMethod could look like

@BeforeMethod
public void beforeMethod() {
   url= GetUrlBasedOnParameter(parameter);
   if(is.empty(url)) {
      throw new SkipException("URL is empty");
   } else {
      ExecuteTest();
   }
}

Upvotes: 1

Related Questions