Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop cypress from closing browser after each test case (it)?

I have a cy.js file like this

/// <reference types="cypress" />
context("Dummy test cases", () => {
  it("open Google", () => {
    cy.visit("www.google.com");
  });

  it("search a keyword", () => {
    cy.get(".gLFyf").type("cypress{enter}");
  });
});

As you can see I've 2 test cases. 1 is to open a website and the other is to do other tasks. But the problem is that after finishing the first 'it' (test case 1), the browser closes and then the next case fails because there is no active browser.

Can you help me how to stop cypress from closing the browser after the execution of each test case?

like image 854
Yogendiran Barath Avatar asked Oct 19 '25 08:10

Yogendiran Barath


2 Answers

I found Cypress is quite opinionated about some things, one at the top of the list is "test isolation" which means one test must not influence another test.

Note each it() is a test.

To make your code work, you must turn off test isolation.

If the cypress.config.js file add the option

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    setupNodeEvents(on, config) {
      // implement node event listeners here
    },
    testIsolation: false,
  },
})

Same if you are using typescript.

This got added in the new Version Cypress 12.0.0, in their Changelogs its at the feature section: Added a new configuration option called testIsolation, which defaults to true.

As @Joylette already said, in your config file just use testIsolation: false to deactivate it. If you ever use an older version use testIsolation: off, they renamed it in the newest version.

like image 33
skkrrea Avatar answered Oct 22 '25 04:10

skkrrea