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 disabled WebView Cookie Tests #24978

Merged
merged 4 commits into from
Dec 2, 2024
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
64 changes: 41 additions & 23 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;
Copy link
Contributor

@MartyIX MartyIX Nov 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding #24978 (comment), if I return back this line, I can compile on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

using Label = Microsoft.Maui.Controls.Label;
using WebView = Microsoft.Maui.Controls.WebView;
using Microsoft.Maui.Controls.PlatformConfiguration;
using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific;

namespace Maui.Controls.Sample.Issues
{
Expand All @@ -13,19 +14,16 @@ public class Issue3262 : TestContentPage // or TestFlyoutPage, etc ...

protected override void Init()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The build is failing:

/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,16): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-maccatalyst]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,5): error CS1929: 'IPlatformElementConfiguration<Windows, WebView>' does not contain a definition for 'SetIsJavaScriptAlertEnabled' and the best extension method overload 'WebView.SetIsJavaScriptAlertEnabled(IPlatformElementConfiguration<Windows, WebView>, bool)' requires a receiver of type 'Microsoft.Maui.Controls.IPlatformElementConfiguration<Microsoft.Maui.Controls.PlatformConfiguration.Windows, Microsoft.Maui.Controls.WebView>' [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-maccatalyst]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,16): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-tizen]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,5): error CS1929: 'IPlatformElementConfiguration<Windows, WebView>' does not contain a definition for 'SetIsJavaScriptAlertEnabled' and the best extension method overload 'WebView.SetIsJavaScriptAlertEnabled(IPlatformElementConfiguration<Windows, WebView>, bool)' requires a receiver of type 'Microsoft.Maui.Controls.IPlatformElementConfiguration<Microsoft.Maui.Controls.PlatformConfiguration.Windows, Microsoft.Maui.Controls.WebView>' [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-tizen]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,16): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-ios]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,5): error CS1929: 'IPlatformElementConfiguration<Windows, WebView>' does not contain a definition for 'SetIsJavaScriptAlertEnabled' and the best extension method overload 'WebView.SetIsJavaScriptAlertEnabled(IPlatformElementConfiguration<Windows, WebView>, bool)' requires a receiver of type 'Microsoft.Maui.Controls.IPlatformElementConfiguration<Microsoft.Maui.Controls.PlatformConfiguration.Windows, Microsoft.Maui.Controls.WebView>' [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-ios]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,16): error CS0246: The type or namespace name 'Windows' could not be found (are you missing a using directive or an assembly reference?) [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-android]
/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs(47,5): error CS1929: 'IPlatformElementConfiguration<Windows, WebView>' does not contain a definition for 'SetIsJavaScriptAlertEnabled' and the best extension method overload 'WebView.SetIsJavaScriptAlertEnabled(IPlatformElementConfiguration<Windows, WebView>, bool)' requires a receiver of type 'Microsoft.Maui.Controls.IPlatformElementConfiguration<Microsoft.Maui.Controls.PlatformConfiguration.Windows, Microsoft.Maui.Controls.WebView>' [/Users/builder/azdo/_work/1/s/src/Controls/tests/TestCases.HostApp/Controls.TestCases.HostApp.csproj::TargetFramework=net9.0-android]
    0 Warning(s)
    8 Error(s)

Could you review it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should work now

{
#pragma warning disable CS0612 // Type or member is obsolete
Label header = new Label
{
Text = "Cookies...",
FontSize = Device.GetNamedSize(NamedSize.Medium, typeof(Label)),
HorizontalOptions = LayoutOptions.Center
};
#pragma warning restore CS0612 // Type or member is obsolete

try
{
CookieContainer cookieContainer = new CookieContainer();
string url = "https://dotnet.microsoft.com/apps/xamarin";
string url = "https://dotnet.microsoft.com/apps/maui";
Uri uri = new Uri(url, UriKind.RelativeOrAbsolute);

Cookie cookie = new Cookie
Expand All @@ -39,35 +37,41 @@ protected override void Init()

cookieContainer.Add(uri, cookie);

#pragma warning disable CS0618 // Type or member is obsolete
WebView webView = new WebView
{
Source = url,
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
HeightRequest = 200,
WidthRequest = 300,
Cookies = cookieContainer
};
#pragma warning restore CS0618 // Type or member is obsolete

#if WINDOWS
webView.On<Microsoft.Maui.Controls.PlatformConfiguration.Windows>().SetIsJavaScriptAlertEnabled(true);
#endif

Action<string> cookieExpectation = null;
var cookieResult = new Label()
{
Text = "Loading",
AutomationId = "CookieResult"
};

webView.Navigating += (_, __) =>
var successfullPageLoadLabel = new Label()
{
if (cookieExpectation != null)
cookieResult.Text = "Navigating";
IsVisible = false,
Text = "Page was loaded",
AutomationId = "SuccessfullPageLoadLabel"
};

webView.Navigated += async (_, __) =>
var successCookiesLabel = new Label()
{
if (cookieResult.Text == "Loading")
cookieResult.Text = "Loaded";
IsVisible = false,
Text = "Success",
AutomationId = "SuccessCookiesLabel"
};

webView.Navigated += async (_, __) =>
{
successfullPageLoadLabel.IsVisible = true;
_currentCookieValue = await webView.EvaluateJavaScriptAsync("document.cookie");
cookieExpectation?.Invoke(_currentCookieValue);
cookieExpectation = null;
Expand All @@ -84,7 +88,12 @@ protected override void Init()
{
Text = "Modify the Cookie Container"
},
cookieResult,
new HorizontalStackLayout()
{
cookieResult,
successfullPageLoadLabel,
successCookiesLabel
},
new StackLayout()
{
Orientation = StackOrientation.Horizontal,
Expand All @@ -97,7 +106,8 @@ protected override void Init()
Command = new Command(() =>
{
webView.Cookies = cookieContainer;
cookieResult.Text = String.Empty;
cookieResult.Text = string.Empty;
successCookiesLabel.IsVisible = false;
cookieExpectation = (cookieValue) =>
{
if(cookieValue.Contains("TestCookie", StringComparison.OrdinalIgnoreCase))
Expand All @@ -106,7 +116,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

Expand Down Expand Up @@ -134,7 +144,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

Expand All @@ -149,6 +159,7 @@ protected override void Init()
Command = new Command(() =>
{
cookieResult.Text = String.Empty;
successCookiesLabel.IsVisible = false;
cookieExpectation = (cookieValue) =>
{
if(Regex.Matches(cookieValue, "TestCookie").Count > 1)
Expand All @@ -157,7 +168,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

Expand Down Expand Up @@ -190,6 +201,7 @@ protected override void Init()
{
webView.Cookies = cookieContainer;
cookieResult.Text = String.Empty;
successCookiesLabel.IsVisible = false;
cookieContainer.Add(new Cookie
{
Name = $"TestCookie{cookieContainer.Count}",
Expand All @@ -212,7 +224,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

Expand Down Expand Up @@ -242,6 +254,7 @@ protected override void Init()
};

cookieResult.Text = String.Empty;
successCookiesLabel.IsVisible = false;
cookieExpectation = (cookieValue) =>
{
if(cookieValue.Contains(cookieToAdd.Name, StringComparison.OrdinalIgnoreCase))
Expand All @@ -250,7 +263,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

Expand All @@ -275,8 +288,13 @@ protected override void Init()
AutomationId = "PageWithoutCookies",
Command = new Command(() =>
{
var previousCookies = webView.Cookies;
webView.Cookies = null;
webView.Source = "file:///android_asset/googlemapsearch.html";

//Restore to the previous state
webView.Cookies = previousCookies;
webView.Source = url;
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Microsoft.Maui.TestCases.Tests.Issues
{
[Category(UITestCategories.WebView)]
public class Issue3262 : _IssuesUITest
{
public Issue3262(TestDevice testDevice) : base(testDevice)
Expand All @@ -13,21 +14,22 @@ public Issue3262(TestDevice testDevice) : base(testDevice)
public override string Issue => "Adding Cookies ability to a WebView...";

[Test]
[Category(UITestCategories.WebView)]
[FailsOnAllPlatformsWhenRunningOnXamarinUITest]
[FailsOnMacWhenRunningOnXamarinUITest]
[FailsOnWindowsWhenRunningOnXamarinUITest]
public void LoadingPageWithoutCookiesSpecifiedDoesntCrash()
{
App.WaitForElement("SuccessfullPageLoadLabel");
App.Tap("PageWithoutCookies");
App.WaitForElement("PageWithoutCookies");
}

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatformsWhenRunningOnXamarinUITest]
[FailsOnMacWhenRunningOnXamarinUITest]
[FailsOnWindowsWhenRunningOnXamarinUITest]
public void ChangeDuringNavigating()
{
App.WaitForElement("Loaded");
App.WaitForElement("SuccessfullPageLoadLabel");
// add a couple cookies
App.Tap("ChangeDuringNavigating");
ValidateSuccess();
Expand All @@ -36,12 +38,12 @@ public void ChangeDuringNavigating()
}

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatformsWhenRunningOnXamarinUITest]
[FailsOnMacWhenRunningOnXamarinUITest]
[FailsOnWindowsWhenRunningOnXamarinUITest]
public void AddAdditionalCookieToWebView()
{
App.WaitForElement("Loaded");
App.WaitForElement("SuccessfullPageLoadLabel");
// add a couple cookies
App.Tap("AdditionalCookie");
ValidateSuccess();
Expand All @@ -50,23 +52,23 @@ public void AddAdditionalCookieToWebView()
}

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatformsWhenRunningOnXamarinUITest]
[FailsOnMacWhenRunningOnXamarinUITest]
[FailsOnWindowsWhenRunningOnXamarinUITest]
public void SetToOneCookie()
{
App.WaitForElement("Loaded");
App.WaitForElement("SuccessfullPageLoadLabel");
App.Tap("OneCookie");
ValidateSuccess();
}

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatformsWhenRunningOnXamarinUITest]
[FailsOnMacWhenRunningOnXamarinUITest]
[FailsOnWindowsWhenRunningOnXamarinUITest]
public void SetCookieContainerToNullDisablesCookieManagement()
{
App.WaitForElement("Loaded");
App.WaitForElement("SuccessfullPageLoadLabel");
// add a cookie to verify said cookie remains
App.Tap("AdditionalCookie");
ValidateSuccess();
Expand All @@ -75,12 +77,12 @@ public void SetCookieContainerToNullDisablesCookieManagement()
}

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatformsWhenRunningOnXamarinUITest]
[FailsOnMacWhenRunningOnXamarinUITest]
[FailsOnWindowsWhenRunningOnXamarinUITest]
public void RemoveAllTheCookiesIAdded()
{
App.WaitForElement("Loaded");
App.WaitForElement("SuccessfullPageLoadLabel");
// add a cookie so you can remove a cookie
App.Tap("AdditionalCookie");
ValidateSuccess();
Expand All @@ -92,7 +94,7 @@ void ValidateSuccess()
{
try
{
App.WaitForElement("Success");
App.WaitForElement("SuccessCookiesLabel");
}
catch
{
Expand Down
Loading