Last active
May 6, 2023 21:49
-
-
Save camilokawerin/38f48121a7a39702759f763325ec0979 to your computer and use it in GitHub Desktop.
Log object properties in Cypress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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