Before Run 事件
before:run
事件在运行开始前触发。当通过 cypress open
运行 Cypress 时,该事件会在打开项目时触发。
每次执行 cypress run
时都会触发该事件。因此,如果在并行模式下运行测试,该事件会在运行测试的每台机器上各触发一次。
语法
caution
⚠️ 这段代码属于
setupNodeEvents 函数的一部分,
因此会在 Node 环境中执行。你不能在此函数中调用 Cypress
或 cy
命令,
但可以直接访问文件系统和操作系统的其他部分。
caution
⚠️ 当通过 cypress open
运行时,before:run
事件仅在启用 experimentalInteractiveRunEvents 标志时触发。
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
/* ... */
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
/* ... */
})
},
},
})
details (Object)
运行的详细信息,包括项目配置、系统信息和 Cypress 版本。通过 cypress run
运行时包含更多细节。
用法
你可以从 before:run
事件处理函数中返回一个 Promise,Cypress 会等待该 Promise 完成后再继续运行测试。
记录浏览器信息和将要运行的测试数量
- cypress.config.js 文件
- cypress.config.ts 文件
const { defineConfig } = require('cypress')
module.exports = defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
// 通过 `cypress run` 运行时,details 对象类似这样:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...更多属性...
// },
// browser: {
// name: 'electron',
// version: '59.0.3071.115',
// // ...更多属性...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '6.1.0',
// specs: [
// {
// name: 'login_cy.js',
// relative: 'cypress/e2e/login_cy.js',
// absolute: '/Users/janelane/app/cypress/e2e/login_cy.js',
// },
// // ...更多测试文件
// ],
// specPattern: [
// '**/*.cy.{js,jsx,ts,tsx}'
// ],
// parallel: false,
// group: 'group-1',
// tag: 'tag-1'
// }
// 通过 `cypress open` 运行时,details 对象类似这样:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...更多属性...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '7.0.0'
// }
if (details.specs && details.browser) {
// 在交互模式下,details.specs 和 details.browser 会是 undefined
console.log(
'在',
details.browser.name,
'中运行',
details.specs.length,
'个测试'
)
}
})
},
},
})
import { defineConfig } from 'cypress'
export default defineConfig({
// setupNodeEvents can be defined in either
// the e2e or component configuration
e2e: {
setupNodeEvents(on, config) {
on('before:run', (details) => {
// 通过 `cypress run` 运行时,details 对象类似这样:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...更多属性...
// },
// browser: {
// name: 'electron',
// version: '59.0.3071.115',
// // ...更多属性...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '6.1.0',
// specs: [
// {
// name: 'login_cy.js',
// relative: 'cypress/e2e/login_cy.js',
// absolute: '/Users/janelane/app/cypress/e2e/login_cy.js',
// },
// // ...更多测试文件
// ],
// specPattern: [
// '**/*.cy.{js,jsx,ts,tsx}'
// ],
// parallel: false,
// group: 'group-1',
// tag: 'tag-1'
// }
// 通过 `cypress open` 运行时,details 对象类似这样:
// {
// config: {
// projectId: '12345',
// baseUrl: 'http://example.com/',
// viewportWidth: 1000,
// viewportHeight: 660,
// // ...更多属性...
// },
// system: {
// osName: 'darwin',
// osVersion: '16.7.0',
// }
// cypressVersion: '7.0.0'
// }
if (details.specs && details.browser) {
// 在交互模式下,details.specs 和 details.browser 会是 undefined
console.log(
'在',
details.browser.name,
'中运行',
details.specs.length,
'个测试'
)
}
})
},
},
})