TitleToBeEqualTo(string expectedTitle)
{
return () => { return driver.Title == expectedTitle; };
}
/**
* Sets given pageLoadTimeout to the {@link #driver} and asserts that attempt to navigate to a
* page that takes much longer (10 seconds longer) to load results in a TimeoutException.
*
* Side effects: 1) {@link #driver} is configured to use given pageLoadTimeout,
* 2) test HTTP server still didn't serve the page to browser (some browsers may still
* be waiting for the page to load despite the fact that driver responded with the timeout).
*
*/
private void TestPageLoadTimeoutIsEnforced(long webDriverPageLoadTimeoutInSeconds)
{
// Test page will load this many seconds longer than WD pageLoadTimeout.
long pageLoadTimeBufferInSeconds = 10;
string slowLoadingPageUrl = EnvironmentManager.Instance.UrlBuilder.WhereIs("sleep?time=" + (webDriverPageLoadTimeoutInSeconds + pageLoadTimeBufferInSeconds));
driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(webDriverPageLoadTimeoutInSeconds);
AssertPageLoadTimeoutIsEnforced(() => driver.Url = slowLoadingPageUrl, webDriverPageLoadTimeoutInSeconds, pageLoadTimeBufferInSeconds);
}
private void AssertPageLoadTimeoutIsEnforced(TestDelegate delegateToTest, long webDriverPageLoadTimeoutInSeconds, long pageLoadTimeBufferInSeconds)
{
DateTime start = DateTime.Now;
Assert.That(delegateToTest, Throws.InstanceOf(), "I should have timed out after " + webDriverPageLoadTimeoutInSeconds + " seconds");
DateTime end = DateTime.Now;
TimeSpan duration = end - start;
Assert.That(duration.TotalSeconds, Is.GreaterThan(webDriverPageLoadTimeoutInSeconds));
Assert.That(duration.TotalSeconds, Is.LessThan(webDriverPageLoadTimeoutInSeconds + pageLoadTimeBufferInSeconds));
}
private void InitLocalDriver(PageLoadStrategy strategy)
{
EnvironmentManager.Instance.CloseCurrentDriver();
if (localDriver != null)
{
localDriver.Quit();
}
PageLoadStrategyOptions options = new PageLoadStrategyOptions();
options.PageLoadStrategy = strategy;
localDriver = EnvironmentManager.Instance.CreateDriverInstance(options);
}
private class PageLoadStrategyOptions : DriverOptions
{
public override void AddAdditionalOption(string capabilityName, object capabilityValue)
{
}
public override ICapabilities ToCapabilities()
{
return null;
}
}
}