diff --git a/README.md b/README.md index a3cd52a..7b0ae5e 100644 --- a/README.md +++ b/README.md @@ -52,26 +52,26 @@ You can checkout this Github repository or you can use the NuGet package: **Install using the command line from the Package Manager:** ```bash -Install-Package SoloX.CodeQuality.Prod -version 2.3.0 +Install-Package SoloX.CodeQuality.Prod -version 2.3.1 or -Install-Package SoloX.CodeQuality.Test -version 2.3.0 +Install-Package SoloX.CodeQuality.Test -version 2.3.1 ``` **Install using the .Net CLI:** ```bash -dotnet add package SoloX.CodeQuality.Prod --version 2.3.0 +dotnet add package SoloX.CodeQuality.Prod --version 2.3.1 or -dotnet add package SoloX.CodeQuality.Test --version 2.3.0 +dotnet add package SoloX.CodeQuality.Test --version 2.3.1 ``` **Install editing your project file (csproj):** ```xml - + all runtime; build; native; contentfiles; analyzers or - + all runtime; build; native; contentfiles; analyzers @@ -218,17 +218,17 @@ You can checkout this Github repository or use the NuGet package: **Install using the command line from the Package Manager:** ```bash -Install-Package SoloX.CodeQuality.Playwright -version 2.3.0 +Install-Package SoloX.CodeQuality.Playwright -version 2.3.1 ``` **Install using the .Net CLI:** ```bash -dotnet add package SoloX.CodeQuality.Playwright --version 2.3.0 +dotnet add package SoloX.CodeQuality.Playwright --version 2.3.1 ``` **Install editing your project file (csproj):** ```xml - + ``` * * * @@ -406,6 +406,10 @@ await playwrightTest.GotoPageAsync("index.html", async (page) => > Note: you can check on the [Playwright](https://playwright.dev/dotnet/) web site to find out more about the > [Codegen Tool](https://playwright.dev/dotnet/docs/codegen-intro). +> Note: you can also specify the [Playwright device](https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json) +> name in the BuildAsync method to simulate browser behavior for a specific device such as desktop, tablet and mobile +> devices. + * * * ## Test Helpers @@ -420,29 +424,29 @@ You can checkout this Github repository or you can use the NuGet package: **Install using the command line from the Package Manager:** ```bash -Install-Package SoloX.CodeQuality.Test.Helpers -version 2.3.0 +Install-Package SoloX.CodeQuality.Test.Helpers -version 2.3.1 -Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 2.3.0 +Install-Package SoloX.CodeQuality.Test.Helpers.XUnit -version 2.3.1 -Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 2.3.0 +Install-Package SoloX.CodeQuality.Test.Helpers.NUnit -version 2.3.1 ``` **Install using the .Net CLI:** ```bash -dotnet add package SoloX.CodeQuality.Test.Helpers --version 2.3.0 +dotnet add package SoloX.CodeQuality.Test.Helpers --version 2.3.1 -dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 2.3.0 +dotnet add package SoloX.CodeQuality.Test.Helpers.XUnit --version 2.3.1 -dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 2.3.0 +dotnet add package SoloX.CodeQuality.Test.Helpers.NUnit --version 2.3.1 ``` **Install editing your project file (csproj):** ```xml - + - + - + ``` * * * diff --git a/src/SharedProperties.props b/src/SharedProperties.props index de50aa6..e933d94 100644 --- a/src/SharedProperties.props +++ b/src/SharedProperties.props @@ -1,7 +1,7 @@ - 2.3.0 + 2.3.1 Xavier Solau Copyright © 2021 Xavier Solau MIT diff --git a/src/libs/SoloX.CodeQuality.Playwright/Devices.cs b/src/libs/SoloX.CodeQuality.Playwright/Devices.cs new file mode 100644 index 0000000..4175632 --- /dev/null +++ b/src/libs/SoloX.CodeQuality.Playwright/Devices.cs @@ -0,0 +1,54 @@ +// ---------------------------------------------------------------------- +// +// Copyright © 2021 Xavier Solau. +// Licensed under the MIT license. +// See LICENSE file in the project root for full license information. +// +// ---------------------------------------------------------------------- + +namespace SoloX.CodeQuality.Playwright +{ + /// + /// Some Playwright devices. + /// For more information see https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json + /// + public static class Devices + { + /// + /// Safari on desktop. + /// + public const string DesktopSafari = "Desktop Safari"; + /// + /// Chrome on desktop. + /// + public const string DesktopChrome = "Desktop Chrome"; + /// + /// Edge on desktop. + /// + public const string DesktopEdge = "Desktop Edge"; + /// + /// Firefox on desktop. + /// + public const string DesktopFirefox = "Desktop Firefox"; + + /// + /// Pixel. + /// + public const string Pixel = "Pixel 7"; + /// + /// Pixel landscape. + /// + public const string PixelLandscape = "Pixel 7 landscape"; + +#pragma warning disable IDE1006 // Naming Styles + /// + /// Pixel. + /// + public const string iPhone = "iPhone 15"; + /// + /// Pixel landscape. + /// + public const string iPhoneLandscape = "iPhone 15 landscape"; +#pragma warning restore IDE1006 // Naming Styles + } +} diff --git a/src/libs/SoloX.CodeQuality.Playwright/IPlaywrightTestBuilder.cs b/src/libs/SoloX.CodeQuality.Playwright/IPlaywrightTestBuilder.cs index 27b50c4..72363d9 100644 --- a/src/libs/SoloX.CodeQuality.Playwright/IPlaywrightTestBuilder.cs +++ b/src/libs/SoloX.CodeQuality.Playwright/IPlaywrightTestBuilder.cs @@ -70,7 +70,8 @@ public interface IPlaywrightTestBuilder /// Build and set up Web host and Playwright test driver. /// /// Browser override (optional). + /// Device name to use (optional). /// The Playwright test. - Task BuildAsync(Browser? browser = null); + Task BuildAsync(Browser? browser = null, string? deviceName = null); } } diff --git a/src/libs/SoloX.CodeQuality.Playwright/PlaywrightDriver.cs b/src/libs/SoloX.CodeQuality.Playwright/PlaywrightDriver.cs index 4ba67d8..2e9f966 100644 --- a/src/libs/SoloX.CodeQuality.Playwright/PlaywrightDriver.cs +++ b/src/libs/SoloX.CodeQuality.Playwright/PlaywrightDriver.cs @@ -23,7 +23,7 @@ public class PlaywrightDriver : IAsyncDisposable private static readonly object LockSync = new object(); private static bool playwrightInstalled; - private readonly BrowserNewContextOptions? browserNewContextOptions; + private BrowserNewContextOptions? browserNewContextOptions; private readonly TracingStartOptions? tracingStartOptions; private readonly int goToPageRetryCount; @@ -49,12 +49,10 @@ public class PlaywrightDriver : IAsyncDisposable /// Build PlaywrightDriver instance. /// /// Goto page retry count. - /// New context options. /// Tracing start options. - public PlaywrightDriver(int goToPageRetryCount = 3, BrowserNewContextOptions? browserNewContextOptions = null, TracingStartOptions? tracingStartOptions = null) + public PlaywrightDriver(int goToPageRetryCount = 3, TracingStartOptions? tracingStartOptions = null) { this.goToPageRetryCount = goToPageRetryCount; - this.browserNewContextOptions = browserNewContextOptions; this.tracingStartOptions = tracingStartOptions; } @@ -162,6 +160,26 @@ private static void InstallPlaywrightBin() } } + /// + /// Setup BrowserNewContextOptions for the given device name and/or the given builder. + /// + /// Device name to get the options from. + /// BrowserNewContextOptions builder. + public void SetupBrowserNewContextOptions(string? deviceName = null, Action? browserNewContextOptionsBuilder = null) + { + BrowserNewContextOptions options; + if (string.IsNullOrEmpty(deviceName) || !Playwright.Devices.TryGetValue(deviceName, out options)) + { + options = new BrowserNewContextOptions(); + } + + options.IgnoreHTTPSErrors = true; + + browserNewContextOptionsBuilder?.Invoke(options); + + this.browserNewContextOptions = options; + } + /// /// Open a Browser page and navigate to the given URL before applying the given test handler. /// diff --git a/src/libs/SoloX.CodeQuality.Playwright/PlaywrightTestBuilder.cs b/src/libs/SoloX.CodeQuality.Playwright/PlaywrightTestBuilder.cs index e992d38..2539dd8 100644 --- a/src/libs/SoloX.CodeQuality.Playwright/PlaywrightTestBuilder.cs +++ b/src/libs/SoloX.CodeQuality.Playwright/PlaywrightTestBuilder.cs @@ -50,14 +50,10 @@ private sealed class PlaywrightTestBuilderInternal : IPlaywrightTestBuilder, IPl private Action traceFileOptionsBuilder = options => { }; private Action browserNewContextOptionsBuilder = options => { }; - public async Task BuildAsync(Browser? browser = null) + public async Task BuildAsync(Browser? browser = null, string? deviceName = null) { var port = SharedPortStore.GetPort(this.portRange); - var browserNewContextOptions = new BrowserNewContextOptions { IgnoreHTTPSErrors = true }; - - this.browserNewContextOptionsBuilder(browserNewContextOptions); - var traceFileOptions = new TracingStartOptions() { Screenshots = true, @@ -67,7 +63,7 @@ public async Task BuildAsync(Browser? browser = null) this.traceFileOptionsBuilder(traceFileOptions); - var playwrightDriver = new PlaywrightDriver(this.goToPageRetryCount, browserNewContextOptions, traceFileOptions); + var playwrightDriver = new PlaywrightDriver(this.goToPageRetryCount, traceFileOptions); var browserTypeLaunchOptions = new BrowserTypeLaunchOptions(); @@ -75,6 +71,8 @@ public async Task BuildAsync(Browser? browser = null) await playwrightDriver.InitializeAsync(browserTypeLaunchOptions).ConfigureAwait(false); + playwrightDriver.SetupBrowserNewContextOptions(deviceName, this.browserNewContextOptionsBuilder); + var disposable = (IAsyncDisposable?)null; var url = this.onLineHost; @@ -283,6 +281,7 @@ public Task GotoPageAsync(string relativePath, Func testHandler, st return this.playwrightDriver.GotoPageAsync( this.url.TrimEnd('/') + "/" + relativePath.TrimStart('/'), testHandler, + browserType: this.browser, traceFile: traceFile, pageSetupHandler: pageSetupHandler); } diff --git a/src/tests/SoloX.CodeQuality.Playwright.E2ETest/PlaywrightTestBuilderLocalTest.cs b/src/tests/SoloX.CodeQuality.Playwright.E2ETest/PlaywrightTestBuilderLocalTest.cs index 793928b..1278e74 100644 --- a/src/tests/SoloX.CodeQuality.Playwright.E2ETest/PlaywrightTestBuilderLocalTest.cs +++ b/src/tests/SoloX.CodeQuality.Playwright.E2ETest/PlaywrightTestBuilderLocalTest.cs @@ -40,7 +40,7 @@ public PlaywrightTestBuilderLocalTest() .WithPlaywrightOptions(opt => { //opt.Headless = false; - //opt.SlowMo = 1000; + //opt.SlowMo = 5000; //opt.Timeout = 60000; }) .WithPlaywrightNewContextOptions(opt => @@ -51,13 +51,17 @@ public PlaywrightTestBuilderLocalTest() } [Theory] - [InlineData(Browser.Chromium)] - [InlineData(Browser.Firefox)] - [InlineData(Browser.Webkit)] - public async Task ItShouldOpenTheHomePageFromStaticHomeFile(Browser browser) + [InlineData(Browser.Chromium, null)] + [InlineData(Browser.Chromium, Devices.Pixel)] + [InlineData(Browser.Chromium, Devices.PixelLandscape)] + [InlineData(Browser.Firefox, null)] + [InlineData(Browser.Webkit, null)] + [InlineData(Browser.Webkit, Devices.iPhone)] + [InlineData(Browser.Webkit, Devices.iPhoneLandscape)] + public async Task ItShouldOpenTheHomePageFromStaticHomeFile(Browser browser, string? deviceName) { var playwrightTest = await this.builder - .BuildAsync(browser) + .BuildAsync(browser, deviceName: deviceName) .ConfigureAwait(true); await using var _ = playwrightTest.ConfigureAwait(false); diff --git a/src/tests/SoloX.CodeQuality.Playwright.E2ETest/Resources/PackageNugetTestProject/PackageNugetTestProject/PackageNugetTestProject.csproj b/src/tests/SoloX.CodeQuality.Playwright.E2ETest/Resources/PackageNugetTestProject/PackageNugetTestProject/PackageNugetTestProject.csproj index 70bef6b..12ef0b8 100644 --- a/src/tests/SoloX.CodeQuality.Playwright.E2ETest/Resources/PackageNugetTestProject/PackageNugetTestProject/PackageNugetTestProject.csproj +++ b/src/tests/SoloX.CodeQuality.Playwright.E2ETest/Resources/PackageNugetTestProject/PackageNugetTestProject/PackageNugetTestProject.csproj @@ -30,7 +30,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - +