From 91a6fcc93d96530f3d9ad15c2942ff167d2e8e46 Mon Sep 17 00:00:00 2001
From: Steve Sanderson <SteveSandersonMS@users.noreply.github.com>
Date: Fri, 6 Sep 2019 16:20:14 +0100
Subject: [PATCH] Improve reliability of globalization E2E tests (#13678)

---
 .../ServerExecutionTests/GlobalizationTest.cs | 49 +++++++------------
 .../ServerExecutionTests/LocalizationTest.cs  |  5 +-
 .../BasicTestApp/CulturePicker.razor          |  2 +-
 .../Controllers/CultureController.cs          |  8 ++-
 4 files changed, 31 insertions(+), 33 deletions(-)

diff --git a/src/Components/test/E2ETest/ServerExecutionTests/GlobalizationTest.cs b/src/Components/test/E2ETest/ServerExecutionTests/GlobalizationTest.cs
index ae4c699153f..af0ef6980e5 100644
--- a/src/Components/test/E2ETest/ServerExecutionTests/GlobalizationTest.cs
+++ b/src/Components/test/E2ETest/ServerExecutionTests/GlobalizationTest.cs
@@ -41,16 +41,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
         public void CanSetCultureAndParseCultueSensitiveNumbersAndDates(string culture)
         {
             var cultureInfo = CultureInfo.GetCultureInfo(culture);
-
-            var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
-            selector.SelectByValue(culture);
-
-            // That should have triggered a redirect, wait for the main test selector to come up.
-            MountTestComponent<GlobalizationBindCases>();
-            WaitUntilExists(By.Id("globalization-cases"));
-
-            var cultureDisplay = WaitUntilExists(By.Id("culture-name-display"));
-            Assert.Equal($"Culture is: {culture}", cultureDisplay.Text);
+            SetCulture(culture);
 
             // int
             var input = Browser.FindElement(By.Id("input_type_text_int"));
@@ -113,16 +104,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
         public void CanSetCultureAndParseCultureInvariantNumbersAndDatesWithInputFields(string culture)
         {
             var cultureInfo = CultureInfo.GetCultureInfo(culture);
-
-            var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
-            selector.SelectByValue(culture);
-
-            // That should have triggered a redirect, wait for the main test selector to come up.
-            MountTestComponent<GlobalizationBindCases>();
-            WaitUntilExists(By.Id("globalization-cases"));
-
-            var cultureDisplay = WaitUntilExists(By.Id("culture-name-display"));
-            Assert.Equal($"Culture is: {culture}", cultureDisplay.Text);
+            SetCulture(culture);
 
             // int
             var input = Browser.FindElement(By.Id("input_type_number_int"));
@@ -179,16 +161,7 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
         public void CanSetCultureAndParseCultureInvariantNumbersAndDatesWithFormComponents(string culture)
         {
             var cultureInfo = CultureInfo.GetCultureInfo(culture);
-
-            var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
-            selector.SelectByValue(culture);
-
-            // That should have triggered a redirect, wait for the main test selector to come up.
-            MountTestComponent<GlobalizationBindCases>();
-            WaitUntilExists(By.Id("globalization-cases"));
-
-            var cultureDisplay = WaitUntilExists(By.Id("culture-name-display"));
-            Assert.Equal($"Culture is: {culture}", cultureDisplay.Text);
+            SetCulture(culture);
 
             // int
             var input = Browser.FindElement(By.Id("inputnumber_int"));
@@ -247,5 +220,21 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
             element.SendKeys(Keys.Control + "a");
             element.SendKeys(text);
         }
+
+        private void SetCulture(string culture)
+        {
+            var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
+            selector.SelectByValue(culture);
+
+            // Click the link to return back to the test page
+            WaitUntilExists(By.ClassName("return-from-culture-setter")).Click();
+
+            // That should have triggered a page load, so wait for the main test selector to come up.
+            MountTestComponent<GlobalizationBindCases>();
+            WaitUntilExists(By.Id("globalization-cases"));
+
+            var cultureDisplay = WaitUntilExists(By.Id("culture-name-display"));
+            Assert.Equal($"Culture is: {culture}", cultureDisplay.Text);
+        }
     }
 }
diff --git a/src/Components/test/E2ETest/ServerExecutionTests/LocalizationTest.cs b/src/Components/test/E2ETest/ServerExecutionTests/LocalizationTest.cs
index 94faed00bcd..3faa49879d6 100644
--- a/src/Components/test/E2ETest/ServerExecutionTests/LocalizationTest.cs
+++ b/src/Components/test/E2ETest/ServerExecutionTests/LocalizationTest.cs
@@ -40,7 +40,10 @@ namespace Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests
             var selector = new SelectElement(Browser.FindElement(By.Id("culture-selector")));
             selector.SelectByValue(culture);
 
-            // That should have triggered a redirect, wait for the main test selector to come up.
+            // Click the link to return back to the test page
+            WaitUntilExists(By.ClassName("return-from-culture-setter")).Click();
+
+            // That should have triggered a page load, so wait for the main test selector to come up.
             MountTestComponent<LocalizedText>();
 
             var cultureDisplay = WaitUntilExists(By.Id("culture-name-display"));
diff --git a/src/Components/test/testassets/BasicTestApp/CulturePicker.razor b/src/Components/test/testassets/BasicTestApp/CulturePicker.razor
index 6e20528c69c..7a367ba7662 100644
--- a/src/Components/test/testassets/BasicTestApp/CulturePicker.razor
+++ b/src/Components/test/testassets/BasicTestApp/CulturePicker.razor
@@ -12,7 +12,7 @@
     {
         // Included fragment to preserve choice of Blazor client or server.
         var redirect = new Uri(NavigationManager.Uri).GetComponents(UriComponents.PathAndQuery | UriComponents.Fragment, UriFormat.UriEscaped);
-        var query = $"?culture={Uri.EscapeDataString((string)e.Value)}&redirectUri={redirect}";
+        var query = $"?culture={Uri.EscapeDataString((string)e.Value)}&redirectUri={Uri.EscapeDataString(redirect)}";
         NavigationManager.NavigateTo("/Culture/SetCulture" + query, forceLoad: true);
     }
 }
diff --git a/src/Components/test/testassets/TestServer/Controllers/CultureController.cs b/src/Components/test/testassets/TestServer/Controllers/CultureController.cs
index f3cbaf8286e..4466bb59c35 100644
--- a/src/Components/test/testassets/TestServer/Controllers/CultureController.cs
+++ b/src/Components/test/testassets/TestServer/Controllers/CultureController.cs
@@ -1,5 +1,8 @@
+using System.Text.Encodings.Web;
 using Microsoft.AspNetCore.Localization;
 using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.WebUtilities;
+using Microsoft.Net.Http.Headers;
 
 namespace Components.TestServer.Controllers
 {
@@ -15,7 +18,10 @@ namespace Components.TestServer.Controllers
                     CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)));
             }
 
-            return LocalRedirect(redirectUri);
+            var htmlEncoder = HtmlEncoder.Default;
+            var html = $"<h1>Culture has been changed to {htmlEncoder.Encode(culture)}</h1>" +
+                $"<a class='return-from-culture-setter' href='{htmlEncoder.Encode(redirectUri)}'>Return to previous page</a>";
+            return Content(html, "text/html");
         }
     }
 }
-- 
GitLab