Balaji Venkatachalam
Balaji Venkatachalam

Reputation: 363

Cypress does not restore Session when using Azure Active Directory login

I'm trying to authenticate using Azure Active Directory and here is my code. I'm creating a method in cypress>support>commands.js

Cypress.Commands.add('authenticate', (username, password) => {
    const activeDirectoryURL = 'login.microsoftonline.com';

    cy.session('test', () => {
        cy.visit(config.baseUrl);
        // Login to your AAD
        cy.origin(
            activeDirectoryURL,
            {
                args: {
                    username,
                },
            },
            ({ username }) => {
                cy.get('input[type="email"]').type(username, {
                    log: false,
                });
                cy.get('input[type="submit"]').click();
            },
        ),
            cy.origin(
                activeDirectoryURL,
                {
                    args: {
                        password,
                    },
                },
                ({ password }) => {
                    cy.get('input[type="password"]').type(password, {
                        log: false,
                    });
                    cy.get('input[type="submit"]').click();
                },
            ),
            {
                validate() {
                    cy.url().should('include', config.baseUrl);
                },
                cacheAcrossSpecs: true,
            };
    });
});

I wan't to get this called only once before all the spec files and i'm using the below code under cypress>support>e2e.js

before(() => {    
    cy.authenticate(Cypress.env.email,Cypress.env.userPassword);
});

What's happening is cy.authenticate() is triggered before each spec file and the session is not restored instead. Not sure what exactly is missing here? Between each tests within the same spec file, its restored but not across different spec files.

I have set

testIsolation: false,
experimentalInteractiveRunEvents: true,

under cypress.config.ts

I want this session created only once before all spec's and restored across them.

Cypress Version i'm using is 14.0.0

All the answers similar to this question refers to "'This file runs before every single spec file'" whereas this question is basically about running this method before all the spec files i.e., only once before all spec file runs.

Upvotes: 0

Views: 40

Answers (0)

Related Questions