Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kubaflo committed Oct 1, 2024
1 parent 0de68dd commit acaee76
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 99 deletions.
41 changes: 15 additions & 26 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,29 +42,27 @@ 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"
};

var successNavigationLabel = new Label()
var successfullPageLoadLabel = new Label()
{
IsVisible = false,
Text = "Page was loaded",
AutomationId = "SuccessNavigationLabel"
AutomationId = "SuccessfullPageLoadLabel"
};

var successCookiesLabel = new Label()
Expand All @@ -77,20 +72,9 @@ protected override void Init()
AutomationId = "SuccessCookiesLabel"
};

webView.Navigating += (_, __) =>
{
if (cookieExpectation != null)
cookieResult.Text = "Navigating";
};

webView.Navigated += async (_, __) =>
{
if (cookieResult.Text == "Loading")
{
cookieResult.Text = "Loaded";
successNavigationLabel.IsVisible = true;
}

successfullPageLoadLabel.IsVisible = true;
_currentCookieValue = await webView.EvaluateJavaScriptAsync("document.cookie");
cookieExpectation?.Invoke(_currentCookieValue);
cookieExpectation = null;
Expand All @@ -110,7 +94,7 @@ protected override void Init()
new HorizontalStackLayout()
{
cookieResult,
successNavigationLabel,
successfullPageLoadLabel,
successCookiesLabel
},
new StackLayout()
Expand All @@ -125,7 +109,7 @@ protected override void Init()
Command = new Command(() =>
{
webView.Cookies = cookieContainer;
cookieResult.Text = String.Empty;
cookieResult.Text = string.Empty;
successCookiesLabel.IsVisible = false;
cookieExpectation = (cookieValue) =>
{
Expand Down Expand Up @@ -307,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
115 changes: 42 additions & 73 deletions src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue3262.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,113 +11,82 @@ public Issue3262(TestDevice testDevice) : base(testDevice)
{
}

protected override bool ResetAfterEachTest => true;
public override string Issue => "Adding Cookies ability to a WebView...";


[Test]
[FailsOnMac]
public void LoadingPageWithoutCookiesSpecifiedDoesntCrash()
{
try
{
App.Tap("PageWithoutCookies");
App.WaitForElement("PageWithoutCookies");
}
catch
{
Assert.Fail();
}
App.WaitForElement("SuccessfullPageLoadLabel");
App.Tap("PageWithoutCookies");
App.WaitForElement("PageWithoutCookies");
}

[Test]
[Category(UITestCategories.Compatibility)]
[FailsOnMac]
[FailsOnWindows]
public void ChangeDuringNavigating()
{
try
{
App.WaitForElement("SuccessNavigationLabel");
// add a couple cookies
App.Tap("ChangeDuringNavigating");
ValidateSuccess();
App.Tap("ChangeDuringNavigating");
ValidateSuccess();
}
catch
{
Assert.Fail();
}
App.WaitForElement("SuccessfullPageLoadLabel");
// add a couple cookies
App.Tap("ChangeDuringNavigating");
ValidateSuccess();
App.Tap("ChangeDuringNavigating");
ValidateSuccess();
}

[Test]
[Category(UITestCategories.Compatibility)]
[FailsOnMac]
[FailsOnWindows]
public void AddAdditionalCookieToWebView()
{
try
{
App.WaitForElement("SuccessNavigationLabel");
// add a couple cookies
App.Tap("AdditionalCookie");
ValidateSuccess();
App.Tap("AdditionalCookie");
ValidateSuccess();
}
catch
{
Assert.Fail();
}
App.WaitForElement("SuccessfullPageLoadLabel");
// add a couple cookies
App.Tap("AdditionalCookie");
ValidateSuccess();
App.Tap("AdditionalCookie");
ValidateSuccess();
}

[Test]
[Category(UITestCategories.Compatibility)]
[FailsOnMac]
[FailsOnWindows]
public void SetToOneCookie()
{
try
{
App.WaitForElement("SuccessNavigationLabel");
App.Tap("OneCookie");
ValidateSuccess();
}
catch
{
Assert.Fail();
}
App.WaitForElement("SuccessfullPageLoadLabel");
App.Tap("OneCookie");
ValidateSuccess();
}

[Test]
[Category(UITestCategories.Compatibility)]
[FailsOnMac]
[FailsOnWindows]
public void SetCookieContainerToNullDisablesCookieManagement()
{
try
{
App.WaitForElement("SuccessNavigationLabel");
// add a cookie to verify said cookie remains
App.Tap("AdditionalCookie");
ValidateSuccess();
App.Tap("NullAllCookies");
ValidateSuccess();
}
catch
{
Assert.Fail();
}
App.WaitForElement("SuccessfullPageLoadLabel");
// add a cookie to verify said cookie remains
App.Tap("AdditionalCookie");
ValidateSuccess();
App.Tap("NullAllCookies");
ValidateSuccess();
}

[Test]
[Category(UITestCategories.Compatibility)]
[FailsOnMac]
[FailsOnWindows]
public void RemoveAllTheCookiesIAdded()
{
try
{
App.WaitForElement("SuccessNavigationLabel");
// add a cookie so you can remove a cookie
App.Tap("AdditionalCookie");
ValidateSuccess();
App.Tap("EmptyAllCookies");
ValidateSuccess();
}
catch
{
Assert.Fail();
}
App.WaitForElement("SuccessfullPageLoadLabel");
// add a cookie so you can remove a cookie
App.Tap("AdditionalCookie");
ValidateSuccess();
App.Tap("EmptyAllCookies");
ValidateSuccess();
}

void ValidateSuccess()
Expand Down

0 comments on commit acaee76

Please sign in to comment.