Skip to content

Commit 0bd2afc

Browse files
committed
Limit relying on JS execution for checking the element position + increase timeouts for landing page assert that can be too short once in every 20 runs.
1 parent 21051c2 commit 0bd2afc

File tree

2 files changed

+17
-20
lines changed

2 files changed

+17
-20
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,11 @@
33

44
using OpenQA.Selenium;
55
using OpenQA.Selenium.Support.UI;
6-
using System;
76

87
namespace Microsoft.AspNetCore.Components.E2ETest;
98

109
internal static class WebDriverExtensions
1110
{
12-
private static string GetFindPositionScript(string elementId) =>
13-
$"return Math.round(document.getElementById('{elementId}').getBoundingClientRect().top + window.scrollY);";
14-
1511
public static void Navigate(this IWebDriver browser, Uri baseUri, string relativeUrl)
1612
{
1713
var absoluteUrl = new Uri(baseUri, relativeUrl);
@@ -45,32 +41,33 @@ public static void WaitForElementToBeVisible(this IWebDriver browser, By by, int
4541

4642
public static long GetElementPositionWithRetry(this IWebDriver browser, string elementId, int retryCount = 3, int delayBetweenRetriesMs = 100)
4743
{
48-
var jsExecutor = (IJavaScriptExecutor)browser;
49-
string script = GetFindPositionScript(elementId);
50-
browser.WaitForElementToBeVisible(By.Id(elementId));
5144
string log = "";
5245

5346
for (int i = 0; i < retryCount; i++)
5447
{
5548
try
5649
{
57-
var result = jsExecutor.ExecuteScript(script);
58-
if (result != null)
59-
{
60-
return (long)result;
61-
}
50+
browser.WaitForElementToBeVisible(By.Id(elementId));
51+
var element = browser.FindElement(By.Id(elementId));
52+
var elementLocation = element.Location.Y;
53+
54+
// Get scroll position using JavaScript (this is less likely to fail than element positioning)
55+
var jsExecutor = (IJavaScriptExecutor)browser;
56+
var scrollY = (long)jsExecutor.ExecuteScript("return window.scrollY");
6257

63-
log += $"Attempt {i + 1}: Script returned null. ";
58+
return elementLocation + scrollY;
6459
}
65-
catch (OpenQA.Selenium.JavaScriptException ex)
60+
catch (Exception ex)
6661
{
67-
// JavaScript execution failed, retry
68-
log += $"Attempt {i + 1}: JavaScriptException - {ex.Message}. ";
62+
log += $"Attempt {i + 1}: - {ex.Message}. ";
6963
}
7064

71-
Thread.Sleep(delayBetweenRetriesMs);
65+
if (i < retryCount - 1)
66+
{
67+
Thread.Sleep(delayBetweenRetriesMs);
68+
}
7269
}
7370

74-
throw new Exception($"Failed to execute script to get position for element '{elementId}' after {retryCount} retries. Debug log: {log}");
71+
throw new Exception($"Failed to get position for element '{elementId}' after {retryCount} retries. Debug log: {log}");
7572
}
7673
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -874,14 +874,14 @@ private void AssertEnhancedNavigation(bool useEnhancedNavigation, IWebElement el
874874
private void AssertWeAreOnLandingPage()
875875
{
876876
string infoName = "test-info-1";
877-
Browser.WaitForElementToBeVisible(By.Id(infoName), timeoutInSeconds: 20);
877+
Browser.WaitForElementToBeVisible(By.Id(infoName), timeoutInSeconds: 30);
878878
Browser.Equal("Scroll tests landing page", () => Browser.Exists(By.Id(infoName)).Text);
879879
}
880880

881881
private void AssertWeAreOnNextPage()
882882
{
883883
string infoName = "test-info-2";
884-
Browser.WaitForElementToBeVisible(By.Id(infoName), timeoutInSeconds: 20);
884+
Browser.WaitForElementToBeVisible(By.Id(infoName), timeoutInSeconds: 30);
885885
Browser.Equal("Scroll tests next page", () => Browser.Exists(By.Id(infoName)).Text);
886886
}
887887

0 commit comments

Comments
 (0)