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

在MS托管代理(windows-2022)运行WinAppDriver/Appium测试时出现间歇性WebDriverException

解决WinAppDriver在DevOps托管代理中创建会话超时的问题

背景说明

  • 使用WinAppDriver/Appium自动化WinForm应用
  • 采用Specflow的BDD框架定义测试用例
  • 每次测试启动时会检查应用是否已打开,未打开则调用InitializedAppium(string application)方法初始化应用
  • 该方法中创建WindowsDriver实例的代码var newDriver = new WindowsDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/"), driverOptions);间歇性抛出错误,本地运行正常,但在DevOps的Windows-2022托管代理流水线中频繁出现

具体错误信息

OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://127.0.0.1:4723/session timed out after 60 seconds. ---> System.Net.WebException: The operation has timed out

触发错误的代码

DebugOutput.Log($"Starting {application}");

var driverOptions = new AppiumOptions();

DebugOutput.Log($"NEW {application}");

driverOptions.AddAdditionalCapability(MobileCapabilityType.App, application);

driverOptions.AddAdditionalCapability(MobileCapabilityType.NewCommandTimeout, 500);

driverOptions.AddAdditionalCapability("appium:createSessionTimeout", "20000");

//you may shorten this (<25s) if testing locally, but remember to change it back again)
driverOptions.AddAdditionalCapability("ms:waitForAppLaunch", "50"); 

DebugOutput.Log($"CAPS {application}");

// this is where the failure most times happen when cannot initiate app
var newDriver = new WindowsDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/"), driverOptions);

DebugOutput.Log($"Window Driver Succeeded = {newDriver}");

return newDriver; 

解决建议

除现有配置外,可尝试添加或调整以下AppiumOptions参数,同时结合流水线环境优化解决超时问题:

  • 延长会话创建超时时间
    目前appium:createSessionTimeout设置为20秒,托管代理资源紧张时建议延长至60秒:

    driverOptions.AddAdditionalCapability("appium:createSessionTimeout", "60000");
    
  • 启用实验性WebDriver特性
    针对WinAppDriver,启用该特性可解决部分会话创建兼容性问题:

    driverOptions.AddAdditionalCapability("ms:experimental-webdriver", true);
    
  • 指定应用绝对启动路径
    流水线环境中确保application参数为完整绝对路径,避免因相对路径无法识别导致应用启动延迟或失败。

  • 调整WindowsDriver构造函数超时
    除Appium配置外,直接延长构造函数的超时时间(默认60秒),比如设置为90秒:

    var timeSpan = TimeSpan.FromSeconds(90);
    var newDriver = new WindowsDriver<AppiumWebElement>(new Uri("http://127.0.0.1:4723/"), driverOptions, timeSpan);
    
  • 流水线环境优化

    • 测试前确认WinAppDriver服务已正常启动,可在流水线步骤中添加进程检查脚本。
    • 托管代理可能存在资源竞争,可增加测试前等待时间,或申请更多代理资源配额。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.14 15:52:43