Shruti
Shruti

Reputation: 15

Selenium/Testng: Skip or Fail rest of @Test if an Exception occur

I am writing my test case using Selenium and TestNG.

How can I skip/fail the rest of a Test when an exception is caught?

I tried to catch block in every @Test. Each @Test is based on priority which dependents on previous ones.

For example like if I have 3 steps on a test

@BeforeTest
public void login(){
}
@Test(priority = 1)
public void Verifytabs() {
}
@Test(priority = 2)
public void checkhomepage() {
 }
@Test(priority = 3)
public void clickonProfile() {
}
@AfterTest
public void logout()
{
}

Upvotes: 1

Views: 2201

Answers (1)

Subburaj
Subburaj

Reputation: 2334

If any of your Test method depends on other test method, then you can use the dependsOnMethods annotation.

Sample Code:

@BeforeTest
public void login(){
}
@Test(priority = 1)
public void Verifytabs() {
}
@Test(dependsOnMethods = {"Verifytabs"})
public void checkhomepage() {
}
@Test(dependsOnMethods={"checkhomepage"})
public void clickonProfile() {
}

@AfterTest
public void logout()
{
}

Here, If Verifytabs Test method fails,then all the dependent method will be skipped and If it is passed, then all the dependent method will be executed.

Upvotes: 1

Related Questions