Skip to content

Commit

Permalink
NullReferenceException throws on Windows when setting Cookies on .NET…
Browse files Browse the repository at this point in the history
… MAUI WebView (#24846)

* Fixed the null exception on maui webview at load time.

* Added the test cases for webview

* Removed unwanted code section.

* Added the output images for web view issue fix.

* Invoked the SyncPlatformCookiesToVirtualView in OnCoreWebView2Initialized method.

* Change the source value in test case.

* Added the UI test cases and removed images.

* Moved the SyncPlatformCookies under UpdateUserAgent method.

* Invoked the SyncPlatformCookies after the UpdateUserAgent method.

* Updated UI test case.

* Removed unwanted condition in Webview test case.

* Used the waitForElement instead of task delay.

* Updated fix due to resolve failed case.

* Added the fix for failed cases.

* Removed unwanted code changes.

* Updated the source URL in test case.

* Get the source property from sender instead of getting from the handler platform view.

* Resolved the webview loaded without exception failed case in android platform.
  • Loading branch information
NanthiniMahalingam authored Nov 26, 2024
1 parent d160357 commit f115e57
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue18452.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using System.Net;

namespace Maui.Controls.Sample.Issues
{
[Issue(IssueTracker.Github, 18452, "NullReferenceException throws on Windows when setting Cookies on .NET MAUI WebView", PlatformAffected.UWP)]
public class Issue18452 : TestContentPage
{
protected override void Init()
{
Grid grid = new Grid();
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.8, GridUnitType.Star) });
grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(0.2, GridUnitType.Star) });
WebView webView = new WebView();

Label label = new Label();
label.AutomationId = "Label";

const string url = "https://learn.microsoft.com/en-us/dotnet/";

CookieContainer cookieContainer = new();
Uri uri = new(url, UriKind.RelativeOrAbsolute);

Cookie cookie = new()
{
Name = "DotNetMAUICookie",
Expires = DateTime.Now.AddDays(1),
Value = "My cookie",
Domain = uri.Host,
Path = "/"
};

cookieContainer.Add(uri, cookie);
webView.Cookies = cookieContainer;
webView.AutomationId = "WebView";
webView.Source = new UrlWebViewSource { Url = uri.ToString() };

grid.Children.Add(webView);

webView.Navigated += (s, e) =>
{
var cookies = webView.Cookies.GetCookies(uri);
foreach (Cookie c in cookies)
{
if (c.Name == "DotNetMAUICookie")
{
if (!grid.Contains(label))
{
grid.Children.Add(label);
label.Text = "Success";
break;
}
}
}
};

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

namespace Microsoft.Maui.TestCases.Tests.Issues;

[Category(UITestCategories.WebView)]
public class Issue18452 : _IssuesUITest
{
public override string Issue => "NullReferenceException throws on Windows when setting Cookies on .NET MAUI WebView";
string? expected = "Success";

public Issue18452(TestDevice device) : base(device)
{
}

[Test]
public void WebViewLoadedWithoutException()
{
App.WaitForElement("Label");
string? label = App.FindElement("Label").GetText();
Assert.That(label, Is.EqualTo(expected));
}
}
11 changes: 10 additions & 1 deletion src/Core/src/Handlers/WebView/WebViewHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ internal async Task SyncPlatformCookies(string url)
if (myCookieJar is null)
return;

if (PlatformView.CoreWebView2 is null)
{
return;
}

await InitialCookiePreloadIfNecessary(url);
var cookies = myCookieJar.GetCookies(uri);

Expand Down Expand Up @@ -366,6 +371,10 @@ void OnCoreWebView2Initialized(WebView2 sender, CoreWebView2InitializedEventArgs
if (Handler is WebViewHandler handler)
{
sender.UpdateUserAgent(handler.VirtualView);
if (sender.Source is not null)
{
handler.SyncPlatformCookies(sender.Source.ToString()).FireAndForget();
}
}
}

Expand Down Expand Up @@ -405,4 +414,4 @@ void OnProcessFailed(CoreWebView2 sender, CoreWebView2ProcessFailedEventArgs arg
}
}
}
}
}

0 comments on commit f115e57

Please sign in to comment.