驱动程序二进制文件
要基于WebDriver协议运行自动化测试,您需要设置浏览器驱动程序来转译自动化命令并能够在浏览器中执行它们。
自动化设置
使用WebdriverIO v8.14
及以上版本,您不再需要手动下载和设置任何浏览器驱动程序,因为这些都由WebdriverIO处理。您只需指定要测试的浏览器,WebdriverIO将完成其余工作。
自定义自动化级别
WebdriverIO有三个自动化级别:
1. 使用@puppeteer/browsers下载并安装浏览器。
如果您在capabilities配置中指定了browserName
/browserVersion
组合,WebdriverIO将下载并安装所请求的组合,无论机器上是否已存在安装。如果您省略browserVersion
,WebdriverIO将首先尝试使用locate-app定位并使用现有安装,否则它将下载并安装当前稳定的浏览器版本。有关browserVersion
的更多详情,请参见此处。
自动浏览器设置不支持Microsoft Edge。目前,仅支持Chrome、Chromium和Firefox。
如果您的浏览器安装在WebdriverIO无法自动检测的位置,您可以指定浏览器二进制文件,这将禁用自动下载和安装。
{
capabilities: [
{
browserName: 'chrome', // 或 'firefox' 或 'chromium'
'goog:chromeOptions': { // 或 'moz:firefoxOptions' 或 'wdio:chromedriverOptions'
binary: '/path/to/chrome'
},
}
]
}
2. 使用Chromedriver、Edgedriver或Geckodriver下载并安装驱动程序。
WebdriverIO将始终执行此操作,除非在配置中指定了驱动程序binary:
{
capabilities: [
{
browserName: 'chrome', // 或 'firefox', 'msedge', 'safari', 'chromium'
'wdio:chromedriverOptions': { // 或 'wdio:geckodriverOptions', 'wdio:edgedriverOptions'
binary: '/path/to/chromedriver' // 或 'geckodriver', 'msedgedriver'
}
}
]
}
WebdriverIO不会自动下载Safari驱动程序,因为它已预装在macOS上。
避免仅为浏览器指定binary
而省略相应的驱动程序binary
,反之亦然。如果仅指定其中一个binary
值,WebdriverIO将尝试使用或下载与之兼容的浏览器/驱动程序。然而,在某些情况下,这可能会导致不兼容的组合。因此,建议您始终同时指定两者,以避免版本不兼容引起的问题。
3. 启动/停止驱动程序。
默认情况下,WebdriverIO将使用任意未使用的端口自动启动和停止驱动程序。指定以下任何配置将禁用此功能,这意味着您需要手动启动和停止驱动程序:
手动设置
以下描述了如何单独设置每个驱动程序。您可以在awesome-selenium
README中找到所有驱动程序的列表。
如果您想设置移动和其他UI平台,请查看我们的Appium设置指南。
Chromedriver
要自动化Chrome,您可以直接在项目网站上下载Chromedriver,或通过NPM包:
- npm
- Yarn
- pnpm
npm install -g chromedriver
yarn global add chromedriver
pnpm add -g chromedriver
然后您可以通过以下方式启动它:
chromedriver --port=4444 --verbose
Geckodriver
要自动化Firefox,请为您的环境下载最新版本的geckodriver
并在项目目录中解压:
- NPM
- Curl
- Brew
- Windows (64 bit / Chocolatey)
- Windows (64 bit / Powershell) DevTools
- npm
- Yarn
- pnpm
npm install geckodriver
yarn add geckodriver
pnpm add geckodriver
Linux:
curl -L https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-linux64.tar.gz | tar xz
MacOS (64 bit):
curl -L https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-macos.tar.gz | tar xz
brew install geckodriver
choco install selenium-gecko-driver
# Run as privileged session. Right-click and set 'Run as Administrator'
# Use geckodriver-v0.24.0-win32.zip for 32 bit Windows
$url = "https://github.com/mozilla/geckodriver/releases/download/v0.24.0/geckodriver-v0.24.0-win64.zip"
$output = "geckodriver.zip" # will drop into current directory unless defined otherwise
$unzipped_file = "geckodriver" # will unzip to this folder name
# By default, Powershell uses TLS 1.0 the site security requires TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Downloads Geckodriver
Invoke-WebRequest -Uri $url -OutFile $output
# Unzip Geckodriver
Expand-Archive $output -DestinationPath $unzipped_file
cd $unzipped_file
# Globally Set Geckodriver to PATH
[System.Environment]::SetEnvironmentVariable("PATH", "$Env:Path;$pwd\geckodriver.exe", [System.EnvironmentVariableTarget]::Machine)
**注意:**其他geckodriver
版本可在此处获取。下载后,您可以通过以下方式启动驱动程序:
/path/to/binary/geckodriver --port 4444
Edgedriver
您可以在项目网站上下载Microsoft Edge的驱动程序,或通过NPM包:
npm install -g edgedriver
edgedriver --version # prints: Microsoft Edge WebDriver 115.0.1901.203 (a5a2b1779bcfe71f081bc9104cca968d420a89ac)
Safaridriver
Safaridriver预装在您的MacOS上,可以直接通过以下方式启动:
safaridriver -p 4444