cypress-time-marks
v1.6.0
Published
Custom Cypress commands to measure elapsed time
Downloads
2,913
Readme
cypress-time-marks
Custom Cypress commands to measure elapsed time
cy.timeMark('start').wait(1500).timeSince('start').wait(500).timeSince('start')
See spec.cy.js
- 📺 Watch the video Measure The Elapsed Time In Your Cypress Tests
- 🎓 Covered in my course Cypress Network Testing Exercises
- 🎓 Covered in my course Cypress Plugins
Install
Add this plugin as a dev dependency
# if using NPM
$ npm i -D cypress-time-marks
# if using Yarn
$ yarn add -D cypress-time-marks
Import this plugin from the spec file or from the support file
// cypress/e2e/spec.cy.js or cypress/support/e2e.js
import 'cypress-time-marks'
This should give you two new custom commands cy.timeMark(name)
and cy.timeSince(name)
. If you want TypeScript definitions, this module includes them:
// my spec JS file
/// <reference types="cypress-time-marks" />
Options
cy.timeSince(markName: string, label?: string, timeLimit?: number, throwError?: boolean)
labels
You can log a label when using cy.timeSince(name, label)
cy.timeMark('start')
.wait(1500)
.timeSince('start', 'initial wait')
.wait(500)
.timeSince('start', 'final load')
time limit warning
You can pass a time limit after the mark name to warn if the elapsed time is longer than the limit. It won't fail the test, but it will change the icon to warn you.
cy.timeMark('start')
.wait(100)
.timeSince('start', 'under time limit', 200)
.wait(500)
.timeSince('start', 'over time limit', 200)
.timeSince('start', 200)
fail the test
You can fail the test if the elapsed time is above the given limit.
cy.timeMark('start').wait(100).timeSince(
'start', // mark name
'waiting', // message
50, // time limit (ms)
true, // throw an error if above the time limit
)
timeBetween
Sometimes you want to measure time, but assert the durations after the fact.
// run commands and capture the marks
cy.timeMark('start').wait(400).timeMark('finish')
// confirm the durations between two marks
cy.timeBetween('start', 'finish')
You can add a label, set maximum duration, and even throw an error if the duration is above the given limit.
// label this duration "loading time" in the command log
// and if it exceeds 1 second, throw an error failing the test
cy.timeBetween('start', 'finish', 'loading time', 1000, true)
See also
- cypress-timestamps plugin
Small print
Author: Gleb Bahmutov <[email protected]> © 2022
- @bahmutov
- glebbahmutov.com
- blog
- videos
- presentations
- cypress.tips
- Cypress Tips & Tricks Newsletter
- my Cypress courses
License: MIT - do anything with the code, but don't blame me if it does not work.
Support: if you find any problems with this module, email / tweet / open issue on Github