Volodymyr  Prysiazhniuk
Volodymyr Prysiazhniuk

Reputation: 1913

Java WebDriver wait for page to load

I want to get exception of page load, but still have not results on it. I use implicitlyWait to set timer to throw exception.

WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.MILLISECONDS);
driver.get("http://www.rambler.ru");
driver.quit();

Could somebody please update me with suggestions? I need this to make sure that page load will not be infinite, and if time to load will be more than I've defined in timer -> throw exception as result and skip TC (as failed).

Thank you, Volodymyr

Upvotes: 8

Views: 39273

Answers (2)

Pazonec
Pazonec

Reputation: 1559

Why are you using implicit wait before the opening of the page? Try to use explicit wait. Find some major page element at ramber(for example, the search textbox). For example:

 WebDriverWait wait = new WebDriverWait(webDriver, 5);
 wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath_to_search_textbox")));

until() method will throw TimeoutException if search text box will not appear within 5 seconds.

Upvotes: 17

djangofan
djangofan

Reputation: 29669

I do not agree that Pavel Zorins answer will work because he doesn't show how to handle the exceptions.

Here is how I do a wait for an iFrame. This requires that your JUnit test class pass the instance of RemoteWebDriver into the page object :

public class IFrame1 extends LoadableComponent<IFrame1> {

    private RemoteWebDriver driver;

    @FindBy(id = "iFrame1TextFieldTestInputControlID" )
    public WebElement iFrame1TextFieldInput;

    @FindBy(id = "iFrame1TextFieldTestProcessButtonID" )
    public WebElement copyButton;

    public IFrame1( RemoteWebDriver drv ) {
        super();
        this.driver = drv;
        this.driver.switchTo().defaultContent();
        waitTimer(1, 1000);
        this.driver.switchTo().frame("BodyFrame1");
        LOGGER.info("IFrame1 constructor...");
    }

    @Override
    protected void isLoaded() throws Error {        
        LOGGER.info("IFrame1.isLoaded()...");
        PageFactory.initElements( driver, this );
        try {
            assertTrue( "Page visible title is not yet available.", driver
     .findElementByCssSelector("body form#webDriverUnitiFrame1TestFormID h1")
                    .getText().equals("iFrame1 Test") );
        } catch ( NoSuchElementException e) {
            LOGGER.info("No such element." );
            assertTrue("No such element.", false);
        }
    }

    @Override
    protected void load() {
        LOGGER.info("IFrame1.load()...");
        Wait<WebDriver> wait = new FluentWait<WebDriver>( driver )
                .withTimeout(30, TimeUnit.SECONDS)
                .pollingEvery(5, TimeUnit.SECONDS)
                .ignoring( NoSuchElementException.class ) 
                .ignoring( StaleElementReferenceException.class ) ;
            wait.until( ExpectedConditions.presenceOfElementLocated( 
            By.cssSelector("body form#webDriverUnitiFrame1TestFormID h1") ) );
    }
....

NOTE: You can see my entire working example here.

Upvotes: 0

Related Questions