In Cypress.io is there a way that I can force a test to fail if a certain condition is met?
Example:
On my webpage, if the string "Sorry, something went wrong." is present on the page I want the test to fail. Currently here is what I am doing.
/// <reference types="Cypress" />
describe("These tests are designed to fail if certain criteria are met.", () => {
beforeEach(() => {
cy.visit("");
});
specify("If 'Sorry, something went wrong.' is present on page, FAIL ", () => {
cy.contains("Sorry, something went wrong.");
});
});
Right now, if "Sorry, something went wrong." is found, the test succeeds. How do I fail the test if this condition is met?
Cypress gives us the ability to stop the test at a spot via cy. pause() command. We can use this to stop the test before any action or assertion that is causing our tests to fail. But this command only works when we run Cypress in GUI mode and, it is ignored when we run the tests in headless mode.
You can configure this in the Cypress configuration by passing the retries option an object with the following options: runMode allows you to define the number of test retries when running cypress run. openMode allows you to define the number of test retries when running cypress open.
skip in order to skip a test in a cypress suite.
You can just throw a JavaScript Exception to fail the test:
throw new Error("test fails here")
However, in your situation, I would recommend using the .should('not.exist')
assertion instead:
cy.contains("Sorry, something went wrong").should('not.exist')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With