Skip to content

Commit

Permalink
Merge branch 'master' into roll-chrome-to-124.0.6367.201
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed May 13, 2024
2 parents 067302e + 32c35f8 commit 5778bb3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 18 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
38 changes: 35 additions & 3 deletions lib/PuppeteerSharp.Tests/QueryObjectsTests/QueryObjectsTests.cs
Expand Up @@ -2,7 +2,7 @@
using NUnit.Framework;
using PuppeteerSharp.Nunit;

namespace PuppeteerSharp.Tests.QueryObjectTests
namespace PuppeteerSharp.Tests.QueryObjectsTests
{
public class QueryObjectsTests : PuppeteerPageBaseTest
{
Expand Down Expand Up @@ -37,14 +37,46 @@ public async Task ShouldWork()
}", objectsHandle));
}

[Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should work for non-trivial page")]
public async Task ShouldWorkForNonTrivialPage()
{
await Page.GoToAsync(TestConstants.EmptyPage);
// Create a custom class
var classHandle = await Page.EvaluateFunctionHandleAsync(@"() => {
return class CustomClass { };
}");

// Create an instance.
await Page.EvaluateFunctionAsync(@"CustomClass => {
self.customClass = new CustomClass();
}", classHandle);

// Validate only one has been added.
var prototypeHandle = await Page.EvaluateFunctionHandleAsync(@"CustomClass => {
return CustomClass.prototype;
}", classHandle);

var objectsHandle = await Page.QueryObjectsAsync(prototypeHandle);
Assert.AreEqual(
1,
await Page.EvaluateFunctionAsync<int>(@"objects => {
return objects.length;
}", objectsHandle));

// Check that instances.
Assert.True(await Page.EvaluateFunctionAsync<bool>(@"objects => {
return objects[0] === self.customClass;
}", objectsHandle));
}

[Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should fail for disposed handles")]
public async Task ShouldFailForDisposedHandles()
{
var prototypeHandle = await Page.EvaluateExpressionHandleAsync("HTMLBodyElement.prototype");
await prototypeHandle.DisposeAsync();
var exception = Assert.ThrowsAsync<PuppeteerException>(()
=> Page.QueryObjectsAsync(prototypeHandle));
Assert.AreEqual("Prototype JSHandle is disposed!", exception.Message);
Assert.AreEqual("Prototype JSHandle is disposed!", exception!.Message);
}

[Test, Retry(2), PuppeteerTest("queryObjects.spec", "page.queryObjects", "should fail primitive values as prototypes")]
Expand All @@ -53,7 +85,7 @@ public async Task ShouldFailPrimitiveValuesAsPrototypes()
var prototypeHandle = await Page.EvaluateExpressionHandleAsync("42");
var exception = Assert.ThrowsAsync<PuppeteerException>(()
=> Page.QueryObjectsAsync(prototypeHandle));
Assert.AreEqual("Prototype JSHandle must not be referencing primitive value", exception.Message);
Assert.AreEqual("Prototype JSHandle must not be referencing primitive value", exception!.Message);
}
}
}
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 5778bb3

Please sign in to comment.