Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dotnet/WebDriver.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebDriver.Safari.Tests", "t
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WebDriver.Support.Tests", "test\support\WebDriver.Support.Tests.csproj", "{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WebDriver.Mock.Tests", "test\mocked\WebDriver.Mock.Tests.csproj", "{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -69,6 +71,10 @@ Global
{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2136C695-2526-45E0-AE1D-68FBBC6A9DE2}.Release|Any CPU.Build.0 = Release|Any CPU
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8FAB9F7-B8F5-46CC-99F9-CF9C0F54AE19}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
1 change: 1 addition & 0 deletions dotnet/test/mocked/GlobalUsings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global using NUnit.Framework;
12 changes: 12 additions & 0 deletions dotnet/test/mocked/MockWebDriver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using OpenQA.Selenium;
using OpenQA.Selenium.Remote;

namespace WebDriver.Mock.Tests;

public class MockWebDriver: OpenQA.Selenium.WebDriver
{
public MockWebDriver(string remoteAddress, ICapabilities capabilities)
: base(new HttpCommandExecutor(new Uri(remoteAddress), TimeSpan.FromSeconds(1)), capabilities)
{
}
}
28 changes: 28 additions & 0 deletions dotnet/test/mocked/WebDriver.Mock.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
<AssemblyName>WebDriver.Mock.Tests</AssemblyName>
<NoWarn>CS1570</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0"/>
<PackageReference Include="Moq" Version="4.13.0" />
<PackageReference Include="NUnit" Version="3.14.0"/>
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0"/>
<PackageReference Include="NUnit.Analyzers" Version="3.10.0"/>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="WireMock.Net" Version="1.5.60" />
<PackageReference Include="WireMock.Net.FluentAssertions" Version="1.5.60" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\webdriver\WebDriver.csproj" />
</ItemGroup>

</Project>
91 changes: 91 additions & 0 deletions dotnet/test/mocked/WebDriverConstructorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System.Net;
using FluentAssertions;
using Moq;
using OpenQA.Selenium;
using WireMock.FluentAssertions;
using WireMock.RequestBuilders;
using WireMock.Server;
using Response = WireMock.ResponseBuilders.Response;

namespace WebDriver.Mock.Tests;

public class WebDriverConstructorTests
{
private WireMockServer mockedServer;
private string mockedServerUrl;

[SetUp]
public void Setup()
{
mockedServer = WireMockServer.Start();
mockedServerUrl = mockedServer.Url!;
}

[TearDown]
public void TearDown()
{
mockedServer.Dispose();
}

[Test]
public void ShouldCreateSessionWhenCreated()
{
mockedServer.Given(Request.Create().WithPath("/session"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new { }));
var capabilities = new RemoteSessionSettings();

_ = new MockWebDriver(mockedServerUrl, capabilities);

mockedServer.Should().HaveReceivedACall()
.UsingPost()
.And.AtUrl($"{mockedServerUrl}/session");
}

[Test]
public void WhenCreateSessionCreatedSetSessionIdAndCapabilities()
{
mockedServer.Given(Request.Create().WithPath("/session"))
.RespondWith(Response.Create()
.WithHeader("Content-Type", "application/json")
.WithBodyAsJson(new
{
sessionId = "1-2-3",
capabilities = new
{
browser = "firefox",
platform = "linux",
}
}));
var capabilities = new RemoteSessionSettings();

var driver = new MockWebDriver(mockedServerUrl, capabilities);

Assert.Multiple(() =>
{
Assert.That(driver.SessionId.ToString(), Is.EqualTo("1-2-3"));
Assert.That(driver.Capabilities["browser"], Is.EqualTo("firefox"));
Assert.That(driver.Capabilities["platform"], Is.EqualTo("linux"));
});
}

[Test]
public void GivenCreateSessionThrowsShouldNotCallQuit()
{
mockedServer.Given(Request.Create().UsingPost().WithPath("/session"))
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.BadGateway));
mockedServer.Given(Request.Create().UsingDelete().WithPath("/session/"))
.RespondWith(Response.Create()
.WithStatusCode(HttpStatusCode.NotFound)
.WithHeader("Content-Type", "application/json")
.WithBody(string.Empty));
var capabilities = new RemoteSessionSettings();

Assert.Throws<WebDriverException>(() => { _ = new MockWebDriver(mockedServerUrl, capabilities); });

mockedServer.Should().HaveReceivedNoCalls()
.UsingDelete();
}
}