In Cypress, there are several ways to run multiple tests without closing the browser. I will start with the most basic methods and provide specific examples to demonstrate how to achieve this.
-
Using the
cypress runcommand By default, when you use thecypress runcommand, Cypress automatically runs all test files in thecypress/integrationfolder. The browser remains open until all tests are completed. For example:bashnpx cypress runThis command runs all test files once without manual intervention in between.
-
Configuring
cypress.jsonIn thecypress.jsonconfiguration file, you can specify particular test files to run by setting the appropriate file patterns in thetestFilesproperty. For example, if you want to run all tests in theloginfolder, you can configure it as:json{ "testFiles": "**/login/*.js" }This will execute all specified test files in a single run.
-
Organizing Tests with Test Suites When writing tests, you can group similar tests using the
describeandcontextfunctions. This allows you to run only a specific group of tests without executing all tests. For example:javascriptdescribe('User Login Flow', () => { it('should correctly fill in the username', () => { // Test content }); it('should correctly fill in the password', () => { // Test content }); });In the Cypress test runner, you can choose to run only the tests under "User Login Flow".
-
Running Specific Files or Tests via Command Line Cypress allows you to directly specify running a single file or a single test via the command line. This can be achieved by passing the file path or using the
--specparameter. For example:bashnpx cypress run --spec "cypress/integration/login/loginTest.js"This command will run only the tests in the
loginTest.jsfile.
The methods listed above provide several ways to run multiple tests in Cypress without closing the browser. They can be flexibly applied to meet various testing needs and scenarios.