Skip to content

Commit 47473aa

Browse files
authored
Merge pull request #289 from csf-dev/craigfowler/issue284
Resolve #284 - Open URL throws if the Url is not absolute
2 parents 5ee5a35 + ec30f24 commit 47473aa

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

CSF.Screenplay.Selenium/Actions/OpenUrl.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ public class OpenUrl : IPerformable, ICanReport
1515
public ValueTask PerformAsAsync(ICanPerform actor, CancellationToken cancellationToken = default)
1616
{
1717
var ability = actor.GetAbility<BrowseTheWeb>();
18+
if(!uri.Uri.IsAbsoluteUri)
19+
throw new InvalidOperationException($"The URL to open must be absolute; have you forgotten to grant {actor} the {nameof(UseABaseUri)} ability?");
1820
ability.WebDriver.Url = uri.Uri.ToString();
1921
return default;
2022
}

Tests/CSF.Screenplay.Selenium.Tests/Actions/OpenUrlTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11

2+
using System;
23
using CSF.Screenplay.Selenium.Elements;
4+
using OpenQA.Selenium;
35
using static CSF.Screenplay.PerformanceStarter;
46
using static CSF.Screenplay.Selenium.PerformableBuilder;
57

@@ -34,4 +36,22 @@ public async Task OpenTheUrlWithDifferentBasePathShouldYieldDifferentContent(ISt
3436

3537
Assert.That(contents, Is.EqualTo("This is content at the deeper path."));
3638
}
39+
40+
[Test, AutoMoqData]
41+
public async Task PerformAsAsyncShouldThrowIfTheUrlIsNotAbsolute(Actor actor,
42+
[MockDriver] BrowseTheWeb ability)
43+
{
44+
actor.IsAbleTo(ability);
45+
var sut = new OpenUrl(new NamedUri("foo/bar/baz.html"));
46+
Assert.That(() => sut.PerformAsAsync(actor), Throws.InstanceOf<InvalidOperationException>());
47+
}
48+
49+
[Test, AutoMoqData]
50+
public async Task PerformAsAsyncShouldNotThrowIfTheUrlIsAbsolute(Actor actor,
51+
[MockDriver] BrowseTheWeb ability)
52+
{
53+
actor.IsAbleTo(ability);
54+
var sut = new OpenUrl(new NamedUri("https://example.com/foo/bar/baz.html"));
55+
Assert.That(() => sut.PerformAsAsync(actor), Throws.Nothing);
56+
}
3757
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Reflection;
2+
using AutoFixture;
3+
using CSF.Extensions.WebDriver;
4+
using CSF.Extensions.WebDriver.Factories;
5+
using Moq;
6+
7+
namespace CSF.Screenplay.Selenium;
8+
9+
public class MockDriverAttribute : CustomizeAttribute
10+
{
11+
public override ICustomization GetCustomization(ParameterInfo parameter)
12+
=> new MockDriverCustomization();
13+
}
14+
15+
public class MockDriverCustomization : ICustomization
16+
{
17+
public void Customize(IFixture fixture)
18+
{
19+
fixture.Customize<IGetsWebDriver>(c => c.FromFactory((WebDriverAndOptions d) => Mock.Of<IGetsWebDriver>(m => m.GetDefaultWebDriver(null) == d
20+
&& m.GetWebDriver(It.IsAny<string>(), null) == d)));
21+
fixture.Customize<BrowseTheWeb>(c => c.FromFactory((IGetsWebDriver d) => new BrowseTheWeb(d)));
22+
}
23+
}

0 commit comments

Comments
 (0)