claudiut
claudiut

Reputation: 1711

Cypress - automatically run all tests in browser, with hot reload

By default, running cypress open opens the Cypress window and then I have to manually hit the "Run All Tests" button to run them all.

How can run all tests in the browser just by running the cypress open, with no additional step?

Thank you.

Edit: I need the tests to rerun when I change the test files, just like cypress open does, so just running them once (like in headless mode) doesn't help.

Upvotes: 16

Views: 17773

Answers (4)

Gianni Fontanot
Gianni Fontanot

Reputation: 103

If you want to use npx cypress open, then in package.json set the flag "setnonGlobalStepDefinitions" to false, and then indicate the folder containing the files in "stepDefinitions".

{
  "devDependencies": {
    "cypress": "^9.1.0",
    "cypress-cucumber-preprocessor": "^4.3.0"
  },
  "cypress-cucumber-preprocessor": {
    "nonGlobalStepDefinitions": false,
    "stepDefinitions": "cypress/integration/"
  }
}

Your tests will be run one after another.

Upvotes: 0

conny
conny

Reputation: 10145

You can tell Cypress UI's interface (remember that the Cypress UI lives in the DOM like any other page) to rerun the suite - just send some predefined signal from your app when it starts up, and let Cypress use jQuery directly (avoiding a log-event) to find its own reload button and click it.

In my example that "signal" is a certain console log message my React app burps up during its startup:

Cypress.on("window:before:load", (win) => { // registers callback early, long before the app is loaded
  win.console._log = win.console.log;
  win.console.log = function monkeyPatchedConsoleLog() {
    if (arguments[0].match(/^MyApp starting: \(.*\)/)) { // match the app's startup message
      cy.$$(".restart", top.document).click(); // click Cypress' own reload button
    }
    return win.console._log.apply(win.console, arguments); // otherwise log normally
  };
});

(include this somewhere within your support/* files)

Upvotes: 0

Craig
Craig

Reputation: 348

When using cypress open you can get tests to rerun in the browser after each edit by using the global configuration option watchForFileChanges as detailed here

You can pass this as a command line argument:

cypress open --config watchForFileChanges=true

Or you can specify it in your cypress.json file:

{
    "watchForFileChanges": true
}

You will still need to click run all specs when you first run cypress open, but after that any edit to the test files will cause the tests to be rerun.

Upvotes: 12

red_dorian
red_dorian

Reputation: 367

If you run Cypress tests headlessly cypress run it runs all tests without the need to click the "Run all tests" button.

I've found using npx cypress run is the best way to do this. The documentation on running cypress headlessly specifies additional options you can use: https://docs.cypress.io/guides/guides/command-line.html#

Upvotes: 9

Related Questions