Skip Tests in Selenium at Runtime using TestNG

In the previous tutorial, we discussed a technique to ignore tests in Selenium WebDriver with the help of TestNG. Our agenda for this tutorial is about skipping tests at runtime in Selenium WebDriver. You should have knowledge of handling exceptions if you want to implement skip tests technique in your Selenium project. Hence, we simply implement an exception to add skip tests feature in our automation test case.

What are the differences between ‘ignore tests’ and ‘skip tests’ feature of the TestNG?

This is an obvious question because there might be some confusion in your mind regarding these two terms. In short, ignore tests feature of TestNG used when we do not want to execute specific tests, so we disable such tests before compilation; whereas, skip tests work at runtime which simply means the driver will throw SkipException at runtime.

Suppose you came across a scenario in an e-commerce website wherein it is required to validate some of the components at Product description page (PDP). At first, you proceed by typing product code in the search box and further, it navigates you to the PDP of the respective product. Now a situation comes that product automatically gets deleted and WebDriver tries to search that product in the search box but not able to navigate to the PDP so in such case, normally, you fail the test case. But I would suggest skipping such tests, instead of failing it, because skipping is the best choice here. A test case can only be failed where there is a mismatch between expected and the actual result, but here validation could only be done if it navigates to PDP so skipping here at runtime is the best choice.

I believe the above explanation will clear all your confusions regarding ‘skip tests’ and ‘ignore tests’ feature of the TestNG.

Let’s go ahead and see the implementation of Skip tests feature using TestNG’s SkipException(message) constructor.

What is SkipException(message) in TestNG?

SkipException is basically a class which extends RuntimeException. So, we simply pass string message as arguments to its constructor. Therefore, we have following constructor types of the SkipException class:

How to implement skip tests feature of TestNG in Selenium project?

We simply have to use the Java concept of throwing an exception in a statement. You can use the following statement inside the test case which will skip the test by invoking SkipException.

Sample Program

Here is the sample program of Skip tests implementation.


In this class, we have two methods which are written inside @Test annotation. In testWithSkip() method, we implemented SkipExcaption. In this method, there is a String array. First, it searches for the element at 0th index, further, it is looking for elements at 1st index from another array when if the statement is true. We see test gets skipped when the expected element of the second array is not there. It’s your call to identify the scenario where you can implement SkipException.

One thing you should notice, if we do assert here then the test will stop right there after finding a mismatch. But it never happens in case of SkipException, rather TestNG skips the current test and starts invoking another method (as seen in below console’s screenshot).

Skip tests

Console Output

 Skip tests using SkipException

Join Inviul fb group

One Response

Leave a Reply