|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Collections.ObjectModel; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using Components.TestServer.RazorComponents; |
| 8 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; |
| 9 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; |
| 10 | +using Microsoft.AspNetCore.E2ETesting; |
| 11 | +using Microsoft.AspNetCore.InternalTesting; |
| 12 | +using OpenQA.Selenium; |
| 13 | +using TestServer; |
| 14 | +using Xunit.Abstractions; |
| 15 | + |
| 16 | +namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests.FormHandlingTests; |
| 17 | + |
| 18 | +public class FormWithNoBackForwardCacheTest : ServerTestBase<BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>>> |
| 19 | +{ |
| 20 | + public FormWithNoBackForwardCacheTest( |
| 21 | + BrowserFixture browserFixture, |
| 22 | + BasicTestAppServerSiteFixture<RazorComponentEndpointsStartup<App>> serverFixture, |
| 23 | + ITestOutputHelper output) |
| 24 | + : base(browserFixture, serverFixture, output) |
| 25 | + { |
| 26 | + } |
| 27 | + |
| 28 | + public override Task InitializeAsync() |
| 29 | + { |
| 30 | + return InitializeAsync(BrowserFixture.StreamingBackForwardCacheContext); |
| 31 | + } |
| 32 | + |
| 33 | + private void SuppressEnhancedNavigation(bool shouldSuppress) |
| 34 | + => EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, shouldSuppress); |
| 35 | + |
| 36 | + [Theory] |
| 37 | + [InlineData(true)] |
| 38 | + [InlineData(false)] |
| 39 | + public void CanUseFormWithMethodGet(bool suppressEnhancedNavigation) |
| 40 | + { |
| 41 | + SuppressEnhancedNavigation(suppressEnhancedNavigation); |
| 42 | + Navigate($"{ServerPathBase}/forms/method-get"); |
| 43 | + Browser.Equal("Form with method=get", () => Browser.FindElement(By.TagName("h2")).Text); |
| 44 | + |
| 45 | + // Validate initial state |
| 46 | + var stringInput = Browser.FindElement(By.Id("mystring")); |
| 47 | + var boolInput = Browser.FindElement(By.Id("mybool")); |
| 48 | + Browser.Equal("Initial value", () => stringInput.GetDomProperty("value")); |
| 49 | + Browser.Equal("False", () => boolInput.GetDomProperty("checked")); |
| 50 | + |
| 51 | + // Edit and submit the form; check it worked |
| 52 | + stringInput.Clear(); |
| 53 | + stringInput.SendKeys("Edited value"); |
| 54 | + boolInput.Click(); |
| 55 | + Browser.FindElement(By.Id("submit-get-form")).Click(); |
| 56 | + AssertUiState("Edited value", true); |
| 57 | + Browser.Contains($"MyString=Edited+value", () => Browser.Url); |
| 58 | + Browser.Contains($"MyBool=True", () => Browser.Url); |
| 59 | + |
| 60 | + // Check 'back' correctly gets us to the previous state |
| 61 | + Browser.Navigate().Back(); |
| 62 | + AssertUiState("Initial value", false); |
| 63 | + Browser.False(() => Browser.Url.Contains("MyString")); |
| 64 | + Browser.False(() => Browser.Url.Contains("MyBool")); |
| 65 | + |
| 66 | + // Check 'forward' correctly recreates the edited state |
| 67 | + Browser.Navigate().Forward(); |
| 68 | + AssertUiState("Edited value", true); |
| 69 | + Browser.Contains($"MyString=Edited+value", () => Browser.Url); |
| 70 | + Browser.Contains($"MyBool=True", () => Browser.Url); |
| 71 | + |
| 72 | + void AssertUiState(string expectedStringValue, bool expectedBoolValue) |
| 73 | + { |
| 74 | + Browser.Equal(expectedStringValue, () => Browser.FindElement(By.Id("mystring-value")).Text); |
| 75 | + Browser.Equal(expectedBoolValue.ToString(), () => Browser.FindElement(By.Id("mybool-value")).Text); |
| 76 | + |
| 77 | + // If we're not suppressing, we'll keep referencing the same elements to show they were preserved |
| 78 | + if (suppressEnhancedNavigation) |
| 79 | + { |
| 80 | + stringInput = Browser.FindElement(By.Id("mystring")); |
| 81 | + boolInput = Browser.FindElement(By.Id("mybool")); |
| 82 | + } |
| 83 | + |
| 84 | + Browser.Equal(expectedStringValue, () => stringInput.GetDomProperty("value")); |
| 85 | + Browser.Equal(expectedBoolValue.ToString(), () => boolInput.GetDomProperty("checked")); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
0 commit comments