Search code examples
cucumbercypressmocha.jscypress-cucumber-preprocessor

How to get the current step definition name in Cypress-Cucumber? NOT the scenario name


I'm doing some custom reporting to an API and I'm retrieving bits and pieces of information to put into the payload.

One piece of information I'd like to get is the step definition name ( not the scenario name ) at invocation.

I'm sending this information over whenever there is a failure. See the snippet below. Any ideas?


  Cypress.on('fail', (error, runnable)=>{
    const errorName = Cypress._.toLower(error.name)
    const message = Cypress._.toLower(error.message)
    const isSkipped = message.includes('skip')
    const stepName = ""
    const wholeMessage = `${stepName} / ${message}`
    if(errorName.includes('error') && isSkipped!=true){
      cy.now('task', 'getValue', 'currentScenario', {log:false}).then(name=>{
        cy.now('postTestResult', "Complete", "Failed", name, wholeMessage)
      })
    }

    if(errorName.includes('error') && isSkipped==true){
      cy.now('task', 'getValue', 'currentScenario', {log:false}).then(name=>{
        cy.now('postTestResult', "Complete", "Skipped", name, wholeMessage)
      })
    }
    throw error
  })

Explored the Mocha runnable object & associated methods, including those references from Cypress. Explored online and could not find a reference for retrieving the step definition name from a Cypress Cucumber step


Solution

  • You can use window.testState.pickleStep.text to get the title text for the current running step. The window.testState object contains the data for the entire test run. window.testState.pickles contains info about all the feature files, window.testState.pickle contains the info for the current feature, and window.testState.pickleStep contains info on the current step within that feature.