5 javascript ui-automation typescript cypress
我在当前项目中使用 cypress 和 typescript,我需要获取保存的屏幕截图的名称。屏幕截图名称由 cypress 自动保存,该名称以描述块中给出的描述开头。
例如
describe('Admin Portal', () => {
it('Login Test', () => {
});
});
Run Code Online (Sandbox Code Playgroud)
屏幕截图将另存为“管理员门户登录--登录测试(失败).png ”
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
const screenshotFileName = `${test.title} (failed).png`
addContext({ test }, `assets/${Cypress.spec.name}/${screenshotFileName}`)
}
Run Code Online (Sandbox Code Playgroud)
上面这行代码实际上是检索It块中定义的名称,如何获取describe块中定义的名称?
soc*_*way -1
console.log("=======>>>", Cypress.mocha.getRunner().suite.title)我在本节中尝试过beforeEach(),它在 console.log() 中向我显示套件标题名称。那么您可以添加Cypress.mocha.getRunner().suite.title并尝试以下代码并让我知道它是否有效:
Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
const screenshotFileName = `Cypress.mocha.getRunner().suite.title (failed).png`
addContext({ test }, `assets/${Cypress.spec.name}/${screenshotFileName}`)
}
})
Run Code Online (Sandbox Code Playgroud)