-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebDriverFactoryIntegrationTests.cs
More file actions
128 lines (111 loc) · 5.22 KB
/
WebDriverFactoryIntegrationTests.cs
File metadata and controls
128 lines (111 loc) · 5.22 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
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using OpenQA.Selenium;
namespace CSF.Extensions.WebDriver.Factories;
[TestFixture,Parallelizable]
public class WebDriverFactoryIntegrationTests
{
[Test]
public void GetDefaultWebDriverShouldThrowAnExceptionBecauseOfAnInvalidUrlScheme()
{
var services = GetServiceProvider();
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
Assert.That(() => driverFactory.GetDefaultWebDriver(), Throws.InstanceOf<NotSupportedException>().And.Message.Contains("'invalid'"));
}
[Test]
public void GetDefaultWebDriverAfterConfigurationCallbackShouldThrowAnExceptionBecauseOfANonsenseUrlScheme()
{
var services = GetServiceProvider(o => o.SelectedConfiguration = "DefaultNonsense");
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
Assert.That(() => driverFactory.GetDefaultWebDriver(), Throws.InstanceOf<NotSupportedException>().And.Message.Contains("'nonsense'"));
}
[Test]
public void GetDefaultWebDriverShouldReturnADriverProxyWithIdentification()
{
var services = GetServiceProvider(o => o.SelectedConfiguration = "DefaultFake");
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
using var driver = driverFactory.GetDefaultWebDriver();
Assert.That(() => driver.WebDriver.GetBrowserId(), Is.Not.Null);
}
[Test]
public void GetWebDriverWithOptionsShouldNotThrow()
{
var services = GetServiceProvider(o => o.SelectedConfiguration = "FakeWithOptions");
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
using var driver = driverFactory.GetDefaultWebDriver();
Assert.That(() => driver.WebDriver.GetBrowserId(), Is.Not.Null);
}
[Test]
public void DriverTypeNorOptionsTypeShouldBeMandatoryIfACustomFactoryTypeIsSpecified()
{
var services = GetServiceProvider(o => o.SelectedConfiguration = "OmittedDriverAndOptionsType");
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
using var driver = driverFactory.GetDefaultWebDriver();
Assert.That(() => driver.WebDriver.GetBrowserId(), Is.Not.Null);
}
[Test]
public void GetWebDriverShouldReturnADriverWithTheCorrectQuirksForChrome()
{
var services = GetServiceProvider(extraRegistrations: services => services.AddWebDriverQuirks(GetCommonBrowserQuirks()));
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
using var driver = driverFactory.GetWebDriver("Chrome");
Assert.Multiple(() =>
{
Assert.That(driver.WebDriver.HasQuirk("IAmChrome"), Is.True, "Chrome quirk");
Assert.That(driver.WebDriver.HasQuirk("IAmFirefox"), Is.False, "Firefox quirk");
});
}
[Test]
public void GetWebDriverShouldReturnADriverWithTheCorrectQuirksForFirefox()
{
var services = GetServiceProvider(extraRegistrations: services => services.AddWebDriverQuirks(GetCommonBrowserQuirks()));
var driverFactory = services.GetRequiredService<IGetsWebDriver>();
using var driver = driverFactory.GetWebDriver("Firefox");
Assert.Multiple(() =>
{
Assert.That(driver.WebDriver.HasQuirk("IAmChrome"), Is.False, "Chrome quirk");
Assert.That(driver.WebDriver.HasQuirk("IAmFirefox"), Is.True, "Firefox quirk");
});
}
Quirks.QuirksData GetCommonBrowserQuirks()
{
return new ()
{
Quirks = new Dictionary<string, Quirks.BrowserInfoCollection>
{
{ "IAmChrome", new () { AffectedBrowsers = new HashSet<Quirks.BrowserInfo>() { new () { Name = "chrome" } } } },
{ "IAmFirefox", new () { AffectedBrowsers = new HashSet<Quirks.BrowserInfo>() { new () { Name = "firefox" } } } },
}
};
}
IServiceProvider GetServiceProvider(Action<WebDriverCreationOptionsCollection>? configureOptions = null,
Action<IServiceCollection>? extraRegistrations = null)
{
var services = new ServiceCollection();
services.AddSingleton(GetConfiguration());
services.AddWebDriverFactory(configureOptions: configureOptions);
extraRegistrations?.Invoke(services);
services.AddLogging();
return services.BuildServiceProvider();
}
IConfiguration GetConfiguration()
{
return new ConfigurationBuilder()
.AddJsonFile("appsettings.WebDriverFactoryIntegrationTests.json")
.Build();
}
public class FakeWebDriverFactory : ICreatesWebDriverFromOptions
{
public WebDriverAndOptions GetWebDriver(WebDriverCreationOptions options, Action<DriverOptions>? supplementaryConfiguration = null)
{
return new(Mock.Of<IWebDriver>(), Mock.Of<DriverOptions>());
}
}
public class FakeOptionsUsingWebDriverFactory : ICreatesWebDriverFromOptions
{
public WebDriverAndOptions GetWebDriver(WebDriverCreationOptions options, Action<DriverOptions>? supplementaryConfiguration = null)
{
return new(Mock.Of<IWebDriver>(), options.OptionsFactory());
}
}
}