组织测试套件
随着项目的增长,不可避免地会添加越来越多的集成测试。这会增加构建时间并降低生产力。
为了避免这种情况,你应该并行运行测试。WebdriverIO已经在单个会话中并行测试每个规格(或Cucumber中的_feature file_)。一般来说,尽量在每个规格文件中只测试一个功能 。尝试不要在一个文件中包含太多或太少的测试。(然而,这里没有黄金法则。)
一旦你的测试有了几个规格文件,你应该开始并发运行测试。为此,在你的配置文件中调整maxInstances属性。WebdriverIO允许你以最大并发度运行测试——这意味着无论你有多少文件和测试,它们都可以并行运行。(这仍然受到某些限制,如计算机的CPU、并发限制等。)
假设你有3种不同的能力(Chrome、Firefox和Safari),并且你将
maxInstances设置为1。WDIO测试运行器将产生3个进程。因此,如果你有10个规格文件并将maxInstances设置为10,_所有_规格文件将同时测试,并产生30个进程。
你可以全局定义maxInstances属性来为所有浏览器设置属性。
如果你运行自己的WebDriver网格,你可能(例如)对一个浏览器有比另一个更多的容量。在这种情况下,你可以在能力对象中_限制_maxInstances:
// wdio.conf.js
export const config = {
// ...
// set maxInstance for all browser
maxInstances: 10,
// ...
capabilities: [{
browserName: 'firefox'
}, {
// maxInstances can get overwritten per capability. So if you have an in-house WebDriver
// grid with only 5 firefox instance available you can make sure that not more than
// 5 instance gets started at a time.
browserName: 'chrome'
}],
// ...
}
继承主配置文件
如果你在多个环境中运行测试套件(例如,开发和集成环境),使用多个配置文件可能有助于保持可管理性。
类似于页面对象概念,你首先需要一个主配置文件。它包含所有在环境间共享的配置。
然后为每个环境创建另一个配置文件,并用环境特定的配置补充主配置:
// wdio.dev.config.js
import { deepmerge } from 'deepmerge-ts'
import wdioConf from './wdio.conf.js'
// have main config file as default but overwrite environment specific information
export const config = deepmerge(wdioConf.config, {
capabilities: [
// more caps defined here
// ...
],
// run tests on sauce instead locally
user: process.env.SAUCE_USERNAME,
key: process.env.SAUCE_ACCESS_KEY,
services: ['sauce']
}, { clone: false })
// add an additional reporter
config.reporters.push('allure')
将测试规格分组到套件中
你可以将测试规格分组到套件中,并运行特定的单个套件,而不是运行所有套件。
首先,在你的WDIO配置中定义套件:
// wdio.conf.js
export const config = {
// define all tests
specs: ['./test/specs/**/*.spec.js'],
// ...
// define specific suites
suites: {
login: [
'./test/specs/login.success.spec.js',
'./test/specs/login.failure.spec.js'
],
otherFeature: [
// ...
]
},
// ...
}