9
votes

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.

3

3 Answers

5
votes

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#

4
votes

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.

0
votes

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)