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

NullReferenceException throws on Windows when setting Cookies on .NET MAUI WebView #24846

Merged
merged 18 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
327964d
Fixed the null exception on maui webview at load time.
NanthiniMahalingam Sep 18, 2024
167e07a
Added the test cases for webview
NanthiniMahalingam Sep 20, 2024
7893d3d
Removed unwanted code section.
NanthiniMahalingam Sep 20, 2024
7ce8c7f
Added the output images for web view issue fix.
NanthiniMahalingam Sep 22, 2024
0c42570
Invoked the SyncPlatformCookiesToVirtualView in OnCoreWebView2Initial…
NanthiniMahalingam Sep 26, 2024
0fea22b
Change the source value in test case.
NanthiniMahalingam Sep 27, 2024
7ad6fb4
Added the UI test cases and removed images.
NanthiniMahalingam Sep 30, 2024
9df9990
Moved the SyncPlatformCookies under UpdateUserAgent method.
NanthiniMahalingam Oct 4, 2024
ed4eeb2
Invoked the SyncPlatformCookies after the UpdateUserAgent method.
NanthiniMahalingam Oct 7, 2024
368f5b5
Updated UI test case.
NanthiniMahalingam Oct 14, 2024
e332f57
Removed unwanted condition in Webview test case.
NanthiniMahalingam Oct 14, 2024
ad0c0a1
Used the waitForElement instead of task delay.
NanthiniMahalingam Oct 14, 2024
92407f2
Updated fix due to resolve failed case.
NanthiniMahalingam Oct 25, 2024
4ab37e8
Added the fix for failed cases.
NanthiniMahalingam Oct 28, 2024
37ad4e9
Removed unwanted code changes.
NanthiniMahalingam Oct 28, 2024
c62a7f2
Updated the source URL in test case.
NanthiniMahalingam Nov 4, 2024
a8a6a5a
Get the source property from sender instead of getting from the handl…
NanthiniMahalingam Nov 5, 2024
25cd0be
Resolved the webview loaded without exception failed case in android …
NanthiniMahalingam Nov 7, 2024
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
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()
jsuarezruiz marked this conversation as resolved.
Show resolved Hide resolved
{
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
}
}
}
}
}
Loading