-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Description
Description
Description
We use Selenium WebDriver (Version 4.31.0) in a .NET MVC application to test UI behavior. The application internally calls APIs, which are mocked during test execution. We are writing our tests using xUnit and running them via the Visual Studio Test Explorer or command line.
Everything worked perfectly while using Chrome version 131.
After Chrome updated beyond version 131, the tests randomly started failing, especially when we run all tests together. The failures mostly seem to be related to page refreshes, DOM updates, or element detachment.
โ ๏ธ Rolling back to Chrome version 131 resolves all these issues, with zero test failures.
Exceptions Observed
When executing the full test suite after updating Chrome, we encounter the following exceptions repeatedly:
System.InvalidOperationException: Sequence contains no elementsOpenQA.Selenium.StaleElementReferenceException: stale element reference: stale element not foundSystem.TimeoutExceptiondue to actions timing out on refreshed or updated elements
โ ๏ธ Rolling back to Chrome version 131 resolves all these issues, with zero test failures.
What We Tried
- Confirmed ChromeDriver version matches the updated Chrome version
- Added explicit and fluent waits for element visibility/clickability
- Added Retry functionality when test fails.
- Wrapped interactions in retry logic with exception handling
- Disabled animations and async behaviors
- Ran tests in headless and headed modes
- Also tried the below WAIT code.
_webDriverWait.Until(d =>
((IJavaScriptExecutor)d).ExecuteScript("return document.readyState").ToString().Equals("complete"));
Despite all these efforts, the test failures persist after Chrome v131.
Steps to Reproduce
-
Use Chrome > 131
-
Write Selenium UI tests on an MVC app that involves full page refreshes or dynamic API updates
-
Mock API calls in the test setup
-
Run the full test suite
-
Observe random
StaleElementReferenceException,InvalidOperationException, orTimeoutException -
Downgrade to Chrome v131
-
Run the exact same test suite
-
โ All tests pass consistently
Hypothesis
We believe the issue is likely triggered during page reloads or DOM refreshes. Elements seem to go stale more frequently, or are no longer available in the DOM in the expected state, after Chrome v131.
It seems like something in the newer Chrome rendering behavior is causing Selenium's references to go invalid more aggressively than before.
Reproducible Code
internal class UserPage : BasePage
{
public UserPage(IWebDriver driver) : base(driver)
{
}
public IWebElement FirstName => Driver.FindElement(By.CssSelector("[data-testid=firstname-input]"));
public IWebElement LastName => Driver.FindElement(By.CssSelector("[data-testid=lastname-input]"));
public IWebElement Email => Driver.FindElement(By.CssSelector("[data-testid=email-input]"));
public IWebElement Roles => Driver.FindElement(By.CssSelector("[data-testid=role-select]"));
public IWebElement NextButton => Driver.FindElement(By.CssSelector("button[value=Next]"));
public IReadOnlyCollection<IWebElement> SelectedSubgroups => Driver.FindElements(By.CssSelector("[data-testid=selected-subgroups-input]"));
}
//------------------------------------------------------------------------------------------
[Fact]
public void AddClass_AddClassButton_AddsClass_RemainsOnAddClassStep()
{
// Arrange
httpMockServer.Reset();
_rootUrl = AppSettings.GetConfiguration()?.Url ?? factory.RootUrl;
_url = $"{_rootUrl}/users/invite";
_driver = DriverFactory.BuildDriver(_url, true);
_userPage = new UserPage(_driver);
// Create Mock
UserMocks.SetupUserSearchMock(httpMockServer.MockServer);
const string firstName = "John";
const string secondName = "Smith";
const string email = "[email protected]";
const UserRole role = UserRole.AdultStudent;
// Act
_userPage.FirstName.SendKeys(firstName);
_userPage.LastName.SendKeys(secondName);
_userPage.Email.SendKeys(email);
_userPage.SelectRole(role);
_userPage.NextButton.Click(); // Throws exception of Stale Element
// Assert
Assert.False(_userPage.SelectedSubgroups.Last().Enabled); // Throws exception sometime here
}
//------------------------------------------------------------------------------------------
public static class UserMocks
{
public static void SetupUserSearchMock(WireMockServer mockServer, params
IDomainUserProfileSummary[] searchResults)
{
mockServer.Given(
Request.Create()
.WithPath("/UserService/User")
.UsingMethod("SEARCH"))
.RespondWith(
Response.Create().WithStatusCode(HttpStatusCode.OK)
.WithBodyAsJson(new PageResponse<IDomainUserProfileSummary> {
Records = searchResults }));
}
}