
WebdriverIO 连接 Selenium Grid 完整指南配置、鉴权、超时与文件传输实战【免费下载链接】webdriverioNext-gen browser and mobile automation test framework for Node.js项目地址: https://gitcode.com/GitHub_Trending/we/webdriverio通过现有 Selenium Grid 运行 WebdriverIO 测试只需修改测试运行器配置中的连接参数无需引入任何新依赖。本文以 website/docs/SeleniumGrid.md 为骨架系统讲解如何将测试指向远程 Grid、通过headers完成 Basic 鉴权、针对动态 Grid 冷启动调整会话超时以及远程浏览器环境下文件上传/下载的完整方案并结合packages/webdriver/src/constants.ts、packages/webdriverio/src/node/等源码说明底层实现细节。连接本地或远程 Selenium Grid 的基础配置WebdriverIO 使用 WebDriver 协议与 Selenium Grid 通信你只需在wdio.conf.ts或wdio.conf.js中更新四个连接参数即可将测试从本地驱动指向任意 Selenium Grid配置项作用类型默认值protocol连接协议http或httpsstringhttphostnameSelenium Grid 所在主机名或 IPstringlocalhostportGrid 暴露的端口number无需显式指定pathWebDriver 端点路径string/必须/开头以上默认值与校验规则可在 packages/webdriver/src/constants.ts 中直接看到例如path选项通过validate强制要求以/开头否则抛出TypeErrorprotocol通过正则/(http|https)/限定取值。连接远程 Selenium Grid如果你的 Grid 部署在独立域名上如公司内网或云环境按如下方式配置export const config: WebdriverIO.Config { // ... protocol: https, hostname: yourseleniumgridhost.yourdomain.com, port: 443, path: /wd/hub, // ... }protocol、hostname、port、path四个值必须与你实际的 Selenium Grid 部署完全一致例如 Grid 是否走 HTTPS、是否挂了反向代理、端点是否挂载在/wd/hub下请根据你的 Grid 设置逐一核对。连接本机 Selenium Grid如果 Selenium Grid 与测试脚本运行在同一台机器上例如本地调试使用以下典型配置export const config: WebdriverIO.Config { // ... protocol: http, hostname: localhost, port: 4444, path: /wd/hub, // ... }本地默认端口4444是 Selenium Grid 的标准端口/wd/hub是 Selenium 3 与 Grid 4 兼容的端点前缀。需要说明的是WebdriverIO 对hostname的默认值是localhost即不配置 Grid 时直接在本机寻找驱动端点。使用 headers 对接受保护的 Selenium GridSelenium Grid 暴露公网或内网端口时强烈建议加认证保护。若 Grid 开启了 Basic Authentication你可以通过配置中的headers选项向每次 WebDriver 请求注入认证头。WebdriverIO 官方推荐用Authorization头完成 Basic 认证示例配置见 website/docs/Configuration.mdimport { Buffer } from buffer; // 从环境变量读取用户名与密码避免明文硬编码 const username process.env.SELENIUM_GRID_USERNAME; const password process.env.SELENIUM_GRID_PASSWORD; // 使用冒号拼接凭证并进行 Base64 编码 const credentials ${username}:${password}; const encodedCredentials Buffer.from(credentials).toString(base64); export const config: WebdriverIO.Config { // ... headers: { Authorization: Basic ${encodedCredentials} } // ... }headers选项的默认值为{}其类型在 packages/webdriver/src/constants.ts 中声明为object会附加到每一个发往 Grid 的 WebDriver 请求中因此认证对会话创建、命令执行等所有请求都生效。将凭证放入环境变量而非配置文件可避免密钥随代码入库泄露。为动态 Selenium Grid 调大会话创建超时在使用按需拉起浏览器 Pod 的动态 Selenium Grid如 Kubernetes 集群中的 Selenium Operator、基于容器的 Grid时新会话创建可能经历「冷启动」——浏览器镜像拉取、Pod 调度、Node 注册等都需要时间首次创建会话可能远超预期。此时建议提高会话创建超时。WebdriverIO 相关默认值在 packages/wdio-config/src/constants.ts 与 packages/webdriver/src/constants.ts 中均有定义connectionRetryTimeout默认120000ms120 秒connectionRetryCount默认3。若你的 Grid 创建新会话耗时更长可调大超时export const config: WebdriverIO.Config { // ... connectionRetryTimeout: 180000, // ... }在底层实现上connectionRetryTimeout不仅用于重试等待也直接映射为 HTTP 请求的connectTimeout、headersTimeout与bodyTimeout见 packages/webdriver/src/request/node.ts因此它同时决定了「等待连接建立」和「等待请求体返回」的时间上限是动态 Grid 场景下防止会话创建期间超时被误杀的关键参数。远程 Grid 下的文件传输当测试运行在远程 Selenium Grid 上时浏览器运行在远端 Node 机器因此文件上传、下载与本地驱动模式有本质差异需要特殊处理。Selenium Standalone 的可用文件操作在远程 Grid 上同样适用可参考 Selenium Grid 文件操作说明 与下方各节。远程下载文件到测试机针对 Chromium 系浏览器先在capabilities中开启 Selenium Grid 的下载能力标志se:downloadsEnabledexport const config: WebdriverIO.Config { // ... protocol: https, hostname: yourseleniumgridhost.yourdomain.com, port: 443, path: /wd/hub, // ... capabilities: [{ browserName: chrome, se:downloadsEnabled: true }], //... }触发浏览器下载后文件默认保存在远端 Selenium Node 上。若测试脚本需要读取下载文件的内容必须通过browser.downloadFile(fileName, targetDirectory)命令把文件从远端节点拉回测试运行机。该命令的官方文档位于 website/docs/api/Browser.md其命令定义在 packages/webdriverio/src/commands/browser/downloadFile.ts核心实现见 packages/webdriverio/src/node/downloadFile.tsit(should download a file, async () { await browser.url(https://www.selenium.dev/selenium/web/downloads/download.html) await $(#file-1).click() await browser.waitUntil(async function () { return (await browser.getDownloadableFiles()).names.includes(file_1.txt) }, {timeout: 5000}) const files await browser.getDownloadableFiles() const downloaded await browser.downloadFile(files.names[0], process.cwd()) await browser.deleteDownloadableFiles() })从实现看downloadFile先调用底层this.download(fileName)拿到 Base64 编码的压缩内容随后自动在目标目录解压并写出真实文件最终返回解压出的文件路径列表。它要求远端支持 Selenium Grid 下载协议且必须同时开启se:downloadsEnabled实现源码会在不具备该能力时抛出The downloadFile command is not available ... only available when using Selenium Grid错误对应单元测试见 packages/webdriverio/tests/commands/browser/downloadFile.test.ts。配套可用的命令还包括getDownloadableFiles()与deleteDownloadableFiles()用于列出远端已下载文件与清理。上传本地文件到远端浏览器要向远端浏览器中的 Web 应用上传文件第一步是把本地文件上传到远程 Grid。使用browser.uploadFile(localPath)该命令会把本地文件以 ZIP 压缩、Base64 编码后通过底层file协议命令传给远端并返回远端文件路径随后即可把这个路径setValue到文件输入框import path from node:path it(should upload a file, async () { await browser.url(https://the-internet.herokuapp.com/upload) const filePath /path/to/some/file.png const remoteFilePath await browser.uploadFile(filePath) await $(#file-upload).setValue(remoteFilePath) await $(#file-submit).click() });命令定义在 packages/webdriverio/src/commands/browser/uploadFile.ts底层实现 packages/webdriverio/src/node/uploadFile.ts 使用archiver将文件压缩为 ZIP再将二进制内容转成 Base64 调用this.file()完成远端上传。需要留意的是官方文档注释特别说明该命令基于一个非官方协议特性目前仅在 Chrome 上、且运行 Selenium Grid 时受支持在不受支持的浏览器上this.file不存在时会直接抛出错误对应测试见 packages/webdriverio/tests/commands/browser/uploadFile.test.ts。其他 Grid 文件操作Selenium Grid 还支持若干扩展操作Selenium Standalone 的指令在 Grid 场景下通常同样可用。可用的远端文件操作如file、download、uploadFile、getDownloadableFiles、deleteDownloadableFiles等协议命令属于 WebdriverIO 的 Selenium 扩展协议能力具体命令清单与参数说明可参阅 website/docs/api/Protocols.md 中的 Selenium 扩展一节。进阶配置与官方资料高级配置连接之外的更多运行器选项connectionRetryCount、waitforTimeout、services、reporters、各类 Hooks 等完整说明见 website/docs/ConfigurationFile.md 与 website/docs/Configuration.md其中connectionRetryCount向 Selenium 服务器请求的重试次数默认 3与connectionRetryTimeout通常配合调整。Selenium Grid 官方文档Grid 的架构、Hub/Node 部署、Docker Compose 与 Kubernetes 编排细节请参阅 Selenium 官方 Grid 文档如需在 Docker、Docker Compose 或 Kubernetes 中运行 Grid可参考 SeleniumHQ 维护的 docker-selenium 仓库仓库 e2e 目录 也提供了 WebdriverIO 相关的容器编排示例可对照学习。小结把 WebdriverIO 接入既有 Selenium Grid 的核心是四件事按 Grid 部署情况配置protocol/hostname/port/path通过headers注入 Basic 认证应对受保护 Grid针对动态 Grid 的冷启动把connectionRetryTimeout从默认 120 秒调大在远程浏览器场景下用uploadFile/downloadFile与se:downloadsEnabled能力完成文件在测试机与远端节点之间的传输。理解这些配置对应的源码默认值与请求层实现能帮助你在分布式、容器化 Grid 环境中更快定位连接与超时类问题。【免费下载链接】webdriverioNext-gen browser and mobile automation test framework for Node.js项目地址: https://gitcode.com/GitHub_Trending/we/webdriverio创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考