EBOOK – How to Retry Page Methods on Error


This ebook started with a tough problem to solve.

I was working in an automation project and some of my tests would fail unexpectedly and randomly.

Take, for example, the following lines of code:

public void type(String keyword) {
   WebElement name = driver.findElement(NAME_ID);


This code would randomly throw exceptions when

  • finding the element or
  • clicking the element or
  • clearing the existing value or
  • typing a keyword in the element

Most of the time, the exception would be about the name element being stale.

Why would this happen?

After a short discussion with the developers, I found out that the site’s elements had auto-postback enabled so every time there was an interaction with an element, a request was sent to the application server which would reply with a new web element.

In other words, the element was constantly refreshed.

How would you handle this situation?

I thought initially about catching the exception and handling it.

It did not make a difference.

Then I added a do-while statement so that the code is executed again in case of an exception:

public ResultsPage searchFor(String keyword) {
  int i = 0;

  do {
    try {
      return new ResultsPage(driver);
    catch (Exception e) {
      System.out.println("retrying searchFor ...");
  } while (++i < 5);

  throw new RuntimeException("couldnt execute searchFor()");

private void searchWith(String keyword) {
  WebElement searchBox = driver.findElement(SEARCHBOX_ID);
  WebElement searchButton = driver.findElement(SEARCHBUTTON_ID);

This worked well but a new problem appeared.

Every time these exceptions happened in another method,  I would have to use again and again a do-while and try-catch.

This lead to lots of code that followed the next pattern:

public SomeType retryMethod() {
  int i = 0;

  do {
    try {
      return new SomeType();
    catch (Exception e) {
      System.out.println("retrying method ...");
  } while (++i < 5);

  throw new RuntimeException("could not execute retryMethod!");


I dont like code duplication so I needed a better and much more elegant solution for  retrying methods on error.

This ebook shows you how to do it.

It is very short and you can see the first 2 chapter here.

If you want the complete book, it is just $7 for you.

But, before buying, please keep in mind that this is not a book aimed at beginners.

How do you get it?

Please transfer $7 through Paypal to alex@alexsiminiuc.com.

As soon as the payment is confirmed, I will email you the PDF file.