You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure DevOps中VsTest运行Selenium测试不稳定问题排查

Azure DevOps发布流水线中Selenium WebDriver测试不稳定问题

我们在Azure DevOps发布流水线中遇到Selenium WebDriver测试运行不一致的问题,已遵循微软官方教程配置。测试有时正常执行,有时启动后无日志输出直接超时,所有测试在本地运行正常,多次执行代码无变化但结果不同:

  • 第1次执行全部通过
  • 第2次执行超时
  • 第3次执行超时
  • 第4次执行失败

C#示例测试代码

public class HomePageShould : IClassFixture<ChromeDriverFixture>
{
    private readonly IWebDriver _driver;

    public HomePageShould(ChromeDriverFixture chromeDriverFixture)
    {
        _driver = chromeDriverFixture.Driver;
        _driver.Manage().Cookies.DeleteAllCookies();
        _driver.Navigate().GoToUrl("about:blank");
        _driver.Manage().Window.Maximize();
    }

    [Fact]
    public void LogInWithoutForm()
    {
        try
        {
            //Act
            AuthenticatedHomePage? authenticatedHomePage = new(_driver, UserDictionary.UserNameAdmin, UserDictionary.PasswordAdmin);

            //Assert
            Assert.Equal(authenticatedHomePage.Title, _driver.Title);
            Assert.Equal(authenticatedHomePage.Url, _driver.Url);
            Assert.Equal(authenticatedHomePage.WelcomeText, AuthenticatedHomePage.ExpectedWelcomeText);

        }
        catch (Exception ex)
        {
            Console.WriteLine($"Something went wrong in testing, {ex.Message}");
            throw new ApplicationException($"Something went wrong in testing, {ex.Message}");
        }
        finally
        {
            _driver.Quit();
        }
    }
}

认证逻辑相关代码

AuthenticatedHomePage通过设置浏览器sessionStorage完成认证,使用Selenium显式等待,相关代码如下:

public AuthenticatedPage(IWebDriver driver, string usernameConfig, string passwordConfig) : base(driver)
{
    Driver.Navigate().GoToUrl(UrlWithoutRedirect);

    try
    {
        MsalAuthenticator msalAuthenticator = new(usernameConfig, passwordConfig);
        msalAuthenticator.AcquireAndSetTokensAsync(this.Driver).Wait();
    }
    catch
    {
        throw;
    }

    WebDriverWait waitForAuth = new(this.Driver, TimeSpan.FromSeconds(WaitTimeConstants.DriveTimeOut));
    Driver.Navigate().GoToUrl(BaseUrl);
    waitForAuth.Until(ExpectedConditions.ElementIsVisible(AuthenticatedHomePage.WelcomeTextLocation));
}

VsTest步骤YAML配置

最初将Dev与UI测试放在同一步骤,后来分离以排除Dev实例未启动的问题,并添加了前置等待。VsTest步骤的YAML配置如下:

steps:
- task: VSTest@2
  displayName: 'VsTest - testAssemblies'
  inputs:
    testAssemblyVer2: |
     **\*UITests.dll
     !**\*TestAdapter.dll
     !**\obj\**
    searchFolder: '$(System.DefaultWorkingDirectory)/Web/drop/CP.UITests'
    uiTests: true
    vsTestVersion: toolsInstaller
    codeCoverageEnabled: false
    publishRunAttachments: false
  timeoutInMinutes: 15
  retryCountOnTaskFailure: 3

已尝试的解决方案

  • 使用微软屏幕分辨率工具,未解决问题
  • 设置Chrome的--no-sandbox参数(非Linux环境不推荐),搭配incognito模式,无效
  • 更换其他浏览器驱动,无效
  • 修改WebDriver超时时间为几分钟,无效

补充说明

2023年4月13日,移除认证逻辑后测试运行恢复稳定,确认问题源于认证模式而非测试运行器,现需明确该认证模式的问题根源及优化方案。


内容的提问来源于stack exchange,提问作者domshyra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 03:18:24