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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
61 changes: 38 additions & 23 deletions src/Controls/tests/TestCases.HostApp/Issues/Issue3262.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,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
Expand All @@ -45,35 +42,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<Microsoft.Maui.Controls.PlatformConfiguration.Windows>().SetIsJavaScriptAlertEnabled(true);

webView.On<Windows>().SetIsJavaScriptAlertEnabled(true);

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 @@ -90,7 +91,12 @@ protected override void Init()
{
Text = "Modify the Cookie Container"
},
cookieResult,
new HorizontalStackLayout()
{
cookieResult,
successfullPageLoadLabel,
successCookiesLabel
},
new StackLayout()
{
Orientation = StackOrientation.Horizontal,
Expand All @@ -103,7 +109,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 @@ -112,7 +119,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

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

Expand All @@ -155,6 +162,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 @@ -163,7 +171,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

Expand Down Expand Up @@ -196,6 +204,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 @@ -218,7 +227,7 @@ protected override void Init()
}
else
{
cookieResult.Text = "Success";
successCookiesLabel.IsVisible = true;
}
};

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

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

Expand All @@ -281,8 +291,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,21 @@ public Issue3262(TestDevice testDevice) : base(testDevice)
public override string Issue => "Adding Cookies ability to a WebView...";

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

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

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

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

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatforms]
[FailsOnMac]
[FailsOnWindows]
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 +76,12 @@ public void SetCookieContainerToNullDisablesCookieManagement()
}

[Test]
[Category(UITestCategories.WebView)]
[Category(UITestCategories.Compatibility)]
[FailsOnAllPlatforms]
[FailsOnMac]
[FailsOnWindows]
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 +93,7 @@ void ValidateSuccess()
{
try
{
App.WaitForElement("Success");
App.WaitForElement("SuccessCookiesLabel");
}
catch
{
Expand Down