框架
WebdriverIO Runner 内置支持 Mocha、Jasmine 和 Cucumber.js。您还可以将其与第三方开源框架集成,如 Serenity/JS。
要将 WebdriverIO 与测试 框架集成,您需要在 NPM 上有一个适配器包。 请注意,适配器包必须安装在与 WebdriverIO 相同的位置。 因此,如果您全局安装了 WebdriverIO,请确保也全局安装适配器包。
将 WebdriverIO 与测试框架集成可让您在规范文件或步骤定义中使用全局 browser
变量访问 WebDriver 实例。
请注意,WebdriverIO 还将负责实例化和结束 Selenium 会话,因此您不必自己做这些。
使用 Mocha
首先,从 NPM 安装适配器包:
- npm
- Yarn
- pnpm
- Bun
npm install @wdio/mocha-framework --save-dev
yarn add @wdio/mocha-framework --dev
pnpm add @wdio/mocha-framework --save-dev
bun add @wdio/mocha-framework --dev
默认情况下,WebdriverIO 提供了一个内置的断言库,您可以立即开始使用:
describe('my awesome website', () => {
it('should do some assertions', async () => {
await browser.url('https://webdriver.io')
await expect(browser).toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO')
})
})
WebdriverIO 支持 Mocha 的 BDD
(默认)、TDD
和 QUnit
接口。
如果您喜欢以 TDD 风格编写规范,请在配置中的 mochaOpts
属性中将 ui
设置为 tdd
。现在您的测试文件应该这样编写:
suite('my awesome website', () => {
test('should do some assertions', async () => {
await browser.url('https://webdriver.io')
await expect(browser).toHaveTitle('WebdriverIO · Next-gen browser and mobile automation test framework for Node.js | WebdriverIO')
})
})
如果您想定义其他 Mocha 特定设置,可以在配置文件中使用 mochaOpts
键。所有选项的列表可以在 Mocha 项目网站上找到。
注意: WebdriverIO 不支持在 Mocha 中使用已弃用的 done
回调:
it('should test something', (done) => {
done() // 抛出 "done is not a function" 错误
})
Mocha 选项
以下选项可以在您的 wdio.conf.js
中应用,以配置您的 Mocha 环境。注意: 并非所有选项都受支持,例如应用 parallel
选项将导致错误,因为 WDIO 测试运行器有自己的方式来并行运行测试。您可以将这些框架选项作为参数传递,例如:
wdio run wdio.conf.ts --mochaOpts.grep "my test" --mochaOpts.bail --no-mochaOpts.checkLeaks
这将传递以下 Mocha 选项:
{
grep: ['my-test'],
bail: true
checkLeacks: false
}
支持以下 Mocha 选项:
require
当您想添加或扩展一些基本功能时,require
选项很有用(WebdriverIO 框架选项)。
类型:string|string[]
默认值:[]
compilers
使用给定的模块来编译文件。编译器将在 requires 之前包含(WebdriverIO 框架选项)。
类型:string[]
默认值:[]
allowUncaught
传播未捕获的错误。
类型:boolean
默认值:false
bail
首次测试失败后中止。
类型:boolean
默认值:false
checkLeaks
检查全局变量泄漏。
类型:boolean
默认值:false
delay
延迟根套件执行。
类型:boolean
默认值:false
fgrep
给定字符串的测试过滤器。
类型:string
默认值:null
forbidOnly
标记为 only
的测试会导致套件失败。
类型:boolean
默认值:false
forbidPending
待处理的测试会导致套件失败。
类型:boolean
默认值:false
fullTrace
失败时的完整堆栈跟踪。
类型:boolean
默认值:false
global
全局范围内预期的变量。
类型:string[]
默认值:[]
grep
给定正则表达式的测试过滤器。
类型:RegExp|string
默认值:null
invert
反转测试过滤器匹配。
类型:boolean
默认值:false
retries
失败测试的重试次数。
类型:number
默认值:0
timeout
超时阈值(毫秒)。
类型:number
默认值:30000
使用 Jasmine
首先,从 NPM 安装适配器包:
- npm
- Yarn
- pnpm
- Bun
npm install @wdio/jasmine-framework --save-dev
yarn add @wdio/jasmine-framework --dev
pnpm add @wdio/jasmine-framework --save-dev
bun add @wdio/jasmine-framework --dev
然后 ,您可以通过在配置中设置 jasmineOpts
属性来配置 Jasmine 环境。所有选项的列表可以在 Jasmine 项目网站上找到。
Jasmine 选项
以下选项可以在您的 wdio.conf.js
中应用,使用 jasmineOpts
属性配置您的 Jasmine 环境。有关这些配置选项的更多信息,请查看 Jasmine 文档。您可以将这些框架选项作为参数传递,例如:
wdio run wdio.conf.ts --jasmineOpts.grep "my test" --jasmineOpts.failSpecWithNoExpectations --no-jasmineOpts.random
这将传递以下 Mocha 选项:
{
grep: ['my-test'],
bail: true
checkLeacks: false
}
支持以下 Jasmine 选项:
defaultTimeoutInterval
Jasmine 操作的默认超时间隔。
类型:number
默认值:60000
helpers
相对于 spec_dir 的文件路径(和通配符)数组,在 jasmine 规范之前包含。
类型:string[]
默认值:[]
requires
当您想添加或扩展一些基本功能时,requires
选项很有用。
类型:string[]
默认值:[]
random
是否随机化规范执行顺序。
类型:boolean
默认值:true
seed
用作随机化基础的种子。Null 会导致种子在执行开始时随机确定。
类型:Function
默认值:null
failSpecWithNoExpectations
没有期望的规范是否失败。默认情况下,没有运行期望的规范被报告为通过。将此设置为 true 将报告此类规范为失败。
类型:boolean
默认值:false
oneFailurePerSpec
规范是否只有一个期望失败。
类型:boolean
默认值:false
specFilter
用于过滤规范的函数。
类型:Function
默认值:(spec) => true
grep
仅运行与此字符串或正则表达式匹配的测试。(仅在未设置自定义 specFilter
函数时适用)
类型:string|Regexp
默认值:null
invertGrep
如果为 true,则反转匹配的测试,只运行与 grep
中使用的表达式不匹配的测试。(仅在未设置自定义 specFilter
函数时适用)
类型:boolean
默认值:false
使用 Cucumber
首先,从 NPM 安装适配器包:
- npm
- Yarn
- pnpm
- Bun
npm install @wdio/cucumber-framework --save-dev
yarn add @wdio/cucumber-framework --dev
pnpm add @wdio/cucumber-framework --save-dev
bun add @wdio/cucumber-framework --dev
如果您想使用 Cucumber,通过在配置文件中添加 framework: 'cucumber'
将 framework
属性设置为 cucumber
。
Cucumber 的选项可以在配 置文件中使用 cucumberOpts
给出。在这里查看选项的完整列表。
要快速上手 Cucumber,请查看我们的 cucumber-boilerplate
项目,它带有您开始所需的所有步骤定义,您可以立即开始编写特性文件。
Cucumber 选项
以下选项可以在您的 wdio.conf.js
中应用,使用 cucumberOpts
属性配置您的 Cucumber 环境:
cucumberOpts
,例如用于过滤测试的自定义 tags
,可以通过命令行指定。这是通过使用 cucumberOpts.{optionName}="value"
格式实现的。
例如,如果您只想运行带有 @smoke
标签的测试,可以使用以下命令:
# 当您只想运行带有 "@smoke" 标签的测试时
npx wdio run ./wdio.conf.js --cucumberOpts.tags="@smoke"
npx wdio run ./wdio.conf.js --cucumberOpts.name="some scenario name" --cucumberOpts.failFast
此命令将 cucumberOpts
中的 tags
选项设置为 @smoke
,确保只执行带有此标签的测试。
backtrace
显示错误的完整回溯。
类型:Boolean
默认值:true
requireModule
在要求任何支持文件之前要求模块。
类型:string[]
默认值:[]
示例:
cucumberOpts: {
requireModule: ['@babel/register']
// 或
requireModule: [
[
'@babel/register',
{
rootMode: 'upward',
ignore: ['node_modules']
}
]
]
}
failFast
首次失败后中止运行。
类型:boolean
默认值:false
name
仅执行名称与表达式匹配的场景(可重复)。
类型:RegExp[]
默认值:[]
require
在执行特性之前,要求包含您的步骤定义的文件。您还可以指定步骤定义的 glob。
类型:string[]
默认值:[]
示例:
cucumberOpts: {
require: [path.join(__dirname, 'step-definitions', 'my-steps.js')]
}
import
您的支持代码所在的路径,用于 ESM。
类型:String[]
默认值:[]
示例:
cucumberOpts: {
import: [path.join(__dirname, 'step-definitions', 'my-steps.js')]
}
strict
如果有任何未定义或待处理的步骤,则失败。
类型:boolean
默认值:false
tags
仅执行标签与表达式匹配的特性或场景。 请参阅 Cucumber 文档 了解更多详情。
类型:String
默认值:``
timeout
步骤定义的超时时间(毫秒)。
类型:Number
默认值:30000
retry
指定重试失败测试用例的次数。
类型:Number
默认值:0
retryTagFilter
仅重试标签与表达式匹配的特性或场景(可重复)。此选项需要指定 '--retry'。
类型:RegExp
language
特性文件的默认语言
类型:String
默认值:en
order
按定义/随机顺序运行测试
类型:String
默认值:defined
format
要使用的格式器的名称和输出文件路径。 WebdriverIO 主要只支持将输出写入文件的格式器。
类型:string[]
formatOptions
提供给格式器的选项
类型:object
tagsInTitle
将 cucumber 标签添加到特性或场景名称
类型:Boolean
默认值:false
请注意,这是 @wdio/cucumber-framework 特有的选项,cucumber-js 本身不识别
ignoreUndefinedDefinitions
将未定义的定义视为警告。
类型:Boolean
默认值:false
请注意,这是 @wdio/cucumber-framework 特有的选项,cucumber-js 本身不识别
failAmbiguousDefinitions
将模糊定义视为错误。
类型:Boolean
默认值:false
请注意,这是 @wdio/cucumber-framework 特有的选项,cucumber-js 本身不识别
tagExpression
仅执行标签与表达式匹配的特性或场景。 请参阅 Cucumber 文档 了解更多详情。
类型:String
默认值:``
请注意,此选项将在未来被弃用。请改用 tags
配置属性