Skip to content

Commit 604e029

Browse files
committed
Revert - every test needs testID granted + clean test id in the disposal method.
1 parent 92cad9a commit 604e029

File tree

8 files changed

+64
-28
lines changed

8 files changed

+64
-28
lines changed

src/Components/test/E2ETest/Infrastructure/ServerTestBase.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,56 @@ public void Navigate(string relativeUrl)
3131
Browser.Navigate(_serverFixture.RootUri, relativeUrl);
3232
}
3333

34-
protected override void InitializeAsyncCore()
34+
public override async Task DisposeAsync()
3535
{
36+
CleanupTestId();
37+
38+
await base.DisposeAsync();
39+
}
40+
41+
protected override void InitializeAsyncCore(bool supportEnhancedNavigationSuppression = false)
42+
{
43+
if (supportEnhancedNavigationSuppression)
44+
{
45+
GrantTestId();
46+
}
47+
3648
// Clear logs - we check these during tests in some cases.
3749
// Make sure each test starts clean.
3850
((IJavaScriptExecutor)Browser).ExecuteScript("console.clear()");
51+
52+
InitializeAsyncCore();
53+
}
54+
55+
private void GrantTestId()
56+
{
57+
// We need to navigate here first otherwise the browser isn't on the correct origin to access sessionStorage
58+
Navigate($"{ServerPathBase}/");
59+
Browser.Equal("Hello", () => Browser.Exists(By.TagName("h1")).Text);
60+
61+
try
62+
{
63+
((IJavaScriptExecutor)Browser).ExecuteScript("sessionStorage.length");
64+
}
65+
catch (Exception ex)
66+
{
67+
throw new InvalidOperationException("Session storage not found. Ensure that the browser is on the correct origin.", ex);
68+
}
69+
70+
var testId = Guid.NewGuid().ToString("N")[..8];
71+
((IJavaScriptExecutor)Browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");
72+
}
73+
74+
private void CleanupTestId()
75+
{
76+
try
77+
{
78+
((IJavaScriptExecutor)Browser).ExecuteScript("sessionStorage.length");
79+
}
80+
catch (Exception ex)
81+
{
82+
throw new InvalidOperationException("Session storage not found. Ensure that the browser is on the correct origin.", ex);
83+
}
84+
((IJavaScriptExecutor)Browser).ExecuteScript($"sessionStorage.removeItem('test-id')");
3985
}
4086
}

src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public EnhancedNavigationTest(
3131
// One of the tests here makes use of the streaming rendering page, which uses global state
3232
// so we can't run at the same time as other such tests
3333
public override Task InitializeAsync()
34-
=> InitializeAsync(BrowserFixture.StreamingContext);
34+
=> InitializeAsync(BrowserFixture.StreamingContext, supportEnhancedNavigationSuppression: true);
3535

3636
[Fact]
3737
public void CanNavigateToAnotherPageWhilePreservingCommonDOMElements()
@@ -313,7 +313,7 @@ public void RefreshCanFallBackOnFullPageReload(string renderMode)
313313
Browser.Exists(By.TagName("nav")).FindElement(By.LinkText($"Interactive component navigation ({renderMode})")).Click();
314314
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
315315

316-
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true, skipNavigation: true);
316+
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true);
317317
Browser.Navigate().Refresh();
318318
Browser.Equal("Page with interactive components that navigate", () => Browser.Exists(By.TagName("h1")).Text);
319319

src/Components/test/E2ETest/ServerRenderingTests/EnhancedNavigationTestUtil.cs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,24 @@ namespace Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;
1111

1212
public static class EnhancedNavigationTestUtil
1313
{
14-
public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool shouldSuppress, bool skipNavigation = false)
14+
public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool shouldSuppress)
1515
where TServerFixture : ServerFixture
1616
{
1717
if (shouldSuppress)
1818
{
1919
var browser = fixture.Browser;
20-
21-
if (!skipNavigation)
22-
{
23-
// Normally we need to navigate here first otherwise the browser isn't on the correct origin to access
24-
// sessionStorage. But some tests are already in the right place and need to avoid extra navigation.
25-
fixture.Navigate($"{fixture.ServerPathBase}/");
26-
browser.Equal("Hello", () => browser.Exists(By.TagName("h1")).Text);
27-
}
28-
2920
try
3021
{
3122
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
3223
}
3324
catch (Exception ex)
3425
{
35-
throw new InvalidOperationException("Session storage not found. Ensure that the browser is on the correct origin by navigating to a page or by setting skipNavigation to false.", ex);
26+
throw new InvalidOperationException("Session storage not found. Ensure that the browser is on the correct origin by navigating to a page.", ex);
3627
}
3728
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
3829
if (testId == null)
3930
{
40-
testId = GrantTestId(browser);
31+
throw new InvalidOperationException("Test ID not found in sessionStorage. Ensure that suppression is enabled for test class by passing `supportEnhancedNavigationSuppression: true` to InitializeAsync`.");
4132
}
4233

4334
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('suppress-enhanced-navigation-{testId}', 'true')");
@@ -47,12 +38,6 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
4738
"Expected 'suppress-enhanced-navigation' to be set in sessionStorage.");
4839
}
4940
}
50-
private static string GrantTestId(IWebDriver browser)
51-
{
52-
var testId = Guid.NewGuid().ToString("N")[..8];
53-
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");
54-
return testId;
55-
}
5641

5742
public static long GetScrollY(this IWebDriver browser)
5843
=> Convert.ToInt64(((IJavaScriptExecutor)browser).ExecuteScript("return window.scrollY"), CultureInfo.CurrentCulture);

src/Components/test/E2ETest/ServerRenderingTests/FormHandlingTests/FormWithParentBindingContextTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override Task InitializeAsync()
3232
_tempDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N"));
3333
Directory.CreateDirectory(_tempDirectory);
3434

35-
return InitializeAsync(BrowserFixture.StreamingContext);
35+
return InitializeAsync(BrowserFixture.StreamingContext, supportEnhancedNavigationSuppression: true);
3636
}
3737

3838
[Theory]

src/Components/test/E2ETest/ServerRenderingTests/NoInteractivityTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ public void NotFoundSetOnInitialization_ResponseStarted_SSR(bool hasReExecutionM
285285
[InlineData(false, true)]
286286
public void NotFoundSetOnInitialization_ResponseStarted_EnhancedNavigationDisabled_SSR(bool hasReExecutionMiddleware, bool hasCustomNotFoundPageSet)
287287
{
288-
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true, skipNavigation: true);
288+
EnhancedNavigationTestUtil.SuppressEnhancedNavigation(this, true);
289289
string reexecution = hasReExecutionMiddleware ? "/reexecution" : "";
290290
string testUrl = $"{ServerPathBase}{reexecution}/set-not-found-ssr-streaming?useCustomNotFoundPage={hasCustomNotFoundPageSet}";
291291
Navigate(testUrl);

src/Components/test/E2ETest/ServerRenderingTests/StreamingRenderingTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public StreamingRenderingTest(
2727
}
2828

2929
public override Task InitializeAsync()
30-
=> InitializeAsync(BrowserFixture.StreamingContext);
30+
=> InitializeAsync(BrowserFixture.StreamingContext, supportEnhancedNavigationSuppression: true);
3131

3232
[Fact]
3333
public async Task CanRenderNonstreamingPageWithoutInjectingStreamingMarkersOrHeaders()

src/Components/test/E2ETest/Tests/StatePersistenceTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public StatePersistenceTest(
2929

3030
// Separate contexts to ensure that caches and other state don't interfere across tests.
3131
public override Task InitializeAsync()
32-
=> InitializeAsync(BrowserFixture.StreamingContext + _nextStreamingIdContext++);
32+
=> InitializeAsync(BrowserFixture.StreamingContext + _nextStreamingIdContext++, supportEnhancedNavigationSuppression: true);
3333

3434
// Validates that we can use persisted state across server, webassembly, and auto modes, with and without
3535
// streaming rendering.

src/Shared/E2ETesting/BrowserTestBase.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public IWebDriver Browser
4949

5050
public BrowserFixture BrowserFixture { get; }
5151

52-
public Task DisposeAsync()
52+
public virtual Task DisposeAsync()
5353
{
5454
return Task.CompletedTask;
5555
}
@@ -59,13 +59,18 @@ public virtual Task InitializeAsync()
5959
return InitializeAsync("");
6060
}
6161

62-
public virtual Task InitializeAsync(string isolationContext)
62+
public virtual Task InitializeAsync(string isolationContext, bool supportEnhancedNavigationSuppression = false)
6363
{
6464
InitializeBrowser(isolationContext);
65-
InitializeAsyncCore();
65+
InitializeAsyncCore(supportEnhancedNavigationSuppression);
6666
return Task.CompletedTask;
6767
}
6868

69+
protected virtual void InitializeAsyncCore(bool supportEnhancedNavigationSuppression = false)
70+
{
71+
InitializeAsyncCore();
72+
}
73+
6974
protected virtual void InitializeAsyncCore()
7075
{
7176
}

0 commit comments

Comments
 (0)