Skip to content
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
62 changes: 62 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue34558.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
namespace Maui.Controls.Sample.Issues;

[Issue(IssueTracker.Github, 34558, "[Windows] WebView renders blank when HybridWebView and WebView coexist in same app", PlatformAffected.UWP)]
public partial class Issue34558 : ContentPage
{
public Issue34558()
{
var hybridStatusLabel = new Label
{
AutomationId = "HybridStatusLabel",
Text = "Test passes if WebView shows content below without any exception",
};

// HybridWebView — initializing this creates a separate CoreWebView2Environment (without fix)
// that conflicts with the regular WebView's default environment.
var hybridWebView = new HybridWebView
{
AutomationId = "HybridWebViewControl",
HybridRoot = "hybridroot",
HeightRequest = 120,
HorizontalOptions = LayoutOptions.Fill,
};

var webView = new WebView
{
AutomationId = "RegularWebView",
HeightRequest = 150,
HorizontalOptions = LayoutOptions.Fill,
Source = new HtmlWebViewSource { Html = "<html><body><h1>WebView should load here</h1><p>If you see this, the WebView is working.</p></body></html>" },
};
Comment on lines +24 to +30

// This label is added to the layout only after the WebView successfully navigates,
// so the UI test can wait for it to appear as proof the WebView rendered content.
var webViewNavigatedLabel = new Label
{
AutomationId = "WebViewNavigatedLabel",
Text = "WebView successfully navigated and rendered content!",
};

var layout = new VerticalStackLayout
{
Spacing = 10,
Padding = new Thickness(16),
Children =
{
hybridStatusLabel,
hybridWebView,
webView,
}
};

webView.Navigated += (s, e) =>
{
if (e.Result == WebNavigationResult.Success)
{
layout.Children.Add(webViewNavigatedLabel);
}
};

Content = layout;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.TestCases.Tests.Issues;

public class Issue34558 : _IssuesUITest
{
public Issue34558(TestDevice device) : base(device) { }

public override string Issue => "[Windows] WebView renders blank when HybridWebView and WebView coexist in same app";

[Test]
[Category(UITestCategories.WebView)]
public void WebViewLoadsWhenCoexistingWithHybridWebView()
{
App.WaitForElement("HybridStatusLabel");
App.WaitForElement("RegularWebView");

var rect = App.WaitForElement("RegularWebView").GetRect();
Assert.That(rect.Height, Is.GreaterThan(0), "WebView should render with non-zero height");

Comment on lines +20 to +22
// "WebViewNavigatedLabel" is only added to the layout after the WebView successfully
// navigates. Waiting for it confirms the WebView rendered content and was not blank.
App.WaitForElement("WebViewNavigatedLabel");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,17 +307,38 @@ private async Task<bool> TryInitializeWebView2(WebView2 webView)
var initializingArgs = new WebViewInitializationStartedEventArgs();
VirtualView?.WebViewInitializationStarted(initializingArgs);

var env = await CoreWebView2Environment.CreateWithOptionsAsync(
browserExecutableFolder: initializingArgs.BrowserExecutableFolder,
userDataFolder: initializingArgs.UserDataFolder,
options: initializingArgs.EnvironmentOptions);
// Only create a custom CoreWebView2Environment when the user has provided custom
// settings. Creating a separate environment with all-default settings conflicts with
// other WebView2 controls (e.g., regular WebView) in the same app that use the
// default environment, because WebView2 requires all controls sharing the same user
// data folder to use compatible environments. When no customizations are needed,
// use EnsureCoreWebView2Async() so this control joins the default shared environment.
bool hasCustomSettings =
initializingArgs.BrowserExecutableFolder != null ||
initializingArgs.UserDataFolder != null ||
initializingArgs.EnvironmentOptions != null ||
!string.IsNullOrEmpty(initializingArgs.ScriptLocale) ||
initializingArgs.IsInPrivateModeEnabled ||
!string.IsNullOrEmpty(initializingArgs.ProfileName);

if (hasCustomSettings)
{
var env = await CoreWebView2Environment.CreateWithOptionsAsync(
browserExecutableFolder: initializingArgs.BrowserExecutableFolder,
userDataFolder: initializingArgs.UserDataFolder,
options: initializingArgs.EnvironmentOptions);

var options = env.CreateCoreWebView2ControllerOptions();
options.ScriptLocale = initializingArgs.ScriptLocale;
options.IsInPrivateModeEnabled = initializingArgs.IsInPrivateModeEnabled;
options.ProfileName = initializingArgs.ProfileName;
var options = env.CreateCoreWebView2ControllerOptions();
options.ScriptLocale = initializingArgs.ScriptLocale;
options.IsInPrivateModeEnabled = initializingArgs.IsInPrivateModeEnabled;
options.ProfileName = initializingArgs.ProfileName;

await webView.EnsureCoreWebView2Async(env, options);
await webView.EnsureCoreWebView2Async(env, options);
}
else
{
await webView.EnsureCoreWebView2Async();
}

webView.CoreWebView2.Settings.AreDevToolsEnabled = Handler?.DeveloperTools.Enabled ?? false;
webView.CoreWebView2.Settings.IsWebMessageEnabled = true;
Expand Down
Loading