Skip to content

Commit d6439be

Browse files
authored
Introduce RemoveExposedFunctionAsync and RemoveScriptToEvaluateOnNewDocumentAsync (#2510)
1 parent b83563a commit d6439be

File tree

9 files changed

+266
-123
lines changed

9 files changed

+266
-123
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Threading.Tasks;
2+
using NUnit.Framework;
3+
using PuppeteerSharp.Nunit;
4+
5+
namespace PuppeteerSharp.Tests.EvaluationTests
6+
{
7+
public class RemoveScriptToEvaluateOnNewDocumentTests : PuppeteerPageBaseTest
8+
{
9+
[Test, Retry(2), PuppeteerTest("evaluation.spec", "Evaluation specs Page.removeScriptToEvaluateOnNewDocument", "should remove new document script")]
10+
public async Task ShouldRemoveNewDocumentScript()
11+
{
12+
var id = await Page.EvaluateFunctionOnNewDocumentAsync("() => globalThis.injected = 123");
13+
await Page.GoToAsync(TestConstants.ServerUrl + "/tamperable.html");
14+
var result = await Page.EvaluateFunctionAsync<int>("async () => globalThis.result");
15+
Assert.AreEqual(123, result);
16+
17+
await Page.RemoveScriptToEvaluateOnNewDocumentAsync(id.Identifier);
18+
19+
await Page.ReloadAsync();
20+
Assert.IsNull(await Page.EvaluateFunctionAsync("() => globalThis.result ?? null"));
21+
}
22+
}
23+
}

lib/PuppeteerSharp.Tests/PageTests/ExposeFunctionTests.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ namespace PuppeteerSharp.Tests.PageTests
88
{
99
public class ExposeFunctionTests : PuppeteerPageBaseTest
1010
{
11-
public ExposeFunctionTests() : base()
12-
{
13-
}
14-
1511
[Test, Retry(2), PuppeteerTest("page.spec", "Page Page.exposeFunction", "should work")]
1612
public async Task ShouldWork()
1713
{
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Threading.Tasks;
2+
using NUnit.Framework;
3+
using PuppeteerSharp.Nunit;
4+
5+
namespace PuppeteerSharp.Tests.PageTests
6+
{
7+
public class RemoveExposeFunctionTests : PuppeteerPageBaseTest
8+
{
9+
[Test, Retry(2), PuppeteerTest("page.spec", "Page Page.removeExposedFunction", "should work")]
10+
public async Task ShouldWork()
11+
{
12+
await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);
13+
var result = await Page.EvaluateFunctionAsync<int>("async () => compute(9, 4)");
14+
Assert.AreEqual(36, result);
15+
16+
await Page.RemoveExposedFunctionAsync("compute");
17+
18+
Assert.ThrowsAsync<EvaluationFailedException>(() => Page.EvaluateFunctionAsync<int>("async () => compute(9, 4)"));
19+
}
20+
}
21+
}

lib/PuppeteerSharp/IPage.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ public interface IPage : IDisposable, IAsyncDisposable
574574
/// </code>
575575
/// </example>
576576
/// <returns>Task.</returns>
577-
Task EvaluateExpressionOnNewDocumentAsync(string expression);
577+
Task<NewDocumentScriptEvaluation> EvaluateExpressionOnNewDocumentAsync(string expression);
578578

579579
/// <summary>
580580
/// Executes a function in browser context.
@@ -632,7 +632,7 @@ public interface IPage : IDisposable, IAsyncDisposable
632632
/// </code>
633633
/// </example>
634634
/// <returns>Task.</returns>
635-
Task EvaluateFunctionOnNewDocumentAsync(string pageFunction, params object[] args);
635+
Task<NewDocumentScriptEvaluation> EvaluateFunctionOnNewDocumentAsync(string pageFunction, params object[] args);
636636

637637
/// <summary>
638638
/// Adds a function called <c>name</c> on the page's <c>window</c> object.
@@ -647,6 +647,13 @@ public interface IPage : IDisposable, IAsyncDisposable
647647
/// <returns>Task.</returns>
648648
Task ExposeFunctionAsync(string name, Action puppeteerFunction);
649649

650+
/// <summary>
651+
/// Removes a previously added function via <see cref="IPage.ExposeFunctionAsync(string, Action)"/>.
652+
/// </summary>
653+
/// <param name="name">Name of the function to remove.</param>
654+
/// <returns>Task.</returns>
655+
Task RemoveExposedFunctionAsync(string name);
656+
650657
/// <summary>
651658
/// Adds a function called <c>name</c> on the page's <c>window</c> object.
652659
/// When called, the function executes <paramref name="puppeteerFunction"/> in C# and returns a <see cref="Task"/> which resolves to the return value of <paramref name="puppeteerFunction"/>.
@@ -1464,5 +1471,12 @@ public interface IPage : IDisposable, IAsyncDisposable
14641471
/// </summary>
14651472
/// <returns>A task that returns a <see cref="ICDPSession"/>.</returns>
14661473
Task<ICDPSession> CreateCDPSessionAsync();
1474+
1475+
/// <summary>
1476+
/// Removes script that injected into page by <see cref="IPage.EvaluateExpressionOnNewDocumentAsync(string)"/> or <see cref="IPage.EvaluateExpressionOnNewDocumentAsync(string)"/>.
1477+
/// </summary>
1478+
/// <param name="identifier">Function identifier.</param>
1479+
/// <returns>A task that resolves when the script is removed.</returns>
1480+
public Task RemoveScriptToEvaluateOnNewDocumentAsync(string identifier);
14671481
}
14681482
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace PuppeteerSharp.Messaging;
2+
3+
internal class PageAddScriptToEvaluateOnNewDocumentResponse
4+
{
5+
public string Identifier { get; set; }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace PuppeteerSharp.Messaging;
2+
3+
internal class PageRemoveScriptToEvaluateOnNewDocumentRequest
4+
{
5+
public string Identifier { get; set; }
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace PuppeteerSharp.Messaging;
2+
3+
internal class RuntimeRemoveBindingRequest
4+
{
5+
public string Name { get; set; }
6+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace PuppeteerSharp;
2+
3+
/// <summary>
4+
/// New document information.
5+
/// </summary>
6+
public class NewDocumentScriptEvaluation(string documentIdentifierIdentifier)
7+
{
8+
/// <summary>
9+
/// New document identifier.
10+
/// </summary>
11+
public string Identifier { get; set; } = documentIdentifierIdentifier;
12+
}

0 commit comments

Comments
 (0)