Skip to content

Commit

Permalink
fix Page.RemoveExposedFunctionAsync (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
ravriel committed Jul 3, 2024
1 parent 88dc82c commit e24aa62
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions lib/PuppeteerSharp.Tests/PageTests/RemoveExposedFunctionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ public class RemoveExposeFunctionTests : PuppeteerPageBaseTest
[Test, Retry(2), PuppeteerTest("page.spec", "Page Page.removeExposedFunction", "should work")]
public async Task ShouldWork()
{
await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);
var result = await Page.EvaluateFunctionAsync<int>("async () => compute(9, 4)");
Assert.AreEqual(36, result);

await Page.RemoveExposedFunctionAsync("compute");
// Run twice to verify function can be added again after remove
for (var i = 0; i < 2; i++)
{
await Page.ExposeFunctionAsync("compute", (int a, int b) => a * b);
var result = await Page.EvaluateFunctionAsync<int>("async () => compute(9, 4)");
Assert.AreEqual(36, result);

await Page.RemoveExposedFunctionAsync("compute");
}
Assert.ThrowsAsync<EvaluationFailedException>(() => Page.EvaluateFunctionAsync<int>("async () => compute(9, 4)"));
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/PuppeteerSharp/Cdp/CdpPage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ public override async Task RemoveExposedFunctionAsync(string name)
throw new ArgumentNullException(nameof(name));
}

if (!_exposedFunctions.TryRemove(name, out var exposedFun) && !_bindings.TryRemove(name, out _))
if (!_exposedFunctions.TryRemove(name, out var exposedFun) || !_bindings.TryRemove(name, out _))
{
throw new PuppeteerException(
$"Failed to remove page binding with name {name}: window['{name}'] does not exists!");
Expand Down

0 comments on commit e24aa62

Please sign in to comment.