Skip to content

Commit 7ac455c

Browse files
committed
Grant ID for each test on initialization. Change BinaryHttpClientTest inheritance to ServerTestBase, so that it calls ID granting method as well.
1 parent 0bd2afc commit 7ac455c

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,9 @@ protected override void InitializeAsyncCore()
4444
// Make sure each test starts clean.
4545
((IJavaScriptExecutor)Browser).ExecuteScript("console.clear()");
4646
}
47+
48+
protected override void GrantTestId()
49+
{
50+
EnhancedNavigationTestUtil.GrantTestId(this);
51+
}
4752
}

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
2222
{
2323
NavigateToOrigin(fixture);
2424
}
25-
26-
try
27-
{
28-
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
29-
}
30-
catch (Exception ex)
31-
{
32-
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);
33-
}
25+
AssertSessionStorageAvailable(browser);
3426

3527
var testId = ((IJavaScriptExecutor)browser).ExecuteScript($"return sessionStorage.getItem('test-id')");
3628
if (testId is null || string.IsNullOrEmpty(testId as string))
3729
{
38-
testId = GrantTestId(browser);
30+
throw new InvalidOperationException("Test ID not found in sessionStorage. Make sure your test class grants it in InitializeAsync.");
3931
}
4032

4133
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('suppress-enhanced-navigation-{testId}', 'true')");
@@ -46,6 +38,27 @@ public static void SuppressEnhancedNavigation<TServerFixture>(ServerTestBase<TSe
4638
}
4739
}
4840

41+
public static void AssertSessionStorageAvailable(IWebDriver browser)
42+
{
43+
try
44+
{
45+
((IJavaScriptExecutor)browser).ExecuteScript("sessionStorage.length");
46+
}
47+
catch (Exception ex)
48+
{
49+
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);
50+
}
51+
52+
}
53+
54+
public static void GrantTestId<TServerFixture>(ServerTestBase<TServerFixture> fixture)
55+
where TServerFixture : ServerFixture
56+
{
57+
NavigateToOrigin(fixture);
58+
AssertSessionStorageAvailable(fixture.Browser);
59+
GrantTestIdCore(fixture.Browser);
60+
}
61+
4962
public static void CleanEnhancedNavigationSuppression<TServerFixture>(ServerTestBase<TServerFixture> fixture, bool skipNavigation = false)
5063
where TServerFixture : ServerFixture
5164
{
@@ -94,7 +107,7 @@ private static void NavigateToOrigin<TServerFixture>(ServerTestBase<TServerFixtu
94107
fixture.Browser.Equal("Hello", () => fixture.Browser.Exists(By.TagName("h1")).Text);
95108
}
96109

97-
private static string GrantTestId(IWebDriver browser)
110+
private static string GrantTestIdCore(IWebDriver browser)
98111
{
99112
var testId = Guid.NewGuid().ToString("N")[..8];
100113
((IJavaScriptExecutor)browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using BasicTestApp.HttpClientTest;
5+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
56
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
7+
using Microsoft.AspNetCore.Components.E2ETests.ServerRenderingTests;
68
using Microsoft.AspNetCore.E2ETesting;
79
using OpenQA.Selenium;
810
using TestServer;
911
using Xunit.Abstractions;
1012

1113
namespace Microsoft.AspNetCore.Components.E2ETest.Tests;
1214

13-
public class BinaryHttpClientTest : BrowserTestBase,
15+
public class BinaryHttpClientTest : ServerTestBase<AspNetSiteServerFixture>,
1416
IClassFixture<BasicTestAppServerSiteFixture<CorsStartup>>,
1517
IClassFixture<BlazorWasmTestAppFixture<BasicTestApp.Program>>
1618
{
@@ -23,11 +25,13 @@ public class BinaryHttpClientTest : BrowserTestBase,
2325

2426
public BinaryHttpClientTest(
2527
BrowserFixture browserFixture,
28+
AspNetSiteServerFixture serverFixture,
2629
BlazorWasmTestAppFixture<BasicTestApp.Program> devHostServerFixture,
2730
BasicTestAppServerSiteFixture<CorsStartup> apiServerFixture,
2831
ITestOutputHelper output)
29-
: base(browserFixture, output)
32+
: base(browserFixture, serverFixture, output)
3033
{
34+
serverFixture.BuildWebHostMethod = Program.BuildWebHost<CorsStartup>;
3135
_devHostServerFixture = devHostServerFixture;
3236
_devHostServerFixture.PathBase = "/subdir";
3337
_apiServerFixture = apiServerFixture;

src/Shared/E2ETesting/BrowserTestBase.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ public virtual Task InitializeAsync()
6262
public virtual Task InitializeAsync(string isolationContext)
6363
{
6464
InitializeBrowser(isolationContext);
65+
GrantTestId();
6566
InitializeAsyncCore();
6667
return Task.CompletedTask;
6768
}
@@ -70,6 +71,10 @@ protected virtual void InitializeAsyncCore()
7071
{
7172
}
7273

74+
protected virtual void GrantTestId()
75+
{
76+
}
77+
7378
protected void InitializeBrowser(string isolationContext)
7479
{
7580
try

0 commit comments

Comments
 (0)