[Automation] How to get Selenium to wait for a page to load

[Automation] How to get Selenium to wait for a page to load


In my experience with many automation projects at Shift ASIA, I realize that one of the most popular bug while running automation script is about waiting time to page load missing. The reason is when a user clicks a URL, because of slow internet connection, inadequate website optimization, heavy website content or any other reasons, the web page takes a while to load. Obviously, the user would have to wait for the page to load before clicking a button, link, or image to continue their browsing journey.

In automation testing, all possible user scenarios must be automated so that QAs can monitor how to website would act in the real world. The scenario detailed above must also be automated for the same reason. So today I will introduce how we can make Selenium wait for a page to load during an automated test.

There are three ways to implement Selenium to wait for page to load:

  • Using Implicit Wait
  • Using Explicit Wait
  • Using Fluent Wait

1/ Using Implicit Wait

The Implicit Wait tell WebDriver to wait a specific amount of time (say, 30 seconds) before proceeding with the next step. If the tester knows how much time the page and element will take to load, they should use Implicit Wait.

Let's say a website under test takes 10s to load a page until a particular elements shows up. In that case, set implicit wait to 10s .The test will pause and once the time passes, WebDriver will continue to run the script as planned.

WebDriver driver => new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

2/ Using Explicit Wait

The Explicit Wait is more advanced in its functioning. It instructs WebDriver to pause a test until a predetermined condition is fulfilled.

Let's say the website under test has a feature displaying a pop-up. The user has to enter some data following which a pop-up appears. This feature needs to be tested in this exact sequence, including the time taken for the user to input data, server response time, ...

WebDriver driver => new ChromeDriver();
WebDriverElement elm = WebDriverWait(driver, 30).until(ExpectedConditon.presence_of_element_located(By.id, ("elm iD")))

3/ Using Fluent Wait

The Fluent Wait is an advancement on the Explicit Wait. Using it, testers can define a specific condition and the frequency for which WebDriver should check for the condition to appear in a particular length of time.

Let's say the website under test includes some elements that load dynamically. The tester knows it takes a total of 5s to load, not more.But it can become visible anytime between 0 to 5 seconds.

WebDriver driver => new ChromeDriver();
FluentWait wait = new FluentWait(driver);

To Conclude

I think using Selenium Wait for page to load is quite necessary for automated Selenium testing since it is a common occurrence in everyday internet user's browsing journey.And I hope this my share will be helpful to you.