Skip to content

Commit 8dd2d6a

Browse files
committed
Split driver and service
1 parent f16c08e commit 8dd2d6a

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

dotnet/test/common/AssemblyFixture.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public async Task RunBeforeAnyTestAsync()
4747
public async Task RunAfterAnyTestsAsync()
4848
{
4949
EnvironmentManager.Instance.CloseCurrentDriver();
50+
EnvironmentManager.Instance.CloseCurrentDriverService();
5051
await EnvironmentManager.Instance.WebServer.StopAsync();
5152
if (EnvironmentManager.Instance.Browser == Browser.Remote)
5253
{

dotnet/test/common/Environment/DriverFactory.cs

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,51 @@ private void PopulateOptionsTypes()
5555

5656
public event EventHandler<DriverStartingEventArgs> DriverStarting;
5757

58-
public IWebDriver CreateDriver(Type driverType)
58+
public DriverService CreateDriverService(Type driverType)
5959
{
60-
return CreateDriverWithOptions(driverType, null);
60+
DriverService service = null;
61+
62+
if (typeof(ChromeDriver).IsAssignableFrom(driverType))
63+
{
64+
service = ChromeDriverService.CreateDefaultService();
65+
}
66+
else if (typeof(EdgeDriver).IsAssignableFrom(driverType))
67+
{
68+
service = EdgeDriverService.CreateDefaultService();
69+
}
70+
else if (typeof(InternetExplorerDriver).IsAssignableFrom(driverType))
71+
{
72+
service = InternetExplorerDriverService.CreateDefaultService();
73+
}
74+
else if (typeof(FirefoxDriver).IsAssignableFrom(driverType))
75+
{
76+
service = FirefoxDriverService.CreateDefaultService();
77+
}
78+
else if (typeof(SafariDriver).IsAssignableFrom(driverType))
79+
{
80+
service = SafariDriverService.CreateDefaultService();
81+
}
82+
83+
if (!String.IsNullOrEmpty(this.driverPath) && service != null)
84+
{
85+
service.DriverServicePath = Path.GetDirectoryName(this.driverPath);
86+
service.DriverServiceExecutableName = Path.GetFileName(this.driverPath);
87+
}
88+
89+
return service;
6190
}
6291

63-
public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverOptions)
92+
public IWebDriver CreateDriver(DriverService service, Type driverType)
93+
{
94+
return CreateDriverWithOptions(service, driverType, null);
95+
}
96+
97+
public IWebDriver CreateDriverWithOptions(DriverService service, Type driverType, DriverOptions driverOptions)
6498
{
6599
Console.WriteLine($"Creating new driver of {driverType} type...");
66100

67101
Browser browser = Browser.All;
68-
DriverService service = null;
102+
69103
DriverOptions options = null;
70104

71105
List<Type> constructorArgTypeList = new List<Type>();
@@ -78,7 +112,6 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
78112
var chromeOptions = (ChromeOptions)options;
79113
chromeOptions.AddArguments("--no-sandbox", "--disable-dev-shm-usage");
80114

81-
service = ChromeDriverService.CreateDefaultService();
82115
if (!string.IsNullOrEmpty(this.browserBinaryLocation))
83116
{
84117
((ChromeOptions)options).BinaryLocation = this.browserBinaryLocation;
@@ -92,7 +125,6 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
92125
var edgeOptions = (EdgeOptions)options;
93126
edgeOptions.AddArguments("--no-sandbox", "--disable-dev-shm-usage");
94127

95-
service = EdgeDriverService.CreateDefaultService();
96128
if (!string.IsNullOrEmpty(this.browserBinaryLocation))
97129
{
98130
((EdgeOptions)options).BinaryLocation = this.browserBinaryLocation;
@@ -102,13 +134,11 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
102134
{
103135
browser = Browser.IE;
104136
options = GetDriverOptions<InternetExplorerOptions>(driverType, driverOptions);
105-
service = InternetExplorerDriverService.CreateDefaultService();
106137
}
107138
else if (typeof(FirefoxDriver).IsAssignableFrom(driverType))
108139
{
109140
browser = Browser.Firefox;
110141
options = GetDriverOptions<FirefoxOptions>(driverType, driverOptions);
111-
service = FirefoxDriverService.CreateDefaultService();
112142
if (!string.IsNullOrEmpty(this.browserBinaryLocation))
113143
{
114144
((FirefoxOptions)options).BinaryLocation = this.browserBinaryLocation;
@@ -118,18 +148,12 @@ public IWebDriver CreateDriverWithOptions(Type driverType, DriverOptions driverO
118148
{
119149
browser = Browser.Safari;
120150
options = GetDriverOptions<SafariOptions>(driverType, driverOptions);
121-
service = SafariDriverService.CreateDefaultService();
122-
}
123-
124-
if (!String.IsNullOrEmpty(this.driverPath) && service != null)
125-
{
126-
service.DriverServicePath = Path.GetDirectoryName(this.driverPath);
127-
service.DriverServiceExecutableName = Path.GetFileName(this.driverPath);
128151
}
129152

130153
this.OnDriverLaunching(service, options);
131154

132155
driver = (IWebDriver)Activator.CreateInstance(driverType, service, options);
156+
133157
return driver;
134158
}
135159

dotnet/test/common/Environment/EnvironmentManager.cs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class EnvironmentManager
3333
private static EnvironmentManager instance;
3434
private Type driverType;
3535
private Browser browser;
36+
private DriverService driverService;
3637
private IWebDriver driver;
3738
private UrlBuilder urlBuilder;
3839
private TestWebServer webServer;
@@ -205,7 +206,9 @@ private EnvironmentManager()
205206
{
206207
webServer.StopAsync().Wait();
207208
}
209+
208210
CloseCurrentDriver();
211+
CloseCurrentDriverService();
209212
}
210213

211214
public event EventHandler<DriverStartingEventArgs> DriverStarting;
@@ -265,6 +268,18 @@ public UrlBuilder UrlBuilder
265268
}
266269
}
267270

271+
public DriverService GetCurrentDriverService()
272+
{
273+
if (driverService != null)
274+
{
275+
return driverService;
276+
}
277+
278+
driverService = driverFactory.CreateDriverService(driverType);
279+
280+
return driverService;
281+
}
282+
268283
public IWebDriver GetCurrentDriver()
269284
{
270285
if (driver != null)
@@ -277,14 +292,19 @@ public IWebDriver GetCurrentDriver()
277292
}
278293
}
279294

295+
public DriverService CreateDriverServiceInstance()
296+
{
297+
return driverFactory.CreateDriverService(driverType);
298+
}
299+
280300
public IWebDriver CreateDriverInstance()
281301
{
282-
return driverFactory.CreateDriver(driverType);
302+
return driverFactory.CreateDriver(GetCurrentDriverService(), driverType);
283303
}
284304

285305
public IWebDriver CreateDriverInstance(DriverOptions options)
286306
{
287-
return driverFactory.CreateDriverWithOptions(driverType, options);
307+
return driverFactory.CreateDriverWithOptions(GetCurrentDriverService(), driverType, options);
288308
}
289309

290310
public IWebDriver CreateFreshDriver()
@@ -294,6 +314,13 @@ public IWebDriver CreateFreshDriver()
294314
return driver;
295315
}
296316

317+
public void CloseCurrentDriverService()
318+
{
319+
driverService?.Dispose();
320+
321+
driverService = null;
322+
}
323+
297324
public void CloseCurrentDriver()
298325
{
299326
if (driver != null)

0 commit comments

Comments
 (0)