Skip to content

Commit

Permalink
Merge branch 'master' into add-missing-queryobjects-test
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed May 6, 2024
2 parents b60629b + cca33d3 commit 536401c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 15 deletions.
@@ -0,0 +1 @@
// empty
@@ -0,0 +1,9 @@
{
"name": "Simple extension",
"version": "0.1",
"background": {
"service_worker": "background.js"
},
"permissions": ["background", "activeTab"],
"manifest_version": 3
}
42 changes: 38 additions & 4 deletions 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;
Expand All @@ -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())
{
Expand All @@ -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();
Expand Down
11 changes: 0 additions & 11 deletions lib/PuppeteerSharp.Tests/TestConstants.cs
Expand Up @@ -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];
Expand Down Expand Up @@ -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()}"
}
};
}
}

0 comments on commit 536401c

Please sign in to comment.