Cypress: plugin: use command:start command:end

Created on 19 Jul 2018  路  3Comments  路  Source: cypress-io/cypress

Hey

I'm trying to create my own plugin for reporting purposes,
so I want to monitor each command execution time+arguments, but I couldn't find a way to do it.
is it possible to use a plugin like:

module.exports = (on, config) => {
    on('command:start', (command, arguments) => {
        console.log(command+":started at "+new Date().getTime())
    });
    on('command:end', (command, result) => {
                console.log(command+":ended at "+new Date().getTime())
    });
}

so basically I'm trying to create something like the command log for my personal reporting requirements.
image

question

All 3 comments

Plugin events are limited to what's listed here. The command:start and command:end events can only be listened to on the Cypress object in the browser, while the plugin events occur in the background Node process.

The 'task' event in conjunction with cy.task() is perfect for your use-case, bridging the gap between the browser and the background process:

// in your test file or support file
Cypress.on('command:start', (command) => {
  // very important! otherwise we end up in an infinite loop
  if (command.get('name') === 'task') return

  cy.task('command:start')
})

Cypress.on('command:end', (command) => {
  // very important! otherwise we end up in an infinite loop
  if (command.get('name') === 'task') return

  cy.task('command:end', command.get('name'))
})


// in your plugins file
module.exports = (on, config) => {
  on('task', {
    'command:start' (command) {
      console.log(command + ":started at "+new Date().getTime())

      return null
    },
    'command:end' (command) {
      console.log(command + ":end at "+new Date().getTime())

      return null
    },
  })
}

We could also add additional plugins events that match up to the browser events. You won't be able to interface or do anything to control Cypress that's running in the browser, but you could log / list them out if need be.

The problem with trying to run tasks from within this is that they will only be enqueued in the command pipeline, and will not be run until other commands finish.

There is an undocumented way to kick off commands immediately without them being enqueued where they act like a regular promise - which is cy.now(commandName, args...)

cy.now('task', arg1, arg2) but you'll need to add your own .catch and .then handlers so nothing can leak out or explode.

Ideally we just automatically synchronize and add all events in both the background node and browser processes so you don't have to do wire these up manually.

@chrisbreiding @brian-mann coool, thanks a lot 馃槃 that's what I was looking for.

Was this page helpful?
0 / 5 - 0 ratings