cypressntlm-authenticationcypress-session

Cypress: Is it possible to navigate to a web page and leave it opened for the next tests?


I am planning to update the Cypress version of an existing project from 6.9.1 to 12.6.0.

Currently we are navigating to a web page and logging in with ntlm-auth in a before() hook. After that the web page remains opened and can be used in all tests which are coming next.

In the latest Cypress version it seems that the page is being cleared and closed after each test case, which is the desired behavior to have better test cases as I understand.

But is there a way in the latest Cypress version to navigate to a web page in a before hook or in the first test, leave the page opened, then in the second test case to interact with it and navigate to another sections of the same page, leave the page opened, etc.?

The existing code structure looks like that:

before(() => {

  cy.ntlm(
    ['<url>'],
    <username>,
    <password>
  );

  cy.visit(<url>);

});

it('Test 1', () => {
  cy.contains('something').click();
});

it('Test 2', () => {
  cy.get('#something').type('{enter}');
});

I have tried to save the session with cy.session() in the before hook and my idea was to restore the session/page in the next tests, but I am not sure if this would be the right approach.


Solution

  • There is a Cypress configuration option to disable test isolation. This can be easily set in your cypress.config.js, in your e2e object.

    const { defineConfig } = require('cypress')
    
    module.exports = defineConfig({
      e2e: {
        // e2e options here
        testIsolation: false,
        // more e2e options here
      },
    })
    

    By default, testIsolation is set to true, so that should explain the behavior you are currently seeing. Additionally, this Cypress page describes what happens when testIsolation is set to false.

    When test isolation is disabled, Cypress will not alter the browser context before the test starts. The page does not clear between tests and cookies, local storage and session storage will be available across tests in that suite. Additionally, the cy.session() command will only clear the current browser context when establishing the browser session - the current page is not cleared.