Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run multiple tests in Cypress without closing browser?

I have a 3 spec files for testing form filling and photo uploading using cypress.

  1. spec file: Fill a form
  2. spec file: Upload a photo
  3. spec file: Check the results

Form is - https://demoqa.com/automation-practice-form

Problem is when fill a form test passes, browser is closed and 2 other test fail because I don't call open url in each test case.

If I run tests in GUI, everything is fine. Browser is not closed after first test, but when I run

npx cypress run --headed --browser chrome

from command line, problem occures.

I want to run tests from command line so browser doesn't close till all tests are finished.

like image 545
user3850734 Avatar asked Oct 19 '25 19:10

user3850734


1 Answers

I got exactly same problem and after hours of research I found a workaround that solves this. It is not a real fix but it solves your issue and mine. Here are the steps I did:

Create a 4th spec file, call it main.spec.ts, inside this file do an import for the 3 spec files you have:

import './fillform.spec.js'
import './uploadphoto.spec.js'
import './result.spec.js'

In cypress.json file, add the following entry to allow to run only the main spec file:

"testFiles": ["main.spec.js"],

Now cypress run will work exactly as cypress open and all specs will run without closing the browser in between:

npx cypress run --headed --browser chrome
like image 131
Wael Avatar answered Oct 21 '25 10:10

Wael