How To Skip WebDriver Test In TestNG

If you remember, I have posted many posts on TestNG Framework and you will find all those posts on THIS LINK or you can VIEW THIS PAGE for all related posts listing. Study all of them one by one on posted date order for your better understanding. Till now we have learnt about how to Include or exclude TEST METHOD, PACKAGE from execution. We have also learnt REGULAR EXPRESSION USAGE for Including or Excluding Test methods from execution. Now supposing you wants to skip specific test Intentionally from execution then how can you do It?

Skipping WebDriver Test Intentionally
One way of skipping selenium webdriver test method Is using throw new SkipException() exception - 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.

Let us look at simple webdriver test case example where I have placed SkipException() Inside if condition to Intentionally skip that test. Please note one thing here : once SkipException() thrown, remaining part of that test method will be not executed and control will goes directly to next test method execution.

In bellow given example, If condition will match so throw new SkipException() part will be executed and due to that exception, "After If Else" statement will be not printed. Intensional_Skip() method will be displayed as skipped in Testng report.

Run bellow given example In your eclipse and observe result.

package Testng_Pack;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.SkipException;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class Test_NG {
    
    public static WebDriver driver = null;

    
    @BeforeTest
    public void setup() throws Exception { 
         System.setProperty("webdriver.gecko.driver", "D:\\Selenium Files\\geckodriver.exe");
         driver = new FirefoxDriver();
         driver.manage().window().maximize();
         driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
         driver.get("http://only-testing-blog.blogspot.com/2013/11/new-test.html"); 
    } 
    
    @Test
    public void Intensional_Skip(){
     System.out.println("In Verify_Title");
     String titl = driver.getTitle();
     
     if(titl.equals("Only Testing: New Test")){
                //To Skip Test
      throw new SkipException("Test Check_Checkbox Is Skipped");
      
     }else{
      System.out.println("Check the Checkbox");
         driver.findElement(By.xpath("//input[@value='Bike']")).click();
     }
     System.out.println("After If Else");
    }   
    
    @Test
    public void Radio_check(){
     System.out.println("In Check_Radio");
     driver.findElement(By.xpath("//input[@value='male']")).click();
    }
    
    @AfterTest
         public void tearDown() throws Exception { 
         driver.quit(); 
    } 
}

testng.xml CLICK HERE to view how to create testng.xml
<suite name="Simple Suite">
 <test name="Simple Skip Test">
  <classes>
   <class name = "Testng_Pack.Test_NG"/>
  </classes>
 </test> 
</suite>

Result will be as bellow:


5 comments:

  1. Hi,
    Thanks for sharing your Ideas,
    I have a scenario where I want to skip the test but I don't want this to get add in my test testNG report as skipped, waiting for your Idea :)

    ReplyDelete
  2. Hi,
    Your Blog is very good, and i am getting lots of knowledge from it.
    I have a query-
    I am using retry analyser with testng in my code to run failed test case 3 time but first 2 attempt are coming as a SKIP instead of a FAIL in report.

    ReplyDelete
  3. Hi,
    Its a very helpful article.
    I have a query,i am using retry analyser with testng to run failed test cases 3 times ,but when i am running it first 2 invocation are coming as skipped rather than fail in report

    ReplyDelete
  4. Why you are not using "ENABLED=FALSE" Annotation for skipping?

    ReplyDelete
  5. Why you are not using Enabled annotation for skip?

    ReplyDelete