Skip to content

Commit 6efcb92

Browse files
committed
[dotnet] ability to create tests using mocked driver
Currently tests are using real browser drivers, but we can use a library like wiremock to mock the driver responses and create tests that don't need a real driver
1 parent 785914e commit 6efcb92

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

dotnet/WebDriver.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebDriver.Safari.Tests", "t
2323
EndProject
2424
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebDriver.Support.Tests", "test\support\WebDriver.Support.Tests.csproj", "{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebDriver.Mock.Tests", "test\mocked\WebDriver.Mock.Tests.csproj", "{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,10 @@ Global
6971
{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
7072
{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
7173
{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Release|Any CPU.ActiveCfg = Release|Any CPU
77+
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Release|Any CPU.Build.0 = Release|Any CPU
7278
EndGlobalSection
7379
GlobalSection(SolutionProperties) = preSolution
7480
HideSolutionNode = FALSE

dotnet/test/mocked/GlobalUsings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
global using NUnit.Framework;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using OpenQA.Selenium;
2+
using OpenQA.Selenium.Remote;
3+
4+
namespace WebDriver.Mock.Tests;
5+
6+
public class MockWebDriver: OpenQA.Selenium.WebDriver
7+
{
8+
public MockWebDriver(string remoteAddress, ICapabilities capabilities)
9+
: base(new HttpCommandExecutor(new Uri(remoteAddress), TimeSpan.FromSeconds(1)), capabilities)
10+
{
11+
}
12+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net7.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
<IsPackable>false</IsPackable>
8+
<IsTestProject>true</IsTestProject>
9+
<AssemblyName>WebDriver.Mock.Tests</AssemblyName>
10+
<NoWarn>CS1570</NoWarn>
11+
</PropertyGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/>
15+
<PackageReference Include="Moq" Version="4.13.0" />
16+
<PackageReference Include="NUnit" Version="3.14.0"/>
17+
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
18+
<PackageReference Include="NUnit.Analyzers" Version="3.10.0"/>
19+
<PackageReference Include="FluentAssertions" Version="6.12.0" />
20+
<PackageReference Include="WireMock.Net" Version="1.5.60" />
21+
<PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.60" />
22+
</ItemGroup>
23+
24+
<ItemGroup>
25+
<ProjectReference Include="..\..\src\webdriver\WebDriver.csproj" />
26+
</ItemGroup>
27+
28+
</Project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System.Net;
2+
using FluentAssertions;
3+
using Moq;
4+
using OpenQA.Selenium;
5+
using WireMock.FluentAssertions;
6+
using WireMock.RequestBuilders;
7+
using WireMock.Server;
8+
using Response = WireMock.ResponseBuilders.Response;
9+
10+
namespace WebDriver.Mock.Tests;
11+
12+
public class WebDriverConstructorTests
13+
{
14+
private WireMockServer mockedServer;
15+
private string mockedServerUrl;
16+
17+
[SetUp]
18+
public void Setup()
19+
{
20+
mockedServer = WireMockServer.Start();
21+
mockedServerUrl = mockedServer.Url!;
22+
}
23+
24+
[TearDown]
25+
public void TearDown()
26+
{
27+
mockedServer.Dispose();
28+
}
29+
30+
[Test]
31+
public void ShouldCreateSessionWhenCreated()
32+
{
33+
mockedServer.Given(Request.Create().WithPath("/session"))
34+
.RespondWith(Response.Create()
35+
.WithHeader("Content-Type", "application/json")
36+
.WithBodyAsJson(new { }));
37+
var capabilities = new RemoteSessionSettings();
38+
39+
_ = new MockWebDriver(mockedServerUrl, capabilities);
40+
41+
mockedServer.Should().HaveReceivedACall()
42+
.UsingPost()
43+
.And.AtUrl($"{mockedServerUrl}/session");
44+
}
45+
46+
[Test]
47+
public void WhenCreateSessionCreatedSetSessionIdAndCapabilities()
48+
{
49+
mockedServer.Given(Request.Create().WithPath("/session"))
50+
.RespondWith(Response.Create()
51+
.WithHeader("Content-Type", "application/json")
52+
.WithBodyAsJson(new
53+
{
54+
sessionId = "1-2-3",
55+
capabilities = new
56+
{
57+
browser = "firefox",
58+
platform = "linux",
59+
}
60+
}));
61+
var capabilities = new RemoteSessionSettings();
62+
63+
var driver = new MockWebDriver(mockedServerUrl, capabilities);
64+
65+
Assert.Multiple(() =>
66+
{
67+
Assert.That(driver.SessionId.ToString(), Is.EqualTo("1-2-3"));
68+
Assert.That(driver.Capabilities["browser"], Is.EqualTo("firefox"));
69+
Assert.That(driver.Capabilities["platform"], Is.EqualTo("linux"));
70+
});
71+
}
72+
73+
[Test]
74+
public void GivenCreateSessionThrowsShouldNotCallQuit()
75+
{
76+
mockedServer.Given(Request.Create().UsingPost().WithPath("/session"))
77+
.RespondWith(Response.Create()
78+
.WithStatusCode(HttpStatusCode.BadGateway));
79+
mockedServer.Given(Request.Create().UsingDelete().WithPath("/session/"))
80+
.RespondWith(Response.Create()
81+
.WithStatusCode(HttpStatusCode.NotFound)
82+
.WithHeader("Content-Type", "application/json")
83+
.WithBody(string.Empty));
84+
var capabilities = new RemoteSessionSettings();
85+
86+
Assert.Throws<WebDriverException>(() => { _ = new MockWebDriver(mockedServerUrl, capabilities); });
87+
88+
mockedServer.Should().HaveReceivedNoCalls()
89+
.UsingDelete();
90+
}
91+
}

0 commit comments

Comments
 (0)