Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

Is it possible to set timeout for page load in Selenium test?

I have a problem, when sometimes test execution freezes on page load (application doesn't load new page, test hangs up), so test needs to be killed manually - this wouldn't be a problem if I would run only one test on nightly bases.

As I understand driver.manage().timeouts().pageLoadTimeout() only works on driver.get(URL) method - it doesn't work when Selenium clicks buttons and links during test execution. Is this correct? How can I manage time of wait for every page load?

share|improve this question
    
Does this old question help ? sqa.stackexchange.com/questions/2606/… –  Phil Kirkham Jan 11 '13 at 12:50
    
Phil, it doesnt –  domakas Jan 11 '13 at 14:01
add comment

1 Answer 1

You're correct that Click() will not wait for a page load, so the page load timeout does not apply. I follow clicks with a wait for some element that I know will be on the page that is loading. You can setup an implicit timeout

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromMinutes(5));

and then simply try to find the appropriate element

link.Click();

// implicit timeout
var element = driver.FindElement(By.XPath("//div[contains(@class, 'alert-error')] | //ul[id('menu')]"));

or use an explicit timeout/wait. Note that the implicit timeout still affects this code, so any explicit timeouts should be longer than the implicit setting otherwise they won't really do anything.

link.Click();

// explicit timeout
var wait = new WebDriverWait(driver, TimeSpan.FromMinutes(5));
var element = wait.Until(driver => driver.FindElement(By.XPath("//div[contains(@class, 'alert-error')] | //ul[id('menu')]")));
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.