-
Notifications
You must be signed in to change notification settings - Fork 6
/
WebDriverFactory.cs
183 lines (144 loc) · 7.96 KB
/
WebDriverFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using Atata.WebDriverSetup;
using Lombiq.Tests.UI.Extensions;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Chromium;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.IO;
namespace Lombiq.Tests.UI.Services;
public static class WebDriverFactory
{
public static ChromeDriver CreateChromeDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout)
{
ChromeDriver CreateDriverInner(ChromeDriverService service)
{
var chromeConfig = new ChromeConfiguration { Options = new ChromeOptions().SetCommonOptions() };
chromeConfig.Options.SetLoggingPreference(LogType.Browser, LogLevel.Info);
// Disabling the Chrome sandbox can speed things up a bit, so it's recommended when you get a lot of
// timeouts during parallel execution:
// https://stackoverflow.com/questions/22322596/selenium-error-the-http-request-to-the-remote-webdriver-timed-out-after-60-sec
// However, this makes the executing machine vulnerable to browser-based attacks so it should only be used
// with trusted code (like our own).
chromeConfig.Options.AddArgument("no-sandbox");
// Linux-specific setting, may be necessary for running in containers, see
// https://developers.google.com/web/tools/puppeteer/troubleshooting#tips for more information.
chromeConfig.Options.AddArgument("disable-dev-shm-usage"); // #spell-check-ignore-line
chromeConfig.Options.SetCommonChromiumOptions(configuration);
configuration.BrowserOptionsConfigurator?.Invoke(chromeConfig.Options);
chromeConfig.Service = service ?? ChromeDriverService.CreateDefaultService();
chromeConfig.Service.SuppressInitialDiagnosticInformation = true;
// By default localhost is only allowed in IPv4.
chromeConfig.Service.WhitelistedIPAddresses += "::ffff:127.0.0.1";
// Helps with misconfigured hosts.
if (chromeConfig.Service.HostName == "localhost") chromeConfig.Service.HostName = "127.0.0.1";
return new ChromeDriver(chromeConfig.Service, chromeConfig.Options, pageLoadTimeout).SetCommonTimeouts(pageLoadTimeout);
}
var chromeWebDriverPath = Environment.GetEnvironmentVariable("CHROMEWEBDRIVER"); // #spell-check-ignore-line
if (chromeWebDriverPath is { } driverPath && Directory.Exists(driverPath))
{
return CreateDriverInner(ChromeDriverService.CreateDefaultService(driverPath));
}
return CreateDriver(BrowserNames.Chrome, () => CreateDriverInner(service: null));
}
public static EdgeDriver CreateEdgeDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout) =>
CreateDriver(BrowserNames.Edge, () =>
{
var options = new EdgeOptions().SetCommonOptions();
options.SetCommonChromiumOptions(configuration);
configuration.BrowserOptionsConfigurator?.Invoke(options);
var service = EdgeDriverService.CreateDefaultService();
service.SuppressInitialDiagnosticInformation = true;
return new EdgeDriver(service, options).SetCommonTimeouts(pageLoadTimeout);
});
public static FirefoxDriver CreateFirefoxDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout)
{
var options = new FirefoxOptions().SetCommonOptions();
options.SetPreference("intl.accept_languages", configuration.AcceptLanguage.ToString());
// Disabling smooth scrolling to avoid large waiting time when taking full-page screenshots.
options.SetPreference("general.smoothScroll", preferenceValue: false);
// Disabling hardware acceleration to avoid hardware dependent issues in rendering and visual validation.
options.SetPreference("browser.preferences.defaultPerformanceSettings.enabled", preferenceValue: false);
options.SetPreference("layers.acceleration.disabled", preferenceValue: true);
if (configuration.Headless) options.AddArgument("--headless");
configuration.BrowserOptionsConfigurator?.Invoke(options);
return CreateDriver(BrowserNames.Firefox, () => new FirefoxDriver(options).SetCommonTimeouts(pageLoadTimeout));
}
public static InternetExplorerDriver CreateInternetExplorerDriver(BrowserConfiguration configuration, TimeSpan pageLoadTimeout) =>
CreateDriver(BrowserNames.InternetExplorer, () =>
{
var options = new InternetExplorerOptions().SetCommonOptions();
// IE doesn't support this.
options.AcceptInsecureCertificates = false;
configuration.BrowserOptionsConfigurator?.Invoke(options);
return new InternetExplorerDriver(options).SetCommonTimeouts(pageLoadTimeout);
});
private static TDriverOptions SetCommonOptions<TDriverOptions>(this TDriverOptions driverOptions)
where TDriverOptions : DriverOptions
{
driverOptions.AcceptInsecureCertificates = true;
driverOptions.PageLoadStrategy = PageLoadStrategy.Normal;
return driverOptions;
}
private static TDriverOptions SetCommonChromiumOptions<TDriverOptions>(
this TDriverOptions options,
BrowserConfiguration configuration)
where TDriverOptions : ChromiumOptions
{
options.AddArgument("--lang=" + configuration.AcceptLanguage);
// Disabling hardware acceleration to avoid hardware dependent issues in rendering and visual validation.
options.AddArgument("disable-accelerated-2d-canvas");
options.AddArgument("disable-gpu"); // #spell-check-ignore-line
// Setting color profile explicitly to sRGB to keep colors as they are for visual verification testing.
options.AddArgument("force-color-profile=sRGB");
// Disabling DPI scaling.
options.AddArgument("force-device-scale-factor=1");
options.AddArgument("high-dpi-support=1");
// Disabling smooth scrolling to avoid large waiting time when taking full-page screenshots.
options.AddArgument("disable-smooth-scrolling");
if (configuration.FakeVideoSource is not null)
{
var fakeCameraSourceFilePath = configuration.FakeVideoSource.SaveVideoToTempFolder();
options.AddArgument("use-fake-device-for-media-stream");
options.AddArgument("use-fake-ui-for-media-stream");
options.AddArgument($"use-file-for-fake-video-capture={fakeCameraSourceFilePath}");
}
if (configuration.Headless) options.AddArgument("headless");
return options;
}
private static TDriver SetCommonTimeouts<TDriver>(this TDriver driver, TimeSpan pageLoadTimeout)
where TDriver : IWebDriver
{
// Setting timeouts for cases when tests randomly hang up a bit more for some reason (like the test machine load
// momentarily spiking). We're not increasing ImplicitlyWait, the default of which is 0, since that would make
// all tests slower.
// See: https://stackoverflow.com/a/7312740/220230
var timeouts = driver.Manage().Timeouts();
// Default is 5 minutes.
timeouts.PageLoad = pageLoadTimeout;
return driver;
}
private static TDriver CreateDriver<TDriver>(string browserName, Func<TDriver> driverFactory)
where TDriver : IWebDriver
{
try
{
DriverSetup.AutoSetUp(browserName);
return driverFactory();
}
catch (Exception ex)
{
throw new WebDriverException(
$"Creating the web driver failed with the message \"{ex.Message}\". This can mean that there is a " +
$"leftover web driver process that you have to kill manually. Full exception: {ex}",
ex);
}
}
private sealed class ChromeConfiguration
{
public ChromeOptions Options { get; init; }
public ChromeDriverService Service { get; set; }
}
}