Cypress page behaves differently when trying to use two domains in a single test

Asked

Viewed 277 times

1

Procedure in Google Chrome

I have an Azure B2C login page, enter the user and password and press the "Login" button and then normally. In this process I am redirected to my application page with a token:

https://minhapagina?token=a1df6a5d1fa65...

This page is a Webform with some radios button where I have a button to access the application, if I click this button I access the application.

Cypress code

The code is very simple

describe('Test B2C', () => {

    it('Login', () => {

        var testUrl = 'https://urlB2C...';
        
        cy.visit(testUrl).then(() => {
            cy.get('#signInName').type('usuario');
            cy.get('#password').type('senha');
            cy.get('#next').click();
        });

    });

})

Problem

Performing the manual process, in case using Google Chrome works normally, however doing the same process with Cypress when arriving at the last step which is to click the screen button with the radios button I am redirected to the B2C login screen without any error message or warning.

Remarks

  • If you execute only the command cy.visit to open the page and do all manual process within Cypress the problem continues.
  • If you do the procedure by taking the url directly https://minhapagina?token=a1df6a5d1fa65... inside the Cypress works.
  • I did the same automation using Selenium and it also worked.
  • The flag chromeWebSecurity already this with the value false.

1 answer

2

Problem

You can disable Google Chrome security to accept forwarding to another domain.

Set chromeWebSecurity to false

But can’t work with two domains in one test.

Same superdomain per test

Solution so far

We will then create two tests, in which the first will be run and send the URL from the first to the second. So each test will work with a different domain.

In the first test access the first host and get the current URL:

var currentUrl = null;

cy.url().then(url => {
    currentUrl = url;
});

Right after monitor if the URL is changed, if URL is different from the first saved it in a variable:

var urlToken = "";

cy.on('url:changed', url => {
    if (currentUrl == null) return;
    if (currentUrl == url) return;
    urlToken = url;
});

If we leave this way the forehead passes to the next before even redirecting, for that I implement a wait:

cy.wait(20000).then(fim => {
    cy.writeFile(tempFileName, urlToken);
    expect(urlToken).to.not.be.empty;
})

Then giving time to redirect and finally saving my URL with the token in an archive

This was the only way I could find to pass this information to the next test, since when starting the next one, all data is zeroed.

And finally get the file in the next test with the URL with token:

cy.readFile(tempFileName).then((url) => {
    cy.visit(url);
    cy.get('#btnSelectLogin').click();
})

Complete code

describe('Login', () => {

    var tempFileName = "tempFileName.txt";

    beforeEach(() => {
    })

    it('B2C', () => {

        var testUrl = 'https://urlB2C...';

        cy.visit(testUrl);

        cy.get('#signInName').type('usuario');
        cy.get('#password').type('senha');
        cy.get('#next').click();

        var currentUrl = null;

        cy.url().then(url => {
            currentUrl = url;
        });

        var urlToken = "";

        cy.on('url:changed', url => {
            if (currentUrl == null) return;
            if (currentUrl == url) return;
            urlToken = url;
        });

        cy.wait(20000).then(fim => {
            cy.writeFile(tempFileName, urlToken);
            expect(urlToken).to.not.be.empty;
        })

    });

    it('URL Token', () => {

        cy.readFile(tempFileName).then((url) => {
            cy.visit(url);
            cy.get('#btnSelectLogin').click();
        })

    });

})

Problem of the solution

  • I stay depending on the time set on wait, then if the request takes longer than expected I will not have the expected result and if the request is quick I will have to wait anyway.
  • I need to create a file to exchange information between tests.

Attempts to improve

  • I tried to create a global variable, it works if both tests run using the same domain, but when loading the new domain all information is reset including the global variable.
  • I tried to create Promise, if put await the Cypress complains that it passed more than 4 seconds or if I leave without, the next test runs missing so the URL with the token.

Expected solution

It would be an idea if the next test ran exactly the moment I got the new URL with the token.

  • 1

    Good answer. That hole has some alternatives waiting to load the new page, maybe some will serve you better. This specific point may even be a new question, it is definitely useful for those who use Cypress :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.