diff --git a/lib/PuppeteerSharp.Tests/Assets/service-worker-extension/background.js b/lib/PuppeteerSharp.Tests/Assets/service-worker-extension/background.js new file mode 100644 index 000000000..8b1a39374 --- /dev/null +++ b/lib/PuppeteerSharp.Tests/Assets/service-worker-extension/background.js @@ -0,0 +1 @@ +// empty diff --git a/lib/PuppeteerSharp.Tests/Assets/service-worker-extension/manifest.json b/lib/PuppeteerSharp.Tests/Assets/service-worker-extension/manifest.json new file mode 100644 index 000000000..25828b6d2 --- /dev/null +++ b/lib/PuppeteerSharp.Tests/Assets/service-worker-extension/manifest.json @@ -0,0 +1,9 @@ +{ + "name": "Simple extension", + "version": "0.1", + "background": { + "service_worker": "background.js" + }, + "permissions": ["background", "activeTab"], + "manifest_version": 3 +} diff --git a/lib/PuppeteerSharp.Tests/ExtensionsTests/ExtensionsTests.cs b/lib/PuppeteerSharp.Tests/ExtensionsTests/ExtensionsTests.cs index 75829542d..b77207a20 100644 --- a/lib/PuppeteerSharp.Tests/ExtensionsTests/ExtensionsTests.cs +++ b/lib/PuppeteerSharp.Tests/ExtensionsTests/ExtensionsTests.cs @@ -1,6 +1,5 @@ using System; -using System.Collections.Generic; -using System.Linq; +using System.IO; using System.Threading.Tasks; using NUnit.Framework; using PuppeteerSharp.Helpers; @@ -10,11 +9,34 @@ namespace PuppeteerSharp.Tests.ExtensionsTests { public class ExtensionsTests : PuppeteerBaseTest { + private static readonly string _extensionPath = Path.Combine(AppContext.BaseDirectory, "Assets", "simple-extension"); + private static readonly string _serviceWorkerExtensionPath = Path.Combine(AppContext.BaseDirectory, "Assets", "service-worker-extension"); + + private static LaunchOptions BrowserWithExtensionOptions() => new() + { + Headless = false, + Args = new[] + { + $"--disable-extensions-except={_extensionPath.Quote()}", + $"--load-extension={_extensionPath.Quote()}" + } + }; + + private static LaunchOptions BrowserWithServiceWorkerExtensionOptions() => new() + { + Headless = false, + Args = new[] + { + $"--disable-extensions-except={_serviceWorkerExtensionPath.Quote()}", + $"--load-extension={_serviceWorkerExtensionPath.Quote()}" + } + }; + [Test, Retry(2), PuppeteerTest("extensions.spec", "extensions", "background_page target type should be available")] public async Task BackgroundPageTargetTypeShouldBeAvailable() { await using var browserWithExtension = await Puppeteer.LaunchAsync( - TestConstants.BrowserWithExtensionOptions(), + BrowserWithExtensionOptions(), TestConstants.LoggerFactory); await using (await browserWithExtension.NewPageAsync()) { @@ -23,11 +45,23 @@ await using (await browserWithExtension.NewPageAsync()) } } + [Test, Retry(2), PuppeteerTest("extensions.spec", "extensions", "service_worker target type should be available")] + public async Task ServiceWorkerTargetTypeShouldBeAvailable() + { + await using var browserWithExtension = await Puppeteer.LaunchAsync( + BrowserWithServiceWorkerExtensionOptions(), + TestConstants.LoggerFactory); + var serviceWorkTarget = await browserWithExtension.WaitForTargetAsync(t => t.Type == TargetType.ServiceWorker); + await using var page = await browserWithExtension.NewPageAsync(); + Assert.NotNull(serviceWorkTarget); + + } + [Test, Retry(2), PuppeteerTest("extensions.spec", "extensions", "target.page() should return a background_page")] public async Task TargetPageShouldReturnABackgroundPage() { await using var browserWithExtension = await Puppeteer.LaunchAsync( - TestConstants.BrowserWithExtensionOptions(), + BrowserWithExtensionOptions(), TestConstants.LoggerFactory); var backgroundPageTarget = await browserWithExtension.WaitForTargetAsync(t => t.Type == TargetType.BackgroundPage); await using var page = await backgroundPageTarget.PageAsync(); diff --git a/lib/PuppeteerSharp.Tests/TestConstants.cs b/lib/PuppeteerSharp.Tests/TestConstants.cs index 9b1ded441..66273b374 100644 --- a/lib/PuppeteerSharp.Tests/TestConstants.cs +++ b/lib/PuppeteerSharp.Tests/TestConstants.cs @@ -22,7 +22,6 @@ public static class TestConstants public static readonly string CrossProcessHttpsPrefix = "https://127.0.0.1:8082"; public static readonly string EmptyPage = $"{ServerUrl}/empty.html"; public static readonly string CrossProcessUrl = ServerIpUrl; - public static readonly string ExtensionPath = Path.Combine(AppContext.BaseDirectory, "Assets", "simple-extension"); public static readonly bool IsChrome = PuppeteerTestAttribute.IsChrome; public static readonly DeviceDescriptor IPhone = Puppeteer.Devices[DeviceDescriptorName.IPhone6]; @@ -54,15 +53,5 @@ public static LaunchOptions DefaultBrowserOptions() => new() EnqueueTransportMessages = true #endif }; - - public static LaunchOptions BrowserWithExtensionOptions() => new() - { - Headless = false, - Args = new[] - { - $"--disable-extensions-except={ExtensionPath.Quote()}", - $"--load-extension={ExtensionPath.Quote()}" - } - }; } }