简体   繁体   English

如何获取在描述块中输入的描述? (打字稿)

[英]How to get the description entered in describe block? (typescript)

I am using cypress with typescript for my current project, I am in a situation to get the name of a saved screenshot.我正在为我当前的项目使用带有 typescript 的 cypress,我正在获取已保存屏幕截图的名称。 the screenshot name is automatically saved by cypress which starts with the description given in the describe block.屏幕截图名称由 cypress 自动保存,该名称以 describe 块中给出的描述开头。

for example例如

describe('Admin Portal', () => {
  it('Login Test', () => {

  });
});

the screenshot will be saved as ' Admin Portal login -- Login Test (failed).png '屏幕截图将保存为 ' Admin Portal login -- Login Test (failed).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}`)
}

the above line of code is actually retrieving the name defined in the It block, how to get the name defined in the describe block??上面这行代码实际上是检索It块中定义的名称,如何获取describe块中定义的名称??

Try something like this:尝试这样的事情:

Cypress.on('test:after:run', (test, runnable) => {
if (test.state === 'failed') {
    const screenshotFileName = `${Cypress.config('screenshotsFolder')}/${Cypress.spec.name}/${runnable.parent.title} -- ${test.title} (failed).png`
    addContext({ test }, screenshotFileName)
}

}) })

I have tried console.log("=======>>>", Cypress.mocha.getRunner().suite.title) in the beforeEach() section and it is displaying me the suite title name in the console.log().我在beforeEach()部分尝试了console.log("=======>>>", Cypress.mocha.getRunner().suite.title) ,它在控制台中向我显示了套件标题名称。日志()。 So can you add Cypress.mocha.getRunner().suite.title and try the below code and let me know if it is working:那么您能否添加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}`)
}
})

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM