In Selenium, what is the "wait for the page to stop loading" command,

How to make WebDriver wait for the page to load.

It means that it waits and checks whether the whole page has been loaded or not, and only it will continue the execution of the next line.

+4
source share
4 answers

The biggest problem is that there is no generic universal solution for one size that will work even for most users. The concept of “when my page finished loading” is virtually meaningless in today's dynamic JavaScript-dependent AJAX-dependent network. You can wait until the browser detects network traffic, but it does not take into account JavaScript execution. One could define “full” as an event onload, but this does not take into account the possibility of using the page setTimeout(). In addition, none of these definitions take into account frames or frames.

, , . , Selenium RC API 10 . , - , waitForPageToLoad . , API WebDriver . (, driver.get()), " " . , , (, element.click()), , , , .

WebDriver , , , , . WebDriverWait . , , . , , .

+9

Selenium - , , .

+1

, Selenium SlowLoadableComponent, , : https://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/support/ui/SlowLoadableComponent.html. , extend SlowLoadableComponent. abstract SlowLoadableComponent: load() isLoaded()

isLoaded() , , . load() , . ( ). get() , SlowLoadableComponent, isLoaded(). , load() . , .

, . Selenium , , , . , , -. "", . , -. "" .

. :

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.SlowLoadableComponent;
import org.openqa.selenium.support.ui.SystemClock;

public abstract class AbstractLoadableComponent<T extends AbstractLoadableComponent<T>> extends SlowLoadableComponent<T> {

    public static final int DEFAULT_TIMEOUT_IN_SECONDS = 30;
    private final WebDriver driver;
    private final int timeoutInSeconds;

    public AbstractLoadableComponent(final WebDriver driver, final int timeoutInSeconds) {
        super(new SystemClock(), timeoutInSeconds);

        this.driver = driver;
        this.timeoutInSeconds = timeoutInSeconds;
        this.load();
    }

    public final WebDriver getDriver() {
        return driver;
    }

    public final int getTimeoutInSeconds() {
        return timeoutInSeconds;
    }

    @Override
    protected void load() {
        PageFactory.initElements(getDriver(), this);
    }
}

:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.SlowLoadableComponent;

public abstract class AbstractPage<T extends AbstractPage<T>> extends AbstractLoadableComponent<T> {

    private final String url;

    public AbstractPage(final WebDriver driver) {
        this(driver, driver.getCurrentUrl(), DEFAULT_TIMEOUT_IN_SECONDS);
    }

    public AbstractPage(final WebDriver driver, final String url) {
        this(driver, url, DEFAULT_TIMEOUT_IN_SECONDS);
    }

    public AbstractPage(final WebDriver driver, final String url, final int timeoutInSeconds) {
        super(driver, timeoutInSeconds);
        this.url = url;
    }

    public final String getUrl() {
        return url;
    }

    @Override
    protected void load() {
        super.load();

        if(url != null) {
            getDriver().get(url);
        }
    }
}

:

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;

import static org.testng.Assert.assertTrue;

public final class LoginPage extends AbstractPage<LoginPage>  {

    @FindBy(how = How.ID, using = "username")
    private WebElement usernameBox;

    @FindBy(how = How.ID, using = "password")
    private WebElement passwordBox;

    @FindBy(how = How.NAME, using = "login")
    private WebElement loginButton;

    public LoginPage(final WebDriver driver) {
        this(driver, driver.getCurrentUrl(), DEFAULT_TIMEOUT_IN_SECONDS);
    }

    public LoginPage(final WebDriver driver, final String url) {
        this(driver, url, DEFAULT_TIMEOUT_IN_SECONDS);
    }

    public LoginPage(final WebDriver driver, final String url, final int timeoutInSeconds) {
        super(driver, url, timeoutInSeconds);
    }

    @Override
    protected final void isLoaded() throws Error {
        try {
            assertTrue(usernameBox.isDisplayed(), "Username text box is not displayed");
            assertTrue(passwordBox.isDisplayed(), "Password text box is not displayed");
            assertTrue(loginButton.isDisplayed(), "Login button is not displayed");
        } catch(NoSuchElementException nsee) {
            throw new Error(nsee);
        }
    }
}
+1
source

driver.manage.implicitlywait (3, TimeUnit.Seconds) will be hep.

0
source

All Articles