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
15 changes: 15 additions & 0 deletions src/Playwright.Nunit/Playwright.Nunit.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<AssemblyName>Microsoft.Playwright.Nunit</AssemblyName>
<RootNamespace>Microsoft.Playwright.Nunit</RootNamespace>
</PropertyGroup>
<Import Project="../Common/SignAssembly.props" />
<ItemGroup>
<PackageReference Include="NUnit" Version="4.1.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Playwright\Playwright.csproj" />
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions src/Playwright.Nunit/PlaywrightTestAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;

namespace Microsoft.Playwright.Nunit
{
/// <summary>
/// Enables decorating test facts with information about the corresponding test in the upstream repository.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
public class PlaywrightTestAttribute : Attribute
{
/// <summary>
/// Gets the product based on the PRODUCT environment variable.
/// </summary>
public static string Product => string.IsNullOrEmpty(Environment.GetEnvironmentVariable("PRODUCT"))
? "CHROMIUM"
: Environment.GetEnvironmentVariable("PRODUCT");

/// <summary>
/// Gets a value indicating whether we are testing Chromium.
/// </summary>
public static bool IsChromium => Product.Equals("CHROMIUM", StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Gets a value indicating whether we are testing Firefox.
/// </summary>
public static bool IsFirefox => Product.Equals("FIREFOX", StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Gets a value indicating whether we are testing WebKit.
/// </summary>
public static bool IsWebkit => Product.Equals("WEBKIT", StringComparison.OrdinalIgnoreCase);

/// <summary>
/// Creates a new instance of the attribute.
/// </summary>
/// <param name="fileName"><see cref="FileName"/></param>
/// <param name="nameOfTest"><see cref="TestName"/></param>
public PlaywrightTestAttribute(string fileName, string nameOfTest)
{
FileName = fileName;
TestName = nameOfTest;
}

/// <summary>
/// Creates a new instance of the attribute.
/// </summary>
/// <param name="fileName"><see cref="FileName"/></param>
/// <param name="describe"><see cref="Describe"/></param>
/// <param name="nameOfTest"><see cref="TestName"/></param>
public PlaywrightTestAttribute(string fileName, string describe, string nameOfTest) : this(fileName, nameOfTest)
{
Describe = describe;
}

/// <summary>
/// The file name origin of the test.
/// </summary>
public string FileName { get; }

/// <summary>
/// Returns the trimmed file name.
/// </summary>
public string TrimmedName => FileName.Substring(0, FileName.IndexOf('.'));

/// <summary>
/// The name of the test, the decorated code is based on.
/// </summary>
public string TestName { get; }

/// <summary>
/// The describe of the test, the decorated code is based on, if one exists.
/// </summary>
public string Describe { get; }
}
}
36 changes: 30 additions & 6 deletions src/Playwright.Tests/Attributes/SkipBrowserAndPlatformFact.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using System.Runtime.InteropServices;
using Xunit;
using System;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using OSPlatform = System.Runtime.InteropServices.OSPlatform;
using RuntimeInformation = System.Runtime.InteropServices.RuntimeInformation;

namespace Microsoft.Playwright.Tests.Attributes
{
/// <summary>
/// Skip browsers and/or platforms
/// </summary>
public class SkipBrowserAndPlatformFact : FactAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
public class SkipBrowserAndPlatformFact : NUnitAttribute, IApplyToTest
{
private readonly bool _skipFirefox;
private readonly bool _skipChromium;
private readonly bool _skipWebkit;
private readonly bool _skipOSX;
private readonly bool _skipWindows;
private readonly bool _skipLinux;

/// <summary>
/// Creates a new <seealso cref="SkipBrowserAndPlatformFact"/>
/// </summary>
Expand All @@ -25,14 +37,26 @@ public SkipBrowserAndPlatformFact(
bool skipWindows = false,
bool skipLinux = false)
{
Timeout = TestConstants.DefaultTestTimeout;
_skipFirefox = skipFirefox;
_skipChromium = skipChromium;
_skipWebkit = skipWebkit;
_skipOSX = skipOSX;
_skipWindows = skipWindows;
_skipLinux = skipLinux;
}

if (SkipBrowser(skipFirefox, skipChromium, skipWebkit) && SkipPlatform(skipOSX, skipWindows, skipLinux))
public void ApplyToTest(Test test)
{
if (ShouldSkip())
{
Skip = "Skipped by browser/platform";
test.RunState = RunState.Ignored;
test.Properties.Set(PropertyNames.SkipReason, "Skipped by browser/platform");
}
}

private bool ShouldSkip()
=> SkipBrowser(_skipFirefox, _skipChromium, _skipWebkit) && SkipPlatform(_skipOSX, _skipWindows, _skipLinux);

private static bool SkipPlatform(bool skipOSX, bool skipWindows, bool skipLinux)
=>
(
Expand Down
52 changes: 4 additions & 48 deletions src/Playwright.Tests/BaseTests/PlaywrightSharpBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.Playwright;
using Microsoft.Playwright.Testing.Xunit;
using Microsoft.Playwright.Tests.Helpers;
using Microsoft.Playwright.Tests.TestServer;
using Microsoft.Playwright.Transport.Channels;
using Xunit.Abstractions;
using NUnit.Framework;

namespace Microsoft.Playwright.Tests.BaseTests
{
/// <summary>
/// This base tests setup logging and http servers
/// </summary>
public class PlaywrightSharpBaseTest : IDisposable
public class PlaywrightSharpBaseTest
{
private readonly XunitLoggerProvider _loggerProvider;
private readonly ILogger<SimpleServer> _httpLogger;

internal IPlaywright Playwright => PlaywrightSharpBrowserLoaderFixture.Playwright;
internal string BaseDirectory { get; set; }
internal IBrowserType BrowserType => Playwright[TestConstants.Product];

internal SimpleServer Server => PlaywrightSharpLoader.Server;
internal SimpleServer HttpsServer => PlaywrightSharpLoader.HttpsServer;

internal PlaywrightSharpBaseTest(ITestOutputHelper output)
[SetUp]
public void BaseSetUp()
{
BaseDirectory = Path.Combine(Directory.GetCurrentDirectory(), "workspace");
var dirInfo = new DirectoryInfo(BaseDirectory);
Expand All @@ -37,42 +27,8 @@ internal PlaywrightSharpBaseTest(ITestOutputHelper output)
dirInfo.Create();
}

Initialize();

_loggerProvider = new XunitLoggerProvider(output);
_httpLogger = TestConstants.LoggerFactory.CreateLogger<SimpleServer>();
TestConstants.LoggerFactory.AddProvider(_loggerProvider);
Server.RequestReceived += Server_RequestReceived;
HttpsServer.RequestReceived += Server_RequestReceived;

output.WriteLine($"Running {GetDisplayName(output)}");
}

private void Server_RequestReceived(object sender, RequestReceivedEventArgs e)
{
_httpLogger.LogInformation($"Incoming request: {e.Request.Path}");
}

private static string GetDisplayName(ITestOutputHelper output)
{
var type = output.GetType();
var testMember = type.GetField("test", BindingFlags.Instance | BindingFlags.NonPublic);
var test = (ITest)testMember.GetValue(output);
return test.DisplayName;
}

internal void Initialize()
{
Server.Reset();
HttpsServer.Reset();
}

/// <inheritdoc/>
public virtual void Dispose()
{
Server.RequestReceived -= Server_RequestReceived;
HttpsServer.RequestReceived -= Server_RequestReceived;
_loggerProvider.Dispose();
}
}
}
11 changes: 1 addition & 10 deletions src/Playwright.Tests/BaseTests/PlaywrightSharpBrowserBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;

namespace Microsoft.Playwright.Tests.BaseTests
{
/// <summary>
/// Based on <see cref="PlaywrightSharpBaseTest"/>, this base class also creates a new Browser
/// Based on <see cref="PlaywrightSharpBaseTest"/>, this base class also provides access to the Browser.
/// </summary>
public class PlaywrightSharpBrowserBaseTest : PlaywrightSharpBaseTest
{
internal IBrowser Browser => PlaywrightSharpBrowserLoaderFixture.Browser;

internal PlaywrightSharpBrowserBaseTest(ITestOutputHelper output) : base(output)
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.Playwright.Helpers;
using Xunit;
using Xunit.Abstractions;
using NUnit.Framework;

namespace Microsoft.Playwright.Tests.BaseTests
{
/// <summary>
/// Based on <see cref="PlaywrightSharpBrowserBaseTest"/>, this calss creates a new <see cref="IBrowserContext"/>
/// Based on <see cref="PlaywrightSharpBrowserBaseTest"/>, this class creates a new <see cref="IBrowserContext"/>
/// </summary>
public class PlaywrightSharpBrowserContextBaseTest : PlaywrightSharpBrowserBaseTest, IAsyncLifetime
public class PlaywrightSharpBrowserContextBaseTest : PlaywrightSharpBrowserBaseTest
{
internal PlaywrightSharpBrowserContextBaseTest(ITestOutputHelper output) : base(output)
{
}

internal IBrowserContext Context { get; set; }

/// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
public virtual async Task DisposeAsync()
[SetUp]
public virtual async Task ContextSetUp()
{
await Context.CloseAsync();
Context = await Browser.NewContextAsync();
Context.DefaultTimeout = TestConstants.DefaultPuppeteerTimeout;
}

/// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
public virtual async Task InitializeAsync()
[TearDown]
public virtual async Task ContextTearDown()
{
Context = await Browser.NewContextAsync();
Context.DefaultTimeout = TestConstants.DefaultPuppeteerTimeout;
await Context.CloseAsync();
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
using System;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Playwright.Tests.BaseTests
{
/// <summary>
/// This class setup a single browser instance for tests.
/// </summary>
public class PlaywrightSharpBrowserLoaderFixture : IAsyncLifetime
public class PlaywrightSharpBrowserLoaderFixture
{
internal static IPlaywright Playwright { get; private set; }

internal static IBrowser Browser { get; private set; }

/// <inheritdoc/>
public Task InitializeAsync() => LaunchBrowserAsync();

/// <inheritdoc/>
public Task DisposeAsync() => ShutDownAsync();

private static async Task LaunchBrowserAsync()
internal static async Task LaunchBrowserAsync()
{
try
{
Expand Down
18 changes: 6 additions & 12 deletions src/Playwright.Tests/BaseTests/PlaywrightSharpPageBaseTest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2020 Darío Kondratiuk
* Copyright (c) 2020 Dario Kondratiuk
* Copyright (c) 2020 Meir Blachman
* Modifications copyright (c) Microsoft Corporation.
*
Expand All @@ -23,31 +23,25 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Threading.Tasks;
using Xunit;
using Xunit.Abstractions;
using NUnit.Framework;

namespace Microsoft.Playwright.Tests.BaseTests
{
/// <summary>
/// Based on <see cref="PlaywrightSharpBrowserBaseTest"/>, this class will create a new Page.
/// Based on <see cref="PlaywrightSharpBrowserContextBaseTest"/>, this class will create a new Page.
/// </summary>
public class PlaywrightSharpPageBaseTest : PlaywrightSharpBrowserContextBaseTest
{
internal PlaywrightSharpPageBaseTest(ITestOutputHelper output) : base(output)
{
}

/// <summary>
/// Gets or sets the Page.
/// </summary>
protected IPage Page { get; set; }

/// <inheritdoc cref="IAsyncLifetime.InitializeAsync"/>
public override async Task InitializeAsync()
/// <inheritdoc/>
public override async Task ContextSetUp()
{
await base.InitializeAsync();
await base.ContextSetUp();
Page = await Context.NewPageAsync();
}

Expand Down
Loading
Loading