Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force fail a test in Cypress.io

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?

like image 510
Christian Gentry Avatar asked Jul 11 '19 16:07

Christian Gentry


People also ask

How do you stop a cypress test?

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.

How do you use retry in Cypress?

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.

How do you skip failed test cases in Cypress?

skip in order to skip a test in a cypress suite.


1 Answers

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')
like image 82
Zach Bloomquist Avatar answered Sep 18 '22 15:09

Zach Bloomquist