Implicit Wait in Serenity

HOME

Most Web applications are asynchronous by nature. So it has become necessary to wait for elements, before trying to interact with them. This can be achieved by the use of wait functionality.

I won’t recommend the use of Thread.sleep() statement to wait for a specific web element in the web page, as it slows down the execution as well as makes the test brittle. If you are using Serenity and Selenium for tests, then there are various default timeouts – Implicit, Explicit, and Fluent Wait.

What is Implicit Wait?

Implicit Waits are used to ensure that Serenity does not fail a test if a web element is not immediately present on the page when you first try to use it. Using Implicit wait, you can search for the web element for the specified amount of time. If still, the web element is not found, then Serenity throws NoSuchElementException exception.

To use ImplicitWait in the Test, mention the below-mentioned statement in serenity.properties.

webdriver.timeouts.implicitlywait

There is another way to add implicitwait. Add it to the serenity.conf file as shown below:-

webdriver {
    timeouts {
        implicitlywait = 5000
     }
}

Note:- Make sure to add webdriver and timeout in the same file, either both to properties file or both to conf files.

Let me explain the use of Implicit Wait. Below I have created two classes – ImplictWaitDemo and SynchronizationTests.

ImplictWaitDemo contains detail like default URL, XPath of web elements, methods containing the code for the test whereas SynchronizationTests class calls the tests defined in ImplictWaitDemo and run them using Serenity Runner (@RunWith(SerenityRunner.class)

Scenario 1 – The default value in Serenity for Implicit Wait is currently 2 seconds. In the below example, I’ll open a web page and will try to assert the text present in the webpage. Serenity will wait for 2 seconds and the web element will not be found in 2 secs, so the test fails.

ImplictWaitDemo

@DefaultUrl("http://the-internet.herokuapp.com/dynamic_loading/1")
public class ImplictWaitDemo extends PageObject {

	@FindBy(xpath = "//*[@id='start']/button")
	WebElementFacade startButton;

	@FindBy(xpath = "//*[@id='finish']/h4")
	WebElementFacade pageText;

	public void implictWaitDemo1() throws InterruptedException {
		open();

		startButton.click();
		Assert.assertEquals("Hello World!", pageText.getText())
	}
}

SynchronizationTests

@RunWith(SerenityRunner.class)
public class SynchronizationTests {

	ImplictWaitDemo demo;

	@Managed
	WebDriver driver;

	@Test
	public void waitTest1() throws InterruptedException {
		demo.implictWaitDemo1();

	}
}

This shows that Serenity waited for 2 sec for the text – Hello World.

Now, let us add Implicit Wait to the Test. I have added the implicitWait for 5 sec to each step.

webdriver {
    driver = firefox
    timeouts {
        implicitlywait = 5000
     }
}

Now, the Test is successful.

To know the value of wait in the code, you can use the below code. It will show the value of implicit wait in seconds or milliseconds.

System.out.println("Implicit Time defined for the test (in seconds):" + getImplicitWaitTimeout().toSeconds());
System.out.println("Implicit Time defined for the test (in milliseconds):" + getImplicitWaitTimeout().toMillis());

We are done! Congratulations on making it through this tutorial and hope you found it useful! Happy Learning!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s