Skip to content

Commit

Permalink
#53 Add Is extension method for IWebDriver
Browse files Browse the repository at this point in the history
  • Loading branch information
YevgeniyShunevych committed Oct 3, 2022
1 parent db7022a commit 87308ea
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
13 changes: 13 additions & 0 deletions src/Atata.WebDriverExtras/Extensions/IWebDriverExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,21 @@ public static TInterface As<TInterface>(this IWebDriver webDriver) =>
? castedWebDriver
: throw new NotSupportedException($"{webDriver.GetType().FullName} doesn't implement {typeof(TInterface).FullName}.");

/// <summary>
/// Determines whether the web driver implements <typeparamref name="TInterface"/>.
/// Considers <see cref="IWrapsDriver"/>.
/// </summary>
/// <typeparam name="TInterface">The type of the interface.</typeparam>
/// <param name="webDriver">The <see cref="IWebDriver"/> instance.</param>
/// <returns>
/// <c>true</c> if [is] [the specified web driver]; otherwise, <c>false</c>.
/// </returns>
public static bool Is<TInterface>(this IWebDriver webDriver) =>
webDriver.TryAs<TInterface>(out _);

/// <summary>
/// Tries to cast the web driver to the specified interface type.
/// Considers <see cref="IWrapsDriver"/>.
/// </summary>
/// <typeparam name="TInterface">The type of the interface.</typeparam>
/// <param name="webDriver">The <see cref="IWebDriver"/> instance.</param>
Expand Down
52 changes: 42 additions & 10 deletions test/Atata.WebDriverExtras.Tests/IWebDriverExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.ObjectModel;

using AssertIs = NUnit.Framework.Is;

namespace Atata.WebDriverExtras.Tests;

public static class IWebDriverExtensionsTests
Expand All @@ -13,7 +15,7 @@ public void WithNull() =>

[Test]
public void HasInterface() =>
Assert.That(Driver.As<IJavaScriptExecutor>(), Is.EqualTo(Driver));
Assert.That(Driver.As<IJavaScriptExecutor>(), AssertIs.EqualTo(Driver));

[Test]
public void MissingInterface() =>
Expand All @@ -24,7 +26,7 @@ public void MissingInterface() =>
public void HasInterfaceInWrappedDriver()
{
using IWebDriver wrapper = new DriverWrapper(Driver);
Assert.That(wrapper.As<IJavaScriptExecutor>(), Is.EqualTo(Driver));
Assert.That(wrapper.As<IJavaScriptExecutor>(), AssertIs.EqualTo(Driver));
}

[Test]
Expand All @@ -36,6 +38,36 @@ public void MissingInterfaceInWrappedDriver()
}
}

public class Is : UITestFixture
{
[Test]
public void WithNull() =>
Assert.Throws<ArgumentNullException>(() =>
((IWebDriver)null).Is<IJavaScriptExecutor>());

[Test]
public void HasInterface() =>
Assert.That(Driver.Is<IJavaScriptExecutor>(), AssertIs.True);

[Test]
public void MissingInterface() =>
Assert.That(Driver.Is<IEquatable<int>>(), AssertIs.False);

[Test]
public void HasInterfaceInWrappedDriver()
{
using IWebDriver wrapper = new DriverWrapper(Driver);
Assert.That(wrapper.Is<IJavaScriptExecutor>(), AssertIs.True);
}

[Test]
public void MissingInterfaceInWrappedDriver()
{
using IWebDriver wrapper = new DriverWrapper(Driver);
Assert.That(wrapper.Is<IEquatable<int>>(), AssertIs.False);
}
}

public class TryAs : UITestFixture
{
[Test]
Expand All @@ -48,17 +80,17 @@ public void HasInterface()
{
bool result = Driver.TryAs<IJavaScriptExecutor>(out var casted);

Assert.That(result, Is.True);
Assert.That(casted, Is.EqualTo(Driver));
Assert.That(result, AssertIs.True);
Assert.That(casted, AssertIs.EqualTo(Driver));
}

[Test]
public void MissingInterface()
{
bool result = Driver.TryAs<IEquatable<int>>(out var casted);

Assert.That(result, Is.False);
Assert.That(casted, Is.Null);
Assert.That(result, AssertIs.False);
Assert.That(casted, AssertIs.Null);
}

[Test]
Expand All @@ -68,8 +100,8 @@ public void HasInterfaceInWrappedDriver()

bool result = Driver.TryAs<IJavaScriptExecutor>(out var casted);

Assert.That(result, Is.True);
Assert.That(casted, Is.EqualTo(Driver));
Assert.That(result, AssertIs.True);
Assert.That(casted, AssertIs.EqualTo(Driver));
}

[Test]
Expand All @@ -79,8 +111,8 @@ public void MissingInterfaceInWrappedDriver()

bool result = wrapper.TryAs<IEquatable<int>>(out var casted);

Assert.That(result, Is.False);
Assert.That(casted, Is.Null);
Assert.That(result, AssertIs.False);
Assert.That(casted, AssertIs.Null);
}
}

Expand Down

0 comments on commit 87308ea

Please sign in to comment.