4

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?

2

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')]")));
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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