diff --git a/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_WebView2.cs b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_WebView2.cs new file mode 100644 index 000000000000..ca10e0cbaa6a --- /dev/null +++ b/src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml_Controls/Given_WebView2.cs @@ -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("
"); + 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 diff --git a/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.Interop.wasm.cs b/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.Interop.wasm.cs index d9f7c3ddb4be..a7c2e9a6d118 100644 --- a/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.Interop.wasm.cs +++ b/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.Interop.wasm.cs @@ -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")] diff --git a/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs b/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs index d16e0af64bc8..37dd0e279b96 100644 --- a/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs +++ b/src/Uno.UI/UI/Xaml/Controls/WebView/Native/Wasm/NativeWebView.wasm.cs @@ -48,7 +48,14 @@ private void OnNavigationCompleted(object sender, EventArgs e) _coreWebView.RaiseNavigationCompleted(uri, true, 200, CoreWebView2WebErrorStatus.Unknown); } - public Task