Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CSF.Screenplay.Selenium/Actions/OpenUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public class OpenUrl : IPerformable, ICanReport
public ValueTask PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
{
var ability = actor.GetAbility<BrowseTheWeb>();
if(!uri.Uri.IsAbsoluteUri)
throw new InvalidOperationException($"The URL to open must be absolute; have you forgotten to grant {actor} the {nameof(UseABaseUri)} ability?");
ability.WebDriver.Url = uri.Uri.ToString();
return default;
}
Expand Down
20 changes: 20 additions & 0 deletions Tests/CSF.Screenplay.Selenium.Tests/Actions/OpenUrlTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

using System;
using CSF.Screenplay.Selenium.Elements;
using OpenQA.Selenium;
using static CSF.Screenplay.PerformanceStarter;
using static CSF.Screenplay.Selenium.PerformableBuilder;

Expand Down Expand Up @@ -34,4 +36,22 @@ public async Task OpenTheUrlWithDifferentBasePathShouldYieldDifferentContent(ISt

Assert.That(contents, Is.EqualTo("This is content at the deeper path."));
}

[Test, AutoMoqData]
public async Task PerformAsAsyncShouldThrowIfTheUrlIsNotAbsolute(Actor actor,
[MockDriver] BrowseTheWeb ability)
{
actor.IsAbleTo(ability);
var sut = new OpenUrl(new NamedUri("foo/bar/baz.html"));
Assert.That(() => sut.PerformAsAsync(actor), Throws.InstanceOf<InvalidOperationException>());
}

[Test, AutoMoqData]
public async Task PerformAsAsyncShouldNotThrowIfTheUrlIsAbsolute(Actor actor,
[MockDriver] BrowseTheWeb ability)
{
actor.IsAbleTo(ability);
var sut = new OpenUrl(new NamedUri("https://example.com/foo/bar/baz.html"));
Assert.That(() => sut.PerformAsAsync(actor), Throws.Nothing);
}
}
23 changes: 23 additions & 0 deletions Tests/CSF.Screenplay.Selenium.Tests/MockDriverAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Reflection;
using AutoFixture;
using CSF.Extensions.WebDriver;
using CSF.Extensions.WebDriver.Factories;
using Moq;

namespace CSF.Screenplay.Selenium;

public class MockDriverAttribute : CustomizeAttribute
{
public override ICustomization GetCustomization(ParameterInfo parameter)
=> new MockDriverCustomization();
}

public class MockDriverCustomization : ICustomization
{
public void Customize(IFixture fixture)
{
fixture.Customize<IGetsWebDriver>(c => c.FromFactory((WebDriverAndOptions d) => Mock.Of<IGetsWebDriver>(m => m.GetDefaultWebDriver(null) == d
&& m.GetWebDriver(It.IsAny<string>(), null) == d)));
fixture.Customize<BrowseTheWeb>(c => c.FromFactory((IGetsWebDriver d) => new BrowseTheWeb(d)));
}
}
Loading