Does cypress support soft assertion? For example - I am navigating through 'n' number of elements and want to verify their values. If value of any element is mismatched then it fails the test. It doesn't continue to verify the value of next elements. Is there a way to verify all elements values and show the failed ones at the end?
Asked
Active
Viewed 5,749 times
3
-
Probably the easiest way to do that is to get an array of the values and compare it to an array of expected values. – jonrsharpe Apr 26 '19 at 12:56
-
@jonrsharpe Thanks for reply. I need something like - https://www.softwaretestingmaterial.com/soft-assert/ – user3048235 Apr 26 '19 at 13:02
1 Answers
2
EDIT: I probably misunderstood.. if you meant soft assertions of cy
commands (not chai's expect
/assert
), then this answer is not for you.
WTBS, you could use the below solution and do something like:
describe('test', () => {
const { softExpect } = chai;
it('test', () => {
cy.document().then( doc => {
doc.body.innerHTML = `
<div class="test">1</div>
<div class="test">2</div>
<div class="test">3</div>
`;
});
cy.get('.test').each( $elem => {
softExpect($elem[0].textContent).to.eq('2');
});
});
});
You could do something like this.
support/index.js
let isSoftAssertion = false;
let errors = [];
chai.softExpect = function ( ...args ) {
isSoftAssertion = true;
return chai.expect(...args);
},
chai.softAssert = function ( ...args ) {
isSoftAssertion = true;
return chai.assert(...args);
}
const origAssert = chai.Assertion.prototype.assert;
chai.Assertion.prototype.assert = function (...args) {
if ( isSoftAssertion ) {
try {
origAssert.call(this, ...args)
} catch ( error ) {
errors.push(error);
}
isSoftAssertion = false;
} else {
origAssert.call(this, ...args)
}
};
// monkey-patch `Cypress.log` so that the last `cy.then()` isn't logged to command log
const origLog = Cypress.log;
Cypress.log = function ( data ) {
if ( data && data.error && /soft assertions/i.test(data.error.message) ) {
data.error.message = '\n\n\t' + data.error.message + '\n\n';
throw data.error;
}
return origLog.call(Cypress, ...arguments);
};
// monkey-patch `it` callback so we insert `cy.then()` as a last command
// to each test case where we'll assert if there are any soft assertion errors
function itCallback ( func ) {
func();
cy.then(() => {
if ( errors.length ) {
const _ = Cypress._;
let msg = '';
if ( Cypress.browser.isHeaded ) {
msg = 'Failed soft assertions... check log above ↑';
} else {
_.each( errors, error => {
msg += '\n' + error;
});
msg = msg.replace(/^/gm, '\t');
}
throw new Error(msg);
}
});
}
const origIt = window.it;
window.it = (title, func) => {
origIt(title, func && (() => itCallback(func)));
};
window.it.only = (title, func) => {
origIt.only(title, func && (() => itCallback(func)));
};
window.it.skip = (title, func) => {
origIt.skip(title, func);
};
beforeEach(() => {
errors = [];
});
afterEach(() => {
errors = [];
isSoftAssertion = false;
});
And in your spec:
describe('test', () => {
const { softAssert, softExpect } = chai;
it('test', () => {
cy.wrap([1, 42, 3]).then( vals => {
vals.forEach( val => {
softAssert(val === 42, `${val} should equal 42`);
});
});
});
it('test2', () => {
cy.wrap([2, 5, 4]).then( vals => {
vals.forEach( val => {
softExpect(val).to.eq(42);
});
});
});
});
Cypress log:
Terminal (headless run):

dwelle
- 6,894
- 2
- 46
- 70
-
`it.only` stops working with Cypress 4.0.0 (previous version 3.8.0) using the suggested monkey patch. This affects all versions up to the current Cypress 10.3.0. An error message such as `Cannot read properties of undefined (reading 'parent')` is output when a test spec including `it.only` is run. Is there any suggested modification available to make this work with Cypress 4 and later? – MikeMcC399 Jul 06 '22 at 13:11
-
I have posted the question also to https://github.com/cypress-io/cypress/discussions/23280. – MikeMcC399 Aug 11 '22 at 12:40
-
Apologies, I'm not actively working with Cypress so can't help, but I find it very dubious that `it.only` would be removed (quick check of the [Changelog](https://docs.cypress.io/guides/references/changelog) confirms that it was not). – dwelle Aug 11 '22 at 20:44
-
1`it.only` has not been removed from Cypress and works without issue in an out-of-the-box installation of Cypress. It is only a problem together with the monkey-patch posted here which includes the code: ```js window.it.only = (title, func) => { origIt.only(title, func && (() => itCallback(func))); }; ``` modifying how `it.only` works. – MikeMcC399 Aug 12 '22 at 06:31
-
`support/index.js` is for Cypress up to and including version 9. Replace with `support/e2e.js` for Cypress 10. – MikeMcC399 Nov 02 '22 at 08:23
-
`it.skip` produces the error message: "Cannot set properties of undefined (setting 'body')" after updating from Cypress 10.10.0 to 10.11.0 with this monkey-patch. – MikeMcC399 Nov 03 '22 at 10:04