Skip to content

Commit cca33d3

Browse files
authored
Add missing extensions test (#2620)
1 parent 2eaba48 commit cca33d3

File tree

4 files changed

+48
-15
lines changed

4 files changed

+48
-15
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// empty
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "Simple extension",
3+
"version": "0.1",
4+
"background": {
5+
"service_worker": "background.js"
6+
},
7+
"permissions": ["background", "activeTab"],
8+
"manifest_version": 3
9+
}

lib/PuppeteerSharp.Tests/ExtensionsTests/ExtensionsTests.cs

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
2+
using System.IO;
43
using System.Threading.Tasks;
54
using NUnit.Framework;
65
using PuppeteerSharp.Helpers;
@@ -10,11 +9,34 @@ namespace PuppeteerSharp.Tests.ExtensionsTests
109
{
1110
public class ExtensionsTests : PuppeteerBaseTest
1211
{
12+
private static readonly string _extensionPath = Path.Combine(AppContext.BaseDirectory, "Assets", "simple-extension");
13+
private static readonly string _serviceWorkerExtensionPath = Path.Combine(AppContext.BaseDirectory, "Assets", "service-worker-extension");
14+
15+
private static LaunchOptions BrowserWithExtensionOptions() => new()
16+
{
17+
Headless = false,
18+
Args = new[]
19+
{
20+
$"--disable-extensions-except={_extensionPath.Quote()}",
21+
$"--load-extension={_extensionPath.Quote()}"
22+
}
23+
};
24+
25+
private static LaunchOptions BrowserWithServiceWorkerExtensionOptions() => new()
26+
{
27+
Headless = false,
28+
Args = new[]
29+
{
30+
$"--disable-extensions-except={_serviceWorkerExtensionPath.Quote()}",
31+
$"--load-extension={_serviceWorkerExtensionPath.Quote()}"
32+
}
33+
};
34+
1335
[Test, Retry(2), PuppeteerTest("extensions.spec", "extensions", "background_page target type should be available")]
1436
public async Task BackgroundPageTargetTypeShouldBeAvailable()
1537
{
1638
await using var browserWithExtension = await Puppeteer.LaunchAsync(
17-
TestConstants.BrowserWithExtensionOptions(),
39+
BrowserWithExtensionOptions(),
1840
TestConstants.LoggerFactory);
1941
await using (await browserWithExtension.NewPageAsync())
2042
{
@@ -23,11 +45,23 @@ public async Task BackgroundPageTargetTypeShouldBeAvailable()
2345
}
2446
}
2547

48+
[Test, Retry(2), PuppeteerTest("extensions.spec", "extensions", "service_worker target type should be available")]
49+
public async Task ServiceWorkerTargetTypeShouldBeAvailable()
50+
{
51+
await using var browserWithExtension = await Puppeteer.LaunchAsync(
52+
BrowserWithServiceWorkerExtensionOptions(),
53+
TestConstants.LoggerFactory);
54+
var serviceWorkTarget = await browserWithExtension.WaitForTargetAsync(t => t.Type == TargetType.ServiceWorker);
55+
await using var page = await browserWithExtension.NewPageAsync();
56+
Assert.NotNull(serviceWorkTarget);
57+
58+
}
59+
2660
[Test, Retry(2), PuppeteerTest("extensions.spec", "extensions", "target.page() should return a background_page")]
2761
public async Task TargetPageShouldReturnABackgroundPage()
2862
{
2963
await using var browserWithExtension = await Puppeteer.LaunchAsync(
30-
TestConstants.BrowserWithExtensionOptions(),
64+
BrowserWithExtensionOptions(),
3165
TestConstants.LoggerFactory);
3266
var backgroundPageTarget = await browserWithExtension.WaitForTargetAsync(t => t.Type == TargetType.BackgroundPage);
3367
await using var page = await backgroundPageTarget.PageAsync();

lib/PuppeteerSharp.Tests/TestConstants.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public static class TestConstants
2222
public static readonly string CrossProcessHttpsPrefix = "https://127.0.0.1:8082";
2323
public static readonly string EmptyPage = $"{ServerUrl}/empty.html";
2424
public static readonly string CrossProcessUrl = ServerIpUrl;
25-
public static readonly string ExtensionPath = Path.Combine(AppContext.BaseDirectory, "Assets", "simple-extension");
2625
public static readonly bool IsChrome = PuppeteerTestAttribute.IsChrome;
2726

2827
public static readonly DeviceDescriptor IPhone = Puppeteer.Devices[DeviceDescriptorName.IPhone6];
@@ -54,15 +53,5 @@ public static class TestConstants
5453
EnqueueTransportMessages = true
5554
#endif
5655
};
57-
58-
public static LaunchOptions BrowserWithExtensionOptions() => new()
59-
{
60-
Headless = false,
61-
Args = new[]
62-
{
63-
$"--disable-extensions-except={ExtensionPath.Quote()}",
64-
$"--load-extension={ExtensionPath.Quote()}"
65-
}
66-
};
6756
}
6857
}

0 commit comments

Comments
 (0)