Skip to content

[๐Ÿ› Bug]: Selenium tests fail after Chrome version 131 onward updatesย #15678

@gcu5155

Description

@gcu5155

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:

  1. System.InvalidOperationException: Sequence contains no elements
  2. OpenQA.Selenium.StaleElementReferenceException: stale element reference: stale element not found
  3. System.TimeoutException due 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

  1. Use Chrome > 131

  2. Write Selenium UI tests on an MVC app that involves full page refreshes or dynamic API updates

  3. Mock API calls in the test setup

  4. Run the full test suite

  5. Observe random StaleElementReferenceException, InvalidOperationException, or TimeoutException

  6. Downgrade to Chrome v131

  7. Run the exact same test suite

  8. โœ… 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 }));
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions