Error with authentication in e2e tests using cypress: chrome-error://chromewebdata
Asked Answered
V

1

1

I'm using cypress for writing E2E tests for my UI (Note that it's a PowerBI report, hence it's kind of special case). When I am testing with a public report, it works fine. But when it's a private PBI report, I am having trouble with login part. After some research, I found this approach promising for Azure AD based auth, and added this login function in my commands.js file:

Cypress.Commands.add('login', () => { 
    cy.request({
        method: 'POST',
        url: 'https://login.microsoftonline.com/{TENANT}/oauth2/token',
        form: true,
        body: {
            grant_type: 'client_credentials',
            client_id: CLIENT_ID,
            client_secret: CLIENT_SECRET,
            // resource: RESOURCE
        },
        header: {
            'Content-Type': 'multipart/form-data'
        }
    }).then((responseData) => {
        if (responseData.status === 200) {
            window.sessionStorage.setItem("adal.idtoken", responseData.body.access_token);
            window.sessionStorage.setItem("adal.token.keys", CLIENT_ID + "|")
            window.sessionStorage.setItem(`adal.expiration.key${CLIENT_ID}`, responseData.body.expires_on)
            window.sessionStorage.setItem(`adal.access.token.key${CLIENT_ID}`, responseData.body.access_token)
        } else {
            console.log("error retrieving token")
        }
    })
})

Note that the Client ID and secret are correct and have permission to access the powerbi report. I also tested the token generated, and the sessionStorage variables, and all seem to be assigned correctly. Now, in my test:

describe("E2E Tests", () => {
    beforeEach(() => {
        cy.login();
    })
    it("Sample Test 1", () => {
        cy.visit("https://powerbi-report-url.com");
        //...
    });
})

And I am seeing in the cypress test runner that, even though login has been called in beforeEach, while visiting the powerbi report, it still redirects to https://login.microsoftonline.com url with a different client id as query param, and since the superdomains of powerbi report and redirected urls are different, it gives chrome-error://chromewebdata error(I guess that's the reason). Hence wondering, how to login to a website in cypress tests backed by azure ad auth.

Also, might be unrelated, but seeing one more error in the console:

Refused to display 'https://powerbi-report-url.com' in a frame because it set 'X-Frame-Options' to 'deny'.

Edit 1: Switching to Edge doesn't give the chrome webdata error, but still the cy.visit to the URL times out and gives HTTP 431 Error(Request header too long) and couldn't authenticate.

Edit 2 (More Details about Auth Error): While generating the toke using client credentials, I am getting the token, and see it's stored in the session Storage, however the cypress tests are not picking the same token to authorize the visit to PowerBI report. So, basically even thought the cookie exist to auth the request, the request to Power BI visit still redirects to login.microsoftonline.com/common/oauth2/authorize?client_id={a different client ID from what I am using in the above POST call}

Whereas, while using username/password; getting this error: "error": "interaction_required", "error_description": "AADSTS50079: Due to a configuration change made by your administrator, or because you moved to a new location, you must enroll in multi-factor authentication to access "error_codes": [50079]

Veroniqueverras answered 16/4, 2020 at 20:57 Comment(0)
A
2

At this moment (17-Apr-20), this might be related to an Open issue with the Cypress team: https://github.com/cypress-io/cypress/issues/4220

For me particularly, I used to have one super-domain having this error with the previous version 4.3.0 but now with 4.4.0, I get more domains having same issue.

Current workaround: Roll back to previous version and run via Edge (which is based on Chromium anyway).

Auramine answered 17/4, 2020 at 0:29 Comment(4)
Thanks for the suggestion @ebanster. Now I am not getting chrome webdata error, after switching to Edge. But when I visit the main URL, it redirects to the login auth URL, and gives timeout error. Do you have any idea why it's not picking up the sessionStorage credentials(set in cy.login() step) to authenticate the request? Also, when I check the cookie "cypress-session-cookie", it's null.Veroniqueverras
See if including this before the commands work: cy.clearCookies({ domain: null })Auramine
Getting the error, while visiting the test URL: Failed to load resource: the server responded with a status of 431 (Request Header Fields Too Large)Veroniqueverras
Can you try to clear Cypress app data? docs.cypress.io/guides/references/…Auramine

© 2022 - 2024 — McMap. All rights reserved.