Skip to content

Commit

Permalink
Bidi: Implement SetContentAsync
Browse files Browse the repository at this point in the history
  • Loading branch information
kblok committed Feb 12, 2025
1 parent 0959052 commit 3bbb8f2
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1515,21 +1515,6 @@
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[page.spec] *Page.setContent*",
"platforms": [
"darwin",
"linux",
"win32"
],
"parameters": [
"webDriverBiDi"
],
"expectations": [
"FAIL"
]
},
{
"comment": "This is part of organizing the webdriver bidi implementation, We will remove it one by one",
"testIdPattern": "[proxy.spec] *",
Expand Down
6 changes: 3 additions & 3 deletions lib/PuppeteerSharp.Tests/PageTests/SetContentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace PuppeteerSharp.Tests.PageTests
{
public class SetContentTests : PuppeteerPageBaseTest
{
const string ExpectedOutput = "<html><head></head><body><div>hello</div></body></html>";
private const string ExpectedOutput = "<html><head></head><body><div>hello</div></body></html>";

public async Task Usage(IBrowser browser)
{
Expand Down Expand Up @@ -65,7 +65,7 @@ public async Task ShouldRespectTimeout()
Timeout = 1
}));

Assert.That(exception.Message, Does.Contain("Timeout of 1 ms exceeded"));
Assert.That(exception!.Message, Does.Contain("Timeout of 1 ms exceeded"));
}

[Test, Retry(2), PuppeteerTest("page.spec", "Page Page.setContent", "should respect default navigation timeout")]
Expand All @@ -79,7 +79,7 @@ public async Task ShouldRespectDefaultTimeout()
var exception = Assert.ThrowsAsync<TimeoutException>(async () =>
await Page.SetContentAsync($"<img src='{TestConstants.ServerUrl + imgPath}'></img>"));

Assert.That(exception.Message, Does.Contain("Timeout of 1 ms exceeded"));
Assert.That(exception!.Message, Does.Contain("Timeout of 1 ms exceeded"));
}

[Test, Retry(2), PuppeteerTest("page.spec", "Page Page.setContent", "should await resources to load")]
Expand Down
18 changes: 17 additions & 1 deletion lib/PuppeteerSharp/Bidi/BidiFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,14 @@ internal BidiPage BidiPage
public override Task<IElementHandle> AddScriptTagAsync(AddTagOptions options) => throw new System.NotImplementedException();

/// <inheritdoc />
public override Task SetContentAsync(string html, NavigationOptions options = null) => throw new System.NotImplementedException();
public override Task SetContentAsync(string html, NavigationOptions options = null)
{
var waitForLoadTask = WaitForLoadAsync(options);
var waitForNetworkIdleTask = WaitForNetworkIdleAsync(options);
var waitTask = Task.WhenAny([waitForLoadTask, waitForNetworkIdleTask]);

return Task.WhenAll(SetFrameContentAsync(html), waitTask);
}

/// <inheritdoc />
public override async Task<IResponse> GoToAsync(string url, NavigationOptions options)
Expand Down Expand Up @@ -334,6 +341,15 @@ private void CreateFrameTarget(BrowsingContext browsingContext)
};
}

private Task SetFrameContentAsync(string content)
=> EvaluateFunctionAsync(
@"""html => {{
document.open();
document.write(html);
document.close();
}}""",
content);

private class Realms(BidiFrameRealm defaultRealm, BidiFrameRealm internalRealm)
{
public BidiFrameRealm Default { get; } = defaultRealm;
Expand Down

0 comments on commit 3bbb8f2

Please sign in to comment.