Skip to main content

Rerun Failed JUnit Tests

Hello Friends,
Many times Selenium tests are getting failed due to no reason... as UI tests are fragile in nature.
There are many uncertainties like slow response, browser issues... and many more.

Generally we use readily available Test frameworks like JUnit & TestNG to write test cases. TestNG framework has lot off cool features like rerun test, dependency test and many more... but still many of us have JUnit as first choice.


JUnit frame work does not provide any direct feature to rerun the failed tests. So problem is

How we can rerun Failed test in JUnit?


JUnit provides a Interface TestRule, by implementing this we can apply rule for running the test. We have used same class to solve our problem, Please have a look below code snippet in which have created Class Retry which implements the TestRule interface.
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;

 * Created by Kirtesh on 20-02-2015.
public class Retry implements TestRule {
    private int retryCount;

    public Retry(int retryCount) {
        this.retryCount = retryCount;

    public Statement apply(Statement base, Description description) {
        return statement(base, description);
    private Statement statement(final Statement base, final Description description) {
        return new Statement() {
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;

                // implement retry logic here
                for (int i = 0; i < retryCount; i++) {
                    try {
                    } catch (Throwable t) {
                        caughtThrowable = t;

                        //  System.out.println(": run " + (i+1) + " failed");
                        System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed");
                System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
                throw caughtThrowable;

That's it.. you can directly copy above class & use this rule in your junit test... look in to below test
    @Rule public Retry retry = new Retry(3);
    public void myWebDriverTest(){
above test will get rerun if it get fails for 3 time, 3 is the number which you can set in @Rule Retry...

Hope this may hep you.....

Keep exploring... Keep automating....
Post a Comment

Popular posts from this blog

Alternate Way for sendKeys

One of my friend called me yesterday, he had an interview on Selenium WebDriver and got confused on 1 Question.

What is an alternative for sendKeys Method in WebDriver? I also got stammered on this and thought in mind
Why the hell do we need this? There are below possible reason that I could find, When we need any alternative to sendKeys.
When the Text element is disabled or locked, sendKeys can not set the value in text field.
(in my opinion, this is not the correct way of Automation, because the element is locked or disabled by intend to not allow any text insertion)When we want to write huge text as input... that time the way WebDriver work, by sending Series of characters from String one by one, and Which is Very time consuming. so to minimize that time we can use this alternate method So now we have above two problems with sendKeys ......
Solution!!! The best alternative I found is JavaScript
Way 1 to Execute Script WebDriver driver = new FirefoxDriver(); driver…

Selecting Date on JQuery Datepicker

Hello Friends,

In this post we will see how to select specific date in JQuery Datepicker.

Its very easy to enter date as text by .sendKeys() method, but mostly when application uses JQuery Datepicker, it disables typing on the text-box. the text-box sets to read only.

While automating such screens we need to write logic which can interact with Datepicker and selects desired date.

Consider below example of Datepicker.

Html code for above example is

To automate this, we first break apart a problem.
Decide which date we want to set Read the current date from datepicker Calculate difference between both dates in Months(difference will positive in case of set date is in future respective to current date & will negative when set date is in past respective to current date of datepicker) Set loop depends on month difference and navigate the datepicker to specific month Select the Day. 
Lets implement the above logic.
Note: we have used Joda Date Time Library for calculate date and time …

XPATH Basics....

XPATH Basics...
XPath is a text syntax for locating parts of HTML document XPath contains a library of standard functions which helps to locate the WebElement XPath is a W3C recommendation
XPath uses path expressions to select WebElement or sets of WebElements in Web page. These expressions look very much like the expressions you see when you work with a traditional computer file system. like c:\MyFolder\another_folder\myFile.txt
XPATH terminologies Nodes While using Selenium WebDriver, we come across following three types of nodes
elementattributetext Look at below HTML code sample
Link to GoogleLink to YahooLink to FaceBook here div, a are element nodes (element is the basically type of WebElement). Attribute nodes for div element is class and its value is myLink"a" element has text nodes as Link to Google, Link to Yahoo, Link to FaceBook Relationship of Nodes There are various relations available in XPATH but as a selenium user we must have knowledge of below relations…