Testing Excellence

  • QA Basics
  • Technical QA
  • Agile Testing
  • Questions
  • Quizzes
Home » Selenium WebDriver » WebDriver Wait For Page to Load Example in Java

WebDriver Wait For Page to Load Example in Java

Updated: December 2, 2018 - Amir Ghahrai

In this WebDriver tutorial, we take a look at how to wait for a page to complete loading before doing other actions. Two examples in Java on wait for page to load in WebDriver.

Wait For Page To Load – Method #1

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;

public class Tutorials {

    WebDriver driver = new FirefoxDriver();

    public void waitForPageLoaded() {
        ExpectedCondition<Boolean> expectation = new
                ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver driver) {
                        return ((JavascriptExecutor) driver).executeScript("return document.readyState").toString().equals("complete");
                    }
                };
        try {
            Thread.sleep(1000);
            WebDriverWait wait = new WebDriverWait(driver, 30);
            wait.until(expectation);
        } catch (Throwable error) {
            Assert.fail("Timeout waiting for Page Load Request to complete.");
        }
    }
}

Wait For Page To Load Method #2

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Tutorials {

    public void waitForLoad(WebDriver driver) {
        ExpectedCondition<Boolean> pageLoadCondition = new
                ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver driver) {
                        return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
                    }
                };
        WebDriverWait wait = new WebDriverWait(driver, 30);
        wait.until(pageLoadCondition);
    }
}

Tags: Selenium WebDriver








Selected Articles

  • Test Automation Problems
  • Test Automation Strategy
  • Agile Test Strategy example
  • How QAs add value in agile
  • Agile without automation
  • How agile killed managers
  • Agile testing challenges
  • Testing e-commerce websites
  • Role of QA manager in agile
  • Are you a good agile tester?
  • BDD tips and best practices
  • Myths of test automation
  • Test automation tips
  • Test automation pros & cons






Copyright © 2019 All rights reserved · www.testingexcellence.com

  • About
  • Contact
  • Subscribe
  • Privacy Policy
  • Terms of Use