diff --git a/dotnet/WebDriver.NET.sln b/dotnet/WebDriver.NET.sln index 945f8ecb8b3b9..4e3203877d631 100644 --- a/dotnet/WebDriver.NET.sln +++ b/dotnet/WebDriver.NET.sln @@ -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 @@ -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 diff --git a/dotnet/test/mocked/GlobalUsings.cs b/dotnet/test/mocked/GlobalUsings.cs new file mode 100644 index 0000000000000..324456763afb8 --- /dev/null +++ b/dotnet/test/mocked/GlobalUsings.cs @@ -0,0 +1 @@ +global using NUnit.Framework; diff --git a/dotnet/test/mocked/MockWebDriver.cs b/dotnet/test/mocked/MockWebDriver.cs new file mode 100644 index 0000000000000..0303161384293 --- /dev/null +++ b/dotnet/test/mocked/MockWebDriver.cs @@ -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) + { + } +} diff --git a/dotnet/test/mocked/WebDriver.Mock.Tests.csproj b/dotnet/test/mocked/WebDriver.Mock.Tests.csproj new file mode 100644 index 0000000000000..459ec04068ee6 --- /dev/null +++ b/dotnet/test/mocked/WebDriver.Mock.Tests.csproj @@ -0,0 +1,28 @@ + + + + net7.0 + enable + enable + false + true + WebDriver.Mock.Tests + CS1570 + + + + + + + + + + + + + + + + + + diff --git a/dotnet/test/mocked/WebDriverConstructorTests.cs b/dotnet/test/mocked/WebDriverConstructorTests.cs new file mode 100644 index 0000000000000..41ca5e00fa83b --- /dev/null +++ b/dotnet/test/mocked/WebDriverConstructorTests.cs @@ -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(() => { _ = new MockWebDriver(mockedServerUrl, capabilities); }); + + mockedServer.Should().HaveReceivedNoCalls() + .UsingDelete(); + } +}