Skip to content

Instantly share code, notes, and snippets.

@camilokawerin
Last active May 6, 2023 21:49
Show Gist options
  • Save camilokawerin/38f48121a7a39702759f763325ec0979 to your computer and use it in GitHub Desktop.
Save camilokawerin/38f48121a7a39702759f763325ec0979 to your computer and use it in GitHub Desktop.
Log object properties in Cypress.
/*
* Copy this file to your /support directory and edit index.js to add:
*
* import './utils'
*
* Usage:
*
* cy.logObj(someObjVar)
*
*/
const getPath = (path, index) => {
if (!path) {
return index
}
if (/[\d]+/.test(index)) {
path += '[' + index + ']'
} else {
path += '.' + index
}
return path
}
const logObj = (obj, path = '') => {
for (let i in obj) {
if (typeof obj[i] !== 'object') {
cy.log(getPath(path, i) + ': **' + obj[i] + '**')
} else {
logObj(obj[i], getPath(path, i))
}
}
}
Cypress.Commands.add('logObj', logObj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment