Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webview2): [wasm] Fix ExecuteScriptAsync #19672

Merged
merged 5 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#if HAS_UNO_WINUI
using System;
using System.Threading.Tasks;
using Private.Infrastructure;
using Microsoft/* UWP don't rename */.UI.Xaml.Controls;
using System.Linq;
using Microsoft.Web.WebView2.Core;
using System.Diagnostics;

namespace Uno.UI.RuntimeTests.Tests.Windows_UI_Xaml_Controls;

[TestClass]
[RunsOnUIThread]
public class Given_WebView2
{
[TestMethod]
#if __SKIA__
[Ignore("WebView2 is not yet supported on skia targets")]
#endif
public async Task When_InvokeScriptAsync()
{
var border = new Border();
var webView = new WebView2();
webView.Width = 200;
webView.Height = 200;
border.Child = webView;
TestServices.WindowHelper.WindowContent = border;
bool navigated = false;
await TestServices.WindowHelper.WaitForLoaded(border);
webView.NavigationCompleted += (sender, e) => navigated = true;
webView.NavigateToString("<html><body><div id='test' style='width: 100px; height: 100px; background-color: blue;' /></body></html>");
await TestServices.WindowHelper.WaitFor(() => navigated, timeoutMS: 10000);

var sw = Stopwatch.StartNew();
string color = null;

do
{
// We need to wait for the element to be available, navigated
// may be set to true too early on wasm.
color = await webView.ExecuteScriptAsync(
"""
(function () {
let testElement = document.getElementById('test');
if(testElement){
return testElement.style.backgroundColor.toString();
}
return "";
})()
""");

} while (sw.Elapsed < TimeSpan.FromSeconds(10) && string.IsNullOrEmpty(color.Replace("\"", "")));

Assert.AreEqual("\"blue\"", color);

// Change color to red
await webView.ExecuteScriptAsync("document.getElementById('test').style.backgroundColor = 'red'");
color = await webView.ExecuteScriptAsync("document.getElementById('test').style.backgroundColor.toString()");

Assert.AreEqual("\"red\"", color);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static partial class NativeMethods
[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.goForward")]
internal static partial void GoForward(IntPtr htmlId);

[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.executeScriptAsync")]
[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.executeScript")]
internal static partial string ExecuteScript(IntPtr htmlId, string script);

[JSImport("globalThis.Microsoft.UI.Xaml.Controls.WebView.getDocumentTitle")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,14 @@ private void OnNavigationCompleted(object sender, EventArgs e)
_coreWebView.RaiseNavigationCompleted(uri, true, 200, CoreWebView2WebErrorStatus.Unknown);
}

public Task<string> ExecuteScriptAsync(string script, CancellationToken token) => Task.FromResult(NativeMethods.ExecuteScript(HtmlId, script));
public async Task<string> ExecuteScriptAsync(string script, CancellationToken token)
{
await Task.Yield();
var result = NativeMethods.ExecuteScript(HtmlId, script);

// String needs to be wrapped in quotes to match Windows behavior
return $"\"{result.Replace("\"", "\\\"")}\"";
}

public Task<string> InvokeScriptAsync(string script, string[] arguments, CancellationToken token) => Task.FromResult<string>("");

Expand Down
Loading