Skip to content

Commit 7c1fae3

Browse files
authored
[dotnet] Support setting timeouts in capabilities (#13698)
Setting timeout values in driver options
1 parent e7db08f commit 7c1fae3

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

dotnet/src/webdriver/DriverOptions.cs

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,24 @@ public enum PageLoadStrategy
8989
None
9090
}
9191

92+
internal class Timeout
93+
{
94+
public TimeSpan? Script { get; set; }
95+
public TimeSpan? PageLoad { get; set; }
96+
public TimeSpan? ImplicitWait { get; set; }
97+
98+
public Dictionary<string, object> ToCapabilities()
99+
{
100+
var timeoutCapabilities = new Dictionary<string, object>();
101+
102+
if (Script.HasValue) timeoutCapabilities.Add("script", Script.Value.TotalMilliseconds);
103+
if (PageLoad.HasValue) timeoutCapabilities.Add("pageLoad", PageLoad.Value.TotalMilliseconds);
104+
if (ImplicitWait.HasValue) timeoutCapabilities.Add("implicit", ImplicitWait.Value.TotalMilliseconds);
105+
106+
return timeoutCapabilities;
107+
}
108+
}
109+
92110
/// <summary>
93111
/// Base class for managing options specific to a browser driver.
94112
/// </summary>
@@ -102,6 +120,9 @@ public abstract class DriverOptions
102120
private bool? useWebSocketUrl;
103121
private bool useStrictFileInteractability;
104122
private bool? enableDownloads;
123+
private TimeSpan? scriptTimeout;
124+
private TimeSpan? pageLoadTimeout;
125+
private TimeSpan? implicitWaitTimeout;
105126
private UnhandledPromptBehavior unhandledPromptBehavior = UnhandledPromptBehavior.Default;
106127
private PageLoadStrategy pageLoadStrategy = PageLoadStrategy.Default;
107128
private Dictionary<string, object> additionalCapabilities = new Dictionary<string, object>();
@@ -219,6 +240,52 @@ public bool? EnableDownloads
219240
set { this.enableDownloads = value; }
220241
}
221242

243+
/// <summary>
244+
/// Gets or sets the asynchronous script timeout, which is the amount
245+
/// of time the driver should wait when executing JavaScript asynchronously.
246+
/// This timeout only affects the <see cref="IJavaScriptExecutor.ExecuteAsyncScript(string, object[])"/>
247+
/// method.
248+
/// </summary>
249+
public TimeSpan? ScriptTimeout
250+
{
251+
get { return this.scriptTimeout; }
252+
set { this.scriptTimeout = value; }
253+
}
254+
255+
/// <summary>
256+
/// Gets or sets the page load timeout, which is the amount of time the driver
257+
/// should wait for a page to load when setting the <see cref="IWebDriver.Url"/>
258+
/// property.
259+
/// </summary>
260+
public TimeSpan? PageLoadTimeout
261+
{
262+
get { return this.pageLoadTimeout; }
263+
set { this.pageLoadTimeout = value; }
264+
}
265+
266+
/// <summary>
267+
/// Gets or sets the implicit wait timeout, which is the amount of time the
268+
/// driver should wait when searching for an element if it is not immediately
269+
/// present.
270+
/// </summary>
271+
/// <remarks>
272+
/// When searching for a single element, the driver should poll the page
273+
/// until the element has been found, or this timeout expires before throwing
274+
/// a <see cref="NoSuchElementException"/>. When searching for multiple elements,
275+
/// the driver should poll the page until at least one element has been found
276+
/// or this timeout has expired.
277+
/// <para>
278+
/// Increasing the implicit wait timeout should be used judiciously as it
279+
/// will have an adverse effect on test run time, especially when used with
280+
/// slower location strategies like XPath.
281+
/// </para>
282+
/// </remarks>
283+
public TimeSpan? ImplicitWaitTimeout
284+
{
285+
get { return this.implicitWaitTimeout; }
286+
set { this.implicitWaitTimeout = value; }
287+
}
288+
222289
/// <summary>
223290
/// Set or Get the location of the browser
224291
/// Override in subclass
@@ -537,6 +604,18 @@ protected IWritableCapabilities GenerateDesiredCapabilities(bool isSpecification
537604
}
538605
}
539606

607+
if (this.scriptTimeout.HasValue || this.pageLoadTimeout.HasValue || this.implicitWaitTimeout.HasValue)
608+
{
609+
var timeouts = new Timeout
610+
{
611+
Script = this.scriptTimeout,
612+
PageLoad = this.pageLoadTimeout,
613+
ImplicitWait = this.implicitWaitTimeout
614+
};
615+
616+
capabilities.SetCapability(CapabilityType.Timeouts, timeouts.ToCapabilities());
617+
}
618+
540619
foreach (KeyValuePair<string, object> pair in this.additionalCapabilities)
541620
{
542621
capabilities.SetCapability(pair.Key, pair.Value);

dotnet/src/webdriver/Timeouts.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public Timeouts(WebDriver driver)
6363
/// will have an adverse effect on test run time, especially when used with
6464
/// slower location strategies like XPath.
6565
/// </para>
66+
/// <para>
67+
/// Also can be managed via driver <see cref="DriverOptions.ImplicitWaitTimeout"/> option.
68+
/// </para>
6669
/// </remarks>
6770
public TimeSpan ImplicitWait
6871
{
@@ -76,6 +79,11 @@ public TimeSpan ImplicitWait
7679
/// This timeout only affects the <see cref="IJavaScriptExecutor.ExecuteAsyncScript(string, object[])"/>
7780
/// method.
7881
/// </summary>
82+
/// <remarks>
83+
/// <para>
84+
/// Also can be managed via driver <see cref="DriverOptions.ScriptTimeout"/> option.
85+
/// </para>
86+
/// </remarks>
7987
public TimeSpan AsynchronousJavaScript
8088
{
8189
get { return this.ExecuteGetTimeout(AsyncScriptTimeoutName); }
@@ -87,6 +95,11 @@ public TimeSpan AsynchronousJavaScript
8795
/// should wait for a page to load when setting the <see cref="IWebDriver.Url"/>
8896
/// property.
8997
/// </summary>
98+
/// <remarks>
99+
/// <para>
100+
/// Also can be managed via driver <see cref="DriverOptions.PageLoadTimeout"/> option.
101+
/// </para>
102+
/// </remarks>
90103
public TimeSpan PageLoad
91104
{
92105
get

dotnet/test/common/Environment/DriverFactory.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,10 @@ protected void OnDriverLaunching(DriverService service, DriverOptions options)
179179
options.PageLoadStrategy = overriddenOptions.PageLoadStrategy;
180180
options.UnhandledPromptBehavior = overriddenOptions.UnhandledPromptBehavior;
181181
options.Proxy = overriddenOptions.Proxy;
182+
183+
options.ScriptTimeout = overriddenOptions.ScriptTimeout;
184+
options.PageLoadTimeout = overriddenOptions.PageLoadTimeout;
185+
options.ImplicitWaitTimeout = overriddenOptions.ImplicitWaitTimeout;
182186
}
183187

184188
return options;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using NUnit.Framework;
2+
using OpenQA.Selenium.Environment;
3+
using System;
4+
5+
namespace OpenQA.Selenium
6+
{
7+
[TestFixture]
8+
public class TimeoutDriverOptionsTest
9+
{
10+
private IWebDriver driver;
11+
12+
private readonly TimeSpan defaultScriptTimeout = TimeSpan.FromMilliseconds(30_000);
13+
private readonly TimeSpan defaultPageLoadTimeout = TimeSpan.FromMilliseconds(300_000);
14+
private readonly TimeSpan defaultImplicitWaitTimeout = TimeSpan.Zero;
15+
16+
[TearDown]
17+
public void TearDown()
18+
{
19+
driver?.Quit();
20+
}
21+
22+
[Test]
23+
public void CanSetScriptTimeout()
24+
{
25+
var expectedScriptTimeout = TimeSpan.FromSeconds(5);
26+
27+
var options = new TestDriverOptions()
28+
{
29+
ScriptTimeout = expectedScriptTimeout,
30+
};
31+
32+
Assert.That(options.ScriptTimeout, Is.EqualTo(expectedScriptTimeout));
33+
Assert.That(options.PageLoadTimeout, Is.Null);
34+
Assert.That(options.ImplicitWaitTimeout, Is.Null);
35+
36+
driver = EnvironmentManager.Instance.CreateDriverInstance(options);
37+
38+
Assert.That(driver.Manage().Timeouts().AsynchronousJavaScript, Is.EqualTo(expectedScriptTimeout));
39+
40+
// other timeout options are still default
41+
Assert.That(driver.Manage().Timeouts().PageLoad, Is.EqualTo(defaultPageLoadTimeout));
42+
Assert.That(driver.Manage().Timeouts().ImplicitWait, Is.EqualTo(defaultImplicitWaitTimeout));
43+
}
44+
45+
[Test]
46+
public void CanSetPageLoadTimeout()
47+
{
48+
var expectedPageLoadTimeout = TimeSpan.FromSeconds(5);
49+
50+
var options = new TestDriverOptions()
51+
{
52+
PageLoadTimeout = expectedPageLoadTimeout,
53+
};
54+
55+
Assert.That(options.PageLoadTimeout, Is.EqualTo(expectedPageLoadTimeout));
56+
Assert.That(options.ScriptTimeout, Is.Null);
57+
Assert.That(options.ImplicitWaitTimeout, Is.Null);
58+
59+
driver = EnvironmentManager.Instance.CreateDriverInstance(options);
60+
61+
Assert.That(driver.Manage().Timeouts().PageLoad, Is.EqualTo(expectedPageLoadTimeout));
62+
63+
// other timeout options are still default
64+
Assert.That(driver.Manage().Timeouts().AsynchronousJavaScript, Is.EqualTo(defaultScriptTimeout));
65+
Assert.That(driver.Manage().Timeouts().ImplicitWait, Is.EqualTo(defaultImplicitWaitTimeout));
66+
}
67+
68+
[Test]
69+
public void CanSetImplicitWaitTimeout()
70+
{
71+
var expectedImplicitWaitTimeout = TimeSpan.FromSeconds(5);
72+
73+
var options = new TestDriverOptions()
74+
{
75+
ImplicitWaitTimeout = expectedImplicitWaitTimeout,
76+
};
77+
78+
Assert.That(options.ImplicitWaitTimeout, Is.EqualTo(expectedImplicitWaitTimeout));
79+
Assert.That(options.ScriptTimeout, Is.Null);
80+
Assert.That(options.PageLoadTimeout, Is.Null);
81+
82+
driver = EnvironmentManager.Instance.CreateDriverInstance(options);
83+
84+
Assert.That(driver.Manage().Timeouts().ImplicitWait, Is.EqualTo(expectedImplicitWaitTimeout));
85+
86+
// other timeout options are still default
87+
Assert.That(driver.Manage().Timeouts().AsynchronousJavaScript, Is.EqualTo(defaultScriptTimeout));
88+
Assert.That(driver.Manage().Timeouts().PageLoad, Is.EqualTo(defaultPageLoadTimeout));
89+
}
90+
[Test]
91+
public void CanSetTimeout()
92+
{
93+
var expectedScriptTimeout = TimeSpan.FromSeconds(3);
94+
var expectedPageLoadTimeout = TimeSpan.FromSeconds(4);
95+
var expectedImplicitWaitTimeout = TimeSpan.FromSeconds(5);
96+
97+
var options = new TestDriverOptions()
98+
{
99+
ScriptTimeout = expectedScriptTimeout,
100+
PageLoadTimeout = expectedPageLoadTimeout,
101+
ImplicitWaitTimeout = expectedImplicitWaitTimeout,
102+
};
103+
104+
Assert.That(options.ScriptTimeout, Is.EqualTo(expectedScriptTimeout));
105+
Assert.That(options.PageLoadTimeout, Is.EqualTo(expectedPageLoadTimeout));
106+
Assert.That(options.ImplicitWaitTimeout, Is.EqualTo(expectedImplicitWaitTimeout));
107+
108+
driver = EnvironmentManager.Instance.CreateDriverInstance(options);
109+
110+
Assert.That(driver.Manage().Timeouts().AsynchronousJavaScript, Is.EqualTo(expectedScriptTimeout));
111+
Assert.That(driver.Manage().Timeouts().PageLoad, Is.EqualTo(expectedPageLoadTimeout));
112+
Assert.That(driver.Manage().Timeouts().ImplicitWait, Is.EqualTo(expectedImplicitWaitTimeout));
113+
}
114+
115+
class TestDriverOptions : DriverOptions
116+
{
117+
public override ICapabilities ToCapabilities()
118+
{
119+
return null;
120+
}
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)