-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
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 | ||
{ | ||
|
@@ -13,19 +14,16 @@ public class Issue3262 : TestContentPage // or TestFlyoutPage, etc ... | |
|
||
protected override void Init() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The build is failing:
Could you review it? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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; | ||
|
@@ -84,7 +88,12 @@ protected override void Init() | |
{ | ||
Text = "Modify the Cookie Container" | ||
}, | ||
cookieResult, | ||
new HorizontalStackLayout() | ||
{ | ||
cookieResult, | ||
successfullPageLoadLabel, | ||
successCookiesLabel | ||
}, | ||
new StackLayout() | ||
{ | ||
Orientation = StackOrientation.Horizontal, | ||
|
@@ -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)) | ||
|
@@ -106,7 +116,7 @@ protected override void Init() | |
} | ||
else | ||
{ | ||
cookieResult.Text = "Success"; | ||
successCookiesLabel.IsVisible = true; | ||
} | ||
}; | ||
|
||
|
@@ -134,7 +144,7 @@ protected override void Init() | |
} | ||
else | ||
{ | ||
cookieResult.Text = "Success"; | ||
successCookiesLabel.IsVisible = true; | ||
} | ||
}; | ||
|
||
|
@@ -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) | ||
|
@@ -157,7 +168,7 @@ protected override void Init() | |
} | ||
else | ||
{ | ||
cookieResult.Text = "Success"; | ||
successCookiesLabel.IsVisible = true; | ||
} | ||
}; | ||
|
||
|
@@ -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}", | ||
|
@@ -212,7 +224,7 @@ protected override void Init() | |
} | ||
else | ||
{ | ||
cookieResult.Text = "Success"; | ||
successCookiesLabel.IsVisible = true; | ||
} | ||
}; | ||
|
||
|
@@ -242,6 +254,7 @@ protected override void Init() | |
}; | ||
|
||
cookieResult.Text = String.Empty; | ||
successCookiesLabel.IsVisible = false; | ||
cookieExpectation = (cookieValue) => | ||
{ | ||
if(cookieValue.Contains(cookieToAdd.Name, StringComparison.OrdinalIgnoreCase)) | ||
|
@@ -250,7 +263,7 @@ protected override void Init() | |
} | ||
else | ||
{ | ||
cookieResult.Text = "Success"; | ||
successCookiesLabel.IsVisible = true; | ||
} | ||
}; | ||
|
||
|
@@ -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; | ||
}) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks