【问题标题】:How to find the calling test in cypress custom command如何在 cypress 自定义命令中找到调用测试
【发布时间】:2021-02-04 20:49:53
【问题描述】:

我有一个命令可以覆盖暂停以将对话框中的输入添加到报告中。我想知道是否有办法知道什么测试正在调用该函数,以便我可以自定义输入上的消息。

Cypress.Commands.overwrite('pause', (originalFn, element, options) => {
    var tryThis = '';
    if (//place calling the function == file1) {
         tryThis = 'message1';
    } else if (//place calling the function == file2) {
         ...
    } else if (//place calling the function == file3) {
         ...
    }
    var datalog = window.prompt(tryThis, "Log your results");
    cy.addContext("DATALOG:" + datalog);
    return originalFn(element, options)
  })

【问题讨论】:

    标签: javascript node.js cypress


    【解决方案1】:

    除了通过 Mocha 属性访问之外,还有

    对于规范文件 Cypress.spec

    my.spec.js 的属性

    Cypress.spec.absolute: "C:/.../my.spec.js"  
    
    Cypress.spec.name: "my.spec.js"
    
    Cypress.spec.relative: "cypress\integration\my.spec.js"
    
    Cypress.spec.specFilter: "my"
    
    Cypress.spec.specType: "integration"
    

    为了测试 cy.state('runnable')

    对于

    describe('my-context', () => {
      it('my-test', () => {
    

    属性和方法,

    const title = cy.state('runnable').title;           // "my-test"
    
    const fullTitle = cy.state('runnable').fullTitle(); // "my-context my-test"
    
    const titlePath = cy.state('runnable').titlePath(); // ["my-context", "my-test"]
    

    您还可以将元数据添加到测试中

    describe('my-context', () => {
      it('my-test', { message: "my-message" },  () => {
    

    并在命令覆盖中抓取它

    const message = cy.state('runnable').cfg.message; // "my-message"
    

    【讨论】:

      【解决方案2】:

      我试过这个,它对我有用(我的 cypress 版本是 6.1.0):

      cy.log(Cypress.mocha.getRunner().suite.ctx.test.title);
      

      更多信息:https://github.com/cypress-io/cypress/issues/2972

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-01-29
        • 2019-08-12
        • 2010-11-20
        • 2022-07-27
        • 2020-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多