Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

plugin: use command:start command:end #2177

Closed
tahayk opened this issue Jul 19, 2018 · 3 comments
Closed

plugin: use command:start command:end #2177

tahayk opened this issue Jul 19, 2018 · 3 comments

Comments

@tahayk
Copy link

tahayk commented Jul 19, 2018

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

@chrisbreiding
Copy link
Contributor

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
    },
  })
}

@brian-mann
Copy link
Member

brian-mann commented Jul 20, 2018

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.

@tahayk
Copy link
Author

tahayk commented Jul 23, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants