如何在pa11y的JSON配置文件中为Actions配置超时以覆盖默认30秒
问题:Pa11y Actions中"wait for url to be"操作超时问题解决
我们在Linux环境下通过Jenkins脚本化流水线,用Pa11y对需要认证的URL执行自动化无障碍测试,现有JSON配置文件如下:
{ "chromeLaunchConfig": { "args": [ "--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage" ] }, "actions":[ "set field #loginUserId to ***", "set field #loginPassword to ***", "click element #loginButton_0 to ***", "wait for url to be https://www.qa.***.com/account/dashboard" ] }
调用Pa11y的命令行脚本为:
pa11y --config config.json --threshold 500 -t 120000 --include-notices --include-warnings --reporter cli https://www.qa.***.com/login > results.json
多数情况下测试正常,但偶尔会触发超时错误:
Error: TimeoutError: waiting for function failed: timeout 30000ms exceeded
我们已经通过命令行参数-t 120000将全局超时设置为120秒,但Actions中的"wait for url to be"操作仍使用默认30秒超时,导致偶尔失败。需要在JSON配置文件中为Actions单独配置超时以避免该错误。
解决方案:为单个Action配置自定义超时
Pa11y支持将Action从字符串格式改为对象格式,以此为单个操作指定独立的超时时间,不受全局超时的限制。具体修改方式如下:
修改后的完整配置文件
{ "chromeLaunchConfig": { "args": [ "--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage" ] }, "actions": [ "set field #loginUserId to ***", "set field #loginPassword to ***", "click element #loginButton_0 to ***", { "action": "wait for url to be https://www.qa.***.com/account/dashboard", "timeout": 120000 } ] }
关键说明
- 把原来的字符串类型
wait for url to be操作,改成包含action和timeout属性的对象 timeout值以毫秒为单位,这里设置为120000(即120秒,和全局超时保持一致),你可以根据实际页面加载速度调整这个数值- 其他不需要单独设置超时的Action,依然可以保留字符串格式,不影响原有功能
修改完成后,这个等待URL跳转的操作就会使用你指定的超时时间,不会再触发默认30秒的超时错误。
内容的提问来源于stack exchange,提问作者Ashley
相关产品推荐
相关产品推荐

