From 9d1daa644de42b3efc61da923a61b57370436cc1 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Sat, 28 Sep 2024 01:56:26 +0200 Subject: [PATCH 1/2] Fix disabled WebView Cookie Tests --- .../TestCases.HostApp/Issues/Issue3262.cs | 61 ++++++++++++------- .../Tests/Issues/Issue3262.cs | 37 +++++------ 2 files changed, 57 insertions(+), 41 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs index 76fad6a092b8..d62591977c4f 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs @@ -13,19 +13,16 @@ public class Issue3262 : TestContentPage // or TestFlyoutPage, etc ... protected override void Init() { -#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 @@ -39,35 +36,39 @@ 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 - webView.On().SetIsJavaScriptAlertEnabled(true); + + webView.On().SetIsJavaScriptAlertEnabled(true); Action 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; @@ -84,7 +85,12 @@ protected override void Init() { Text = "Modify the Cookie Container" }, - cookieResult, + new HorizontalStackLayout() + { + cookieResult, + successfullPageLoadLabel, + successCookiesLabel + }, new StackLayout() { Orientation = StackOrientation.Horizontal, @@ -97,7 +103,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)) @@ -106,7 +113,7 @@ protected override void Init() } else { - cookieResult.Text = "Success"; + successCookiesLabel.IsVisible = true; } }; @@ -134,7 +141,7 @@ protected override void Init() } else { - cookieResult.Text = "Success"; + successCookiesLabel.IsVisible = true; } }; @@ -149,6 +156,7 @@ protected override void Init() Command = new Command(() => { cookieResult.Text = String.Empty; + successCookiesLabel.IsVisible = false; cookieExpectation = (cookieValue) => { if(Regex.Matches(cookieValue, "TestCookie").Count > 1) @@ -157,7 +165,7 @@ protected override void Init() } else { - cookieResult.Text = "Success"; + successCookiesLabel.IsVisible = true; } }; @@ -190,6 +198,7 @@ protected override void Init() { webView.Cookies = cookieContainer; cookieResult.Text = String.Empty; + successCookiesLabel.IsVisible = false; cookieContainer.Add(new Cookie { Name = $"TestCookie{cookieContainer.Count}", @@ -212,7 +221,7 @@ protected override void Init() } else { - cookieResult.Text = "Success"; + successCookiesLabel.IsVisible = true; } }; @@ -242,6 +251,7 @@ protected override void Init() }; cookieResult.Text = String.Empty; + successCookiesLabel.IsVisible = false; cookieExpectation = (cookieValue) => { if(cookieValue.Contains(cookieToAdd.Name, StringComparison.OrdinalIgnoreCase)) @@ -250,7 +260,7 @@ protected override void Init() } else { - cookieResult.Text = "Success"; + successCookiesLabel.IsVisible = true; } }; @@ -275,8 +285,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; }) } } diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs index 26088e804e95..5cabf6a52019 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs @@ -4,6 +4,7 @@ namespace Microsoft.Maui.TestCases.Tests.Issues { + [Category(UITestCategories.WebView)] public class Issue3262 : _IssuesUITest { public Issue3262(TestDevice testDevice) : base(testDevice) @@ -13,21 +14,21 @@ public Issue3262(TestDevice testDevice) : base(testDevice) public override string Issue => "Adding Cookies ability to a WebView..."; [Test] - [Category(UITestCategories.WebView)] - [FailsOnAllPlatformsWhenRunningOnXamarinUITest] + [FailsOnMac] public void LoadingPageWithoutCookiesSpecifiedDoesntCrash() { + App.WaitForElement("SuccessfullPageLoadLabel"); App.Tap("PageWithoutCookies"); App.WaitForElement("PageWithoutCookies"); } [Test] - [Category(UITestCategories.WebView)] [Category(UITestCategories.Compatibility)] - [FailsOnAllPlatformsWhenRunningOnXamarinUITest] + [FailsOnMac] + [FailsOnWindows] public void ChangeDuringNavigating() { - App.WaitForElement("Loaded"); + App.WaitForElement("SuccessfullPageLoadLabel"); // add a couple cookies App.Tap("ChangeDuringNavigating"); ValidateSuccess(); @@ -36,12 +37,12 @@ public void ChangeDuringNavigating() } [Test] - [Category(UITestCategories.WebView)] [Category(UITestCategories.Compatibility)] - [FailsOnAllPlatformsWhenRunningOnXamarinUITest] + [FailsOnMac] + [FailsOnWindows] public void AddAdditionalCookieToWebView() { - App.WaitForElement("Loaded"); + App.WaitForElement("SuccessfullPageLoadLabel"); // add a couple cookies App.Tap("AdditionalCookie"); ValidateSuccess(); @@ -50,23 +51,23 @@ public void AddAdditionalCookieToWebView() } [Test] - [Category(UITestCategories.WebView)] [Category(UITestCategories.Compatibility)] - [FailsOnAllPlatformsWhenRunningOnXamarinUITest] + [FailsOnMac] + [FailsOnWindows] public void SetToOneCookie() { - App.WaitForElement("Loaded"); + App.WaitForElement("SuccessfullPageLoadLabel"); App.Tap("OneCookie"); ValidateSuccess(); } [Test] - [Category(UITestCategories.WebView)] [Category(UITestCategories.Compatibility)] - [FailsOnAllPlatformsWhenRunningOnXamarinUITest] + [FailsOnMac] + [FailsOnWindows] public void SetCookieContainerToNullDisablesCookieManagement() { - App.WaitForElement("Loaded"); + App.WaitForElement("SuccessfullPageLoadLabel"); // add a cookie to verify said cookie remains App.Tap("AdditionalCookie"); ValidateSuccess(); @@ -75,12 +76,12 @@ public void SetCookieContainerToNullDisablesCookieManagement() } [Test] - [Category(UITestCategories.WebView)] [Category(UITestCategories.Compatibility)] - [FailsOnAllPlatformsWhenRunningOnXamarinUITest] + [FailsOnMac] + [FailsOnWindows] public void RemoveAllTheCookiesIAdded() { - App.WaitForElement("Loaded"); + App.WaitForElement("SuccessfullPageLoadLabel"); // add a cookie so you can remove a cookie App.Tap("AdditionalCookie"); ValidateSuccess(); @@ -92,7 +93,7 @@ void ValidateSuccess() { try { - App.WaitForElement("Success"); + App.WaitForElement("SuccessCookiesLabel"); } catch { From 2470b849fa2e5786dafc391c75a1707f33492699 Mon Sep 17 00:00:00 2001 From: Jakub Florkowski Date: Fri, 15 Nov 2024 18:24:32 +0100 Subject: [PATCH 2/2] Fixed test --- .../TestCases.HostApp/Issues/Issue3262.cs | 1 + .../Tests/Issues/Issue3262.cs | 23 ++++++++++--------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs b/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs index d62591977c4f..f8856f7eb6ef 100644 --- a/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs +++ b/src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs @@ -1,5 +1,6 @@ using System.Net; using System.Text.RegularExpressions; +using Microsoft.Maui.Controls.PlatformConfiguration; using Microsoft.Maui.Controls.PlatformConfiguration.WindowsSpecific; using Label = Microsoft.Maui.Controls.Label; using WebView = Microsoft.Maui.Controls.WebView; diff --git a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs index 5cabf6a52019..58073167991f 100644 --- a/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs +++ b/src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs @@ -14,7 +14,8 @@ public Issue3262(TestDevice testDevice) : base(testDevice) public override string Issue => "Adding Cookies ability to a WebView..."; [Test] - [FailsOnMac] + [FailsOnMacWhenRunningOnXamarinUITest] + [FailsOnWindowsWhenRunningOnXamarinUITest] public void LoadingPageWithoutCookiesSpecifiedDoesntCrash() { App.WaitForElement("SuccessfullPageLoadLabel"); @@ -24,8 +25,8 @@ public void LoadingPageWithoutCookiesSpecifiedDoesntCrash() [Test] [Category(UITestCategories.Compatibility)] - [FailsOnMac] - [FailsOnWindows] + [FailsOnMacWhenRunningOnXamarinUITest] + [FailsOnWindowsWhenRunningOnXamarinUITest] public void ChangeDuringNavigating() { App.WaitForElement("SuccessfullPageLoadLabel"); @@ -38,8 +39,8 @@ public void ChangeDuringNavigating() [Test] [Category(UITestCategories.Compatibility)] - [FailsOnMac] - [FailsOnWindows] + [FailsOnMacWhenRunningOnXamarinUITest] + [FailsOnWindowsWhenRunningOnXamarinUITest] public void AddAdditionalCookieToWebView() { App.WaitForElement("SuccessfullPageLoadLabel"); @@ -52,8 +53,8 @@ public void AddAdditionalCookieToWebView() [Test] [Category(UITestCategories.Compatibility)] - [FailsOnMac] - [FailsOnWindows] + [FailsOnMacWhenRunningOnXamarinUITest] + [FailsOnWindowsWhenRunningOnXamarinUITest] public void SetToOneCookie() { App.WaitForElement("SuccessfullPageLoadLabel"); @@ -63,8 +64,8 @@ public void SetToOneCookie() [Test] [Category(UITestCategories.Compatibility)] - [FailsOnMac] - [FailsOnWindows] + [FailsOnMacWhenRunningOnXamarinUITest] + [FailsOnWindowsWhenRunningOnXamarinUITest] public void SetCookieContainerToNullDisablesCookieManagement() { App.WaitForElement("SuccessfullPageLoadLabel"); @@ -77,8 +78,8 @@ public void SetCookieContainerToNullDisablesCookieManagement() [Test] [Category(UITestCategories.Compatibility)] - [FailsOnMac] - [FailsOnWindows] + [FailsOnMacWhenRunningOnXamarinUITest] + [FailsOnWindowsWhenRunningOnXamarinUITest] public void RemoveAllTheCookiesIAdded() { App.WaitForElement("SuccessfullPageLoadLabel");