Skip to content

Commit 6a74f03

Browse files
committed
Start Async!
1 parent c4b6bf9 commit 6a74f03

2 files changed

Lines changed: 78 additions & 1 deletion

File tree

dotnet/src/webdriver/Chrome/ChromeDriver.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,61 @@ public ChromeDriver(ChromeDriverService service, ChromeOptions options, TimeSpan
157157
this.AddCustomChromeCommands();
158158
}
159159

160+
/// <summary>
161+
/// Initializes a new instance of the <see cref="ChromeDriver"/> class using the specified command executor and options.
162+
/// </summary>
163+
/// <param name="commandExecutor">The <see cref="ICommandExecutor"/> to use for executing commands.</param>
164+
/// <param name="options">The <see cref="ChromeOptions"/> to use for this driver.</param>
165+
/// <param name="autoStartSession">Whether to automatically start the session.</param>
166+
protected ChromeDriver(ICommandExecutor commandExecutor, ChromeOptions options, bool autoStartSession)
167+
: base(commandExecutor, options, autoStartSession)
168+
{
169+
if (autoStartSession)
170+
{
171+
this.AddCustomChromeCommands();
172+
}
173+
}
174+
175+
/// <summary>
176+
/// Asynchronously creates and starts a new instance of the <see cref="ChromeDriver"/> class with default options.
177+
/// </summary>
178+
/// <returns>A task that represents the asynchronous operation. The task result contains the initialized <see cref="ChromeDriver"/>.</returns>
179+
public static Task<ChromeDriver> StartAsync()
180+
{
181+
return StartAsync(new ChromeOptions());
182+
}
183+
184+
/// <summary>
185+
/// Asynchronously creates and starts a new instance of the <see cref="ChromeDriver"/> class using the specified options.
186+
/// </summary>
187+
/// <param name="options">The <see cref="ChromeOptions"/> to be used with the Chrome driver.</param>
188+
/// <returns>A task that represents the asynchronous operation. The task result contains the initialized <see cref="ChromeDriver"/>.</returns>
189+
/// <exception cref="ArgumentNullException">If <paramref name="options"/> is <see langword="null"/>.</exception>
190+
public static async Task<ChromeDriver> StartAsync(ChromeOptions options)
191+
{
192+
if (options is null)
193+
{
194+
throw new ArgumentNullException(nameof(options), "Chrome options must not be null");
195+
}
196+
197+
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
198+
ICommandExecutor executor = await GenerateDriverServiceCommandExecutorAsync(service, options, DefaultCommandTimeout).ConfigureAwait(false);
199+
200+
ChromeDriver driver = new(executor, options, autoStartSession: false);
201+
driver.AddCustomChromeCommands();
202+
203+
try
204+
{
205+
await driver.StartSessionAsync(options.ToCapabilities()).ConfigureAwait(false);
206+
return driver;
207+
}
208+
catch
209+
{
210+
driver.Dispose();
211+
throw;
212+
}
213+
}
214+
160215
/// <summary>
161216
/// Gets a read-only dictionary of the custom WebDriver commands defined for ChromeDriver.
162217
/// The keys of the dictionary are the names assigned to the command; the values are the

dotnet/src/webdriver/Chromium/ChromiumDriver.cs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,20 @@ protected ChromiumDriver(ChromiumDriverService service, ChromiumOptions options,
132132
this.optionsCapabilityName = options.CapabilityName ?? throw new ArgumentException("No chromium options capability name specified", nameof(options));
133133
}
134134

135+
/// <summary>
136+
/// Initializes a new instance of the <see cref="ChromiumDriver"/> class using the specified command executor and options.
137+
/// </summary>
138+
/// <param name="commandExecutor">The <see cref="ICommandExecutor"/> to use for executing commands.</param>
139+
/// <param name="options">The <see cref="ChromiumOptions"/> to be used with the ChromiumDriver.</param>
140+
/// <param name="autoStartSession">Whether to automatically start the session.</param>
141+
/// <exception cref="ArgumentNullException">If <paramref name="options"/> is <see langword="null"/>.</exception>
142+
/// <exception cref="ArgumentException">If the Chromium options capability name is <see langword="null"/>.</exception>
143+
protected ChromiumDriver(ICommandExecutor commandExecutor, ChromiumOptions options, bool autoStartSession)
144+
: base(commandExecutor, ConvertOptionsToCapabilities(options), autoStartSession)
145+
{
146+
this.optionsCapabilityName = options.CapabilityName ?? throw new ArgumentException("No chromium options capability name specified", nameof(options));
147+
}
148+
135149
/// <summary>
136150
/// Gets the dictionary of custom Chromium commands registered with the driver.
137151
/// </summary>
@@ -144,7 +158,15 @@ await GenerateDriverServiceCommandExecutorAsync(service, options, commandTimeout
144158
.GetAwaiter().GetResult();
145159
}
146160

147-
private static async Task<ICommandExecutor> GenerateDriverServiceCommandExecutorAsync(DriverService service, DriverOptions options, TimeSpan commandTimeout)
161+
/// <summary>
162+
/// Asynchronously generates a driver service command executor.
163+
/// </summary>
164+
/// <param name="service">The <see cref="DriverService"/> to use.</param>
165+
/// <param name="options">The <see cref="DriverOptions"/> to be used with the driver.</param>
166+
/// <param name="commandTimeout">The maximum amount of time to wait for each command.</param>
167+
/// <returns>A task that represents the asynchronous operation. The task result contains the <see cref="ICommandExecutor"/>.</returns>
168+
/// <exception cref="ArgumentNullException">If <paramref name="service"/> or <paramref name="options"/> are <see langword="null"/>.</exception>
169+
protected static async Task<ICommandExecutor> GenerateDriverServiceCommandExecutorAsync(DriverService service, DriverOptions options, TimeSpan commandTimeout)
148170
{
149171
if (service is null)
150172
{

0 commit comments

Comments
 (0)