|
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
4 | 4 | using Components.TestServer.RazorComponents; |
| 5 | +using Microsoft.AspNetCore.Components.E2ETest; |
5 | 6 | using Microsoft.AspNetCore.Components.E2ETest.Infrastructure; |
6 | 7 | using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; |
7 | 8 | using Microsoft.AspNetCore.E2ETesting; |
@@ -59,19 +60,34 @@ public void FocusIsMoved_OnEnhancedNavigation_WhenAnyElementMatchesSelector() |
59 | 60 | { |
60 | 61 | Navigate($"{ServerPathBase}/focus-on-navigate"); |
61 | 62 | Browser.Click(By.LinkText("Statically rendered")); |
62 | | - Browser.True(() => Browser.SwitchTo().ActiveElement().GetDomAttribute("data-focus-on-navigate") is not null); |
| 63 | + Browser.True( |
| 64 | + () => Browser.SwitchTo().ActiveElement().GetDomAttribute("data-focus-on-navigate") is not null, |
| 65 | + TimeSpan.FromSeconds(5) |
| 66 | + ); |
63 | 67 | } |
64 | 68 |
|
65 | 69 | [Fact] |
66 | 70 | public void FocusIsPreserved_OnEnhancedFormPost_WhenAnyElementMatchesSelector() |
67 | 71 | { |
68 | 72 | Navigate($"{ServerPathBase}/focus-on-navigate"); |
69 | 73 | Browser.Click(By.LinkText("Form submission")); |
70 | | - Browser.True(() => Browser.SwitchTo().ActiveElement().GetDomAttribute("id") == "value-to-submit"); |
71 | | - Browser.FindElement(By.Id("value-to-submit")).ReplaceText("Some value"); |
72 | | - Browser.Click(By.Id("submit-button")); |
| 74 | + string valueToSubmit = "value-to-submit"; |
| 75 | + AssertFocusPreserved(valueToSubmit); |
| 76 | + Browser.FindElement(By.Id(valueToSubmit)).ReplaceText("Some value"); |
| 77 | + string submitButtonId = "submit-button"; |
| 78 | + Browser.Click(By.Id(submitButtonId)); |
73 | 79 | Browser.Equal("Some value", () => Browser.FindElement(By.Id("submitted-value")).Text); |
74 | | - Browser.True(() => Browser.SwitchTo().ActiveElement().GetDomAttribute("id") == "submit-button"); |
| 80 | + AssertFocusPreserved(submitButtonId); |
| 81 | + } |
| 82 | + |
| 83 | + private void AssertFocusPreserved(string elementId) |
| 84 | + { |
| 85 | + Browser.WaitForElementToBeVisible(By.Id(elementId)); |
| 86 | + Browser.True( |
| 87 | + () => Browser.SwitchTo().ActiveElement().GetDomAttribute("id") == elementId, |
| 88 | + TimeSpan.FromSeconds(5), |
| 89 | + $"Expected element with id '{elementId}' to be focused, but found '{Browser.SwitchTo().ActiveElement().GetDomAttribute("id")}' instead." |
| 90 | + ); |
75 | 91 | } |
76 | 92 |
|
77 | 93 | [Fact] |
|
0 commit comments