Skip to content

Commit 5ef86a1

Browse files
committed
Wait for the base page to be loaded to make sure session storage is available.
1 parent 73abbda commit 5ef86a1

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
55
using Microsoft.AspNetCore.E2ETesting;
66
using OpenQA.Selenium;
7+
using OpenQA.Selenium.Support.UI;
78
using Xunit.Abstractions;
89

910
namespace Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
@@ -31,10 +32,48 @@ public void Navigate(string relativeUrl)
3132
Browser.Navigate(_serverFixture.RootUri, relativeUrl);
3233
}
3334

34-
protected override void InitializeAsyncCore()
35+
protected override void InitializeAsyncCore(bool supportEnhancedNavigationSuppression = false)
3536
{
37+
if (supportEnhancedNavigationSuppression)
38+
{
39+
GrantTestId();
40+
}
41+
3642
// Clear logs - we check these during tests in some cases.
3743
// Make sure each test starts clean.
3844
((IJavaScriptExecutor)Browser).ExecuteScript("console.clear()");
45+
46+
InitializeAsyncCore();
47+
}
48+
49+
protected override void InitializeAsyncCore()
50+
{
51+
}
52+
53+
private void GrantTestId()
54+
{
55+
// We have to be on any page to ensure session storage is available
56+
// Since PageLoadStrategy can be set to None, we need to manually wait for page load
57+
Navigate(ServerPathBase);
58+
59+
// Wait for the page to be completely loaded (equivalent to PageLoadStrategy.Normal)
60+
var wait = new OpenQA.Selenium.Support.UI.WebDriverWait(Browser, TimeSpan.FromSeconds(10));
61+
wait.Until(driver =>
62+
{
63+
try
64+
{
65+
// Check if document is ready and load event has fired
66+
var loadComplete = ((IJavaScriptExecutor)driver).ExecuteScript(
67+
"return document.readyState === 'complete' && performance.timing.loadEventEnd > 0");
68+
return (bool)loadComplete;
69+
}
70+
catch
71+
{
72+
return false;
73+
}
74+
});
75+
76+
var testId = Guid.NewGuid().ToString("N")[..8];
77+
((IJavaScriptExecutor)Browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");
3978
}
4079
}

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

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

src/Shared/E2ETesting/BrowserTestBase.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,13 @@ public virtual Task InitializeAsync()
6161
public virtual Task InitializeAsync(string isolationContext, bool supportEnhancedNavigationSuppression = false)
6262
{
6363
InitializeBrowser(isolationContext);
64-
InitializeAsyncCore();
65-
if (supportEnhancedNavigationSuppression)
66-
{
67-
GrantTestId();
68-
}
64+
InitializeAsyncCore(supportEnhancedNavigationSuppression);
6965
return Task.CompletedTask;
7066
}
7167

72-
protected void GrantTestId()
68+
protected virtual void InitializeAsyncCore(bool supportEnhancedNavigationSuppression = false)
7369
{
74-
var testId = Guid.NewGuid().ToString("N")[..8];
75-
((IJavaScriptExecutor)Browser).ExecuteScript($"sessionStorage.setItem('test-id', '{testId}')");
70+
InitializeAsyncCore();
7671
}
7772

7873
protected virtual void InitializeAsyncCore()

0 commit comments

Comments
 (0)