-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDriverCreationConfigureOptionsTests.cs
More file actions
249 lines (222 loc) · 10.5 KB
/
WebDriverCreationConfigureOptionsTests.cs
File metadata and controls
249 lines (222 loc) · 10.5 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Safari;
namespace CSF.Extensions.WebDriver.Factories;
[TestFixture,Parallelizable]
public class WebDriverCreationConfigureOptionsTests
{
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToSetupLocalChromeDriverWithSimpleOptionsFromJsonConfiguration([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": {
""DriverType"": ""ChromeDriver"",
""Options"": {
""BinaryLocation"": ""C:\\SomePath\\Chrome\\GoogleChrome.exe""
}
}
}
}");
Assert.Multiple(() =>
{
Assert.That(options.DriverConfigurations, Has.Count.EqualTo(1), "Count of driver configurations should be 1");
Assert.That(options.DriverConfigurations, Contains.Key("Test"), "Options contains item with correct key");
var hasDriverConfig = options.DriverConfigurations.TryGetValue("Test", out var driverConfig);
if(!hasDriverConfig)
{
Assert.Fail("The driver configuration should be present");
return;
}
Assert.That(driverConfig?.DriverType, Is.EqualTo("ChromeDriver"), "Driver config should have correct driver type name");
Assert.That(driverConfig?.OptionsFactory(), Is.InstanceOf<ChromeOptions>(), "Driver config should use correct options type");
#pragma warning disable NUnit2022 // Missing property required for constraint: Really, we are using the subclass ChromeOptions, which does have that property
Assert.That(driverConfig?.OptionsFactory(),
Has.Property(nameof(ChromeOptions.BinaryLocation)).EqualTo(@"C:\SomePath\Chrome\GoogleChrome.exe"),
"Driver config has options with correct binary location");
#pragma warning restore NUnit2022 // Missing property required for constraint
});
}
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToSetupTwoLocalDriversWithSimpleOptionsFromJsonConfiguration([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""SampleChrome"": {
""DriverType"": ""ChromeDriver"",
""Options"": {
""BinaryLocation"": ""C:\\SomePath\\Chrome\\GoogleChrome.exe""
}
},
""SampleFirefox"": {
""DriverType"": ""FirefoxDriver"",
""Options"": {
""EnableDevToolsProtocol"": true
}
},
}
}");
Assert.Multiple(() =>
{
Assert.That(options.DriverConfigurations, Has.Count.EqualTo(2), "Count of driver configurations should be 2");
Assert.That(options.DriverConfigurations, Contains.Key("SampleChrome"), "Options contains item for Chrome");
Assert.That(options.DriverConfigurations, Contains.Key("SampleFirefox"), "Options contains item for Firefox");
var hasDriverConfig = options.DriverConfigurations.TryGetValue("SampleFirefox", out var driverConfig);
if(!hasDriverConfig)
{
Assert.Fail("The driver configuration for Firefox should be present");
return;
}
#pragma warning disable NUnit2022 // Missing property required for constraint: Really, we are using the subclass FirefoxOptions, which does have that property
Assert.That(driverConfig?.OptionsFactory(),
Has.Property(nameof(FirefoxOptions.EnableDevToolsProtocol)).True,
"Driver config has options with correct dev tools protocol setting");
#pragma warning restore NUnit2022 // Missing property required for constraint
});
}
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToSetupLocalChromeDriverWithNoOptionsFromJsonConfiguration([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""ChromeDriver"" }
}
}");
Assert.Multiple(() =>
{
var hasDriverConfig = options.DriverConfigurations.TryGetValue("Test", out var driverConfig);
if(!hasDriverConfig)
{
Assert.Fail("The driver configuration should be present");
return;
}
Assert.That(driverConfig?.OptionsFactory(), Is.InstanceOf<ChromeOptions>(), "Driver config should use correct options type");
Assert.That(driverConfig?.OptionsFactory(), Is.Not.Null, "Driver config should not be null");
});
}
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToGetSelectedConfigWhenThereIsOnlyOnePresent([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""ChromeDriver"" }
}
}");
Assert.That(options.GetSelectedConfiguration()?.DriverType, Is.EqualTo("ChromeDriver"));
}
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToAddACustomizerToSomeOptions([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""ChromeDriver"", ""OptionsCustomizerType"": ""CSF.Extensions.WebDriver.Factories.SampleCustomizer, CSF.Extensions.WebDriver.Tests"" }
}
}");
Assert.That(options.GetSelectedConfiguration()?.OptionsCustomizer, Is.InstanceOf<SampleCustomizer>());
}
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToGetSelectedConfigWhenASelectedConfigIsNamed([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""ChromeDriver"" },
""Test2"": { ""DriverType"": ""FirefoxDriver"" }
},
""SelectedConfiguration"": ""Test2""
}");
Assert.That(options.GetSelectedConfiguration()?.DriverType, Is.EqualTo("FirefoxDriver"));
}
[Test,AutoMoqData]
public async Task ConfigureShouldProvideThrowWhenThereAreTwoConfigsAndNoExplicitSelection([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""ChromeDriver"" },
""Test2"": { ""DriverType"": ""FirefoxDriver"" }
}
}");
Assert.That(() => options.GetSelectedConfiguration(), Throws.InvalidOperationException);
}
[Test,AutoMoqData]
public async Task ConfigureShouldNotThrowForANonsenseDriverType([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""NonexistentDriver"" }
}
}");
Assert.That(options.DriverConfigurations, Is.Empty);
}
[Test,AutoMoqData]
public async Task ConfigureShouldNotThrowForARemoteDriverWithoutOptionsType([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider)
{
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""RemoteWebDriver"" }
}
}");
Assert.That(options.DriverConfigurations, Is.Empty);
}
[Test,AutoMoqData]
public async Task ConfigureShouldBeAbleToSetupRemoteDriverWithSimpleOptionsFromJsonConfiguration([StandardTypes] IGetsWebDriverAndOptionsTypes typeProvider,
[TestLogger] ILogger<WebDriverCreationConfigureOptions> logger)
{
Mock.Get(typeProvider)
.Setup(x => x.GetWebDriverOptionsType(typeof(RemoteWebDriver), "SafariOptions"))
.Returns(typeof(SafariOptions));
var options = await GetOptionsAsync(typeProvider,
@"{
""DriverConfigurations"": {
""Test"": { ""DriverType"": ""RemoteWebDriver"", ""OptionsType"": ""SafariOptions"" }
}
}", logger: logger);
Assert.That(options.DriverConfigurations, Is.Not.Empty);
}
/// <summary>
/// Helper method to create an <see cref="IConfiguration"/> from a specified JSON string.
/// </summary>
/// <param name="jsonConfig">A JSON string which will be used as the basis for the returned config.</param>
/// <returns>A task exposing a configuration object, created from the JSON string.</returns>
static async Task<IConfiguration> GetConfigurationAsync(string jsonConfig)
{
var builder = new ConfigurationBuilder();
var stream = new MemoryStream ();
using var writer = new StreamWriter(stream, leaveOpen: true);
await writer.WriteAsync(jsonConfig);
await writer.FlushAsync();
stream.Position = 0;
builder.AddJsonStream(stream);
return builder.Build();
}
/// <summary>
/// Creates and exercises <see cref="WebDriverCreationConfigureOptions"/> in order to create a new
/// <see cref="WebDriverCreationOptionsCollection"/> from a specified JSON config.
/// </summary>
/// <param name="typeProvider">The type provider for web driver and options types</param>
/// <param name="json">The JSON config from which to create the options</param>
/// <returns>A task exposing the webdriver creation options collection, configured by the SUT</returns>
static async Task<WebDriverCreationOptionsCollection> GetOptionsAsync(IGetsWebDriverAndOptionsTypes typeProvider,
string json,
ILogger<WebDriverCreationConfigureOptions>? logger = null)
{
var options = new WebDriverCreationOptionsCollection();
var config = await GetConfigurationAsync(json);
var sut = new WebDriverCreationConfigureOptions(new WebDriverConfigurationItemParser(typeProvider, Mock.Of<ILogger<WebDriverConfigurationItemParser>>()),
config,
logger ?? Mock.Of<ILogger<WebDriverCreationConfigureOptions>>());
sut.Configure(options);
return options;
}
}