使用赛普拉斯的 e2e 测试中的身份验证错误:chrome-error://chromewebdata [英] Error with authentication in e2e tests using cypress: chrome-error://chromewebdata

查看:7387
本文介绍了使用赛普拉斯的 e2e 测试中的身份验证错误:chrome-error://chromewebdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 cypress 为我的 UI 编写 E2E 测试(请注意,它是 PowerBI 报告,因此是一种特殊情况).当我使用公开报告进行测试时,它工作正常.但是当它是私人 PBI 报告时,我在登录部分遇到问题.经过一番研究,我发现这种方法对基于 Azure AD 的身份验证很有前景,并在我的 commands.js 文件中添加了这个登录功能:

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")
        }
    })
})

请注意,客户端 ID 和密码正确且有权访问 powerbi 报告.我还测试了生成的令牌和 sessionStorage 变量,似乎都正确分配了.现在,在我的测试中:

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");
        //...
    });
})

我在 cypress 测试运行程序中看到,即使在 beforeEach 中调用了 login,但在访问 powerbi 报告时,它仍然重定向到 https://login.microsoftonline.com url 具有不同的客户端 id 作为查询参数,并且由于 powerbi 报告的超级域和重定向的 url 不同,它给出了 chrome-error://chromewebdata 错误(我猜这就是原因).因此想知道如何在 azure ad auth 支持的 cypress 测试中登录网站.

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'.

编辑 1:切换到 Edge 不会出现 chrome webdata 错误,但对 URL 的 cy.visit 仍然会超时并出现 HTTP 431 错误(请求标头太长)并且无法进行身份验证.

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.

编辑 2(有关身份验证错误的更多详细信息):在使用客户端凭据生成令牌时,我正在获取令牌,并看到它存储在会话存储中,但是赛普拉斯测试没有选择相同的令牌来授权访问到 PowerBI 报告.因此,基本上即使存在 cookie 来验证请求,对 Power BI 访问的请求仍会重定向到 login.microsoftonline.com/common/oauth2/authorize?client_id={与我在上述 POST 中使用的不同的客户端 ID调用}

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}

然而,在使用用户名/密码时;收到此错误:错误":interaction_required",error_description":AADSTS50079:由于管理员进行了配置更改,或者因为您移动到了新位置,您必须注册多重身份验证才能访问错误代码":[50079]

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]

推荐答案

目前(20 年 4 月 17 日),这可能与赛普拉斯团队的未解决问题有关:https://github.com/cypress-io/cypress/issues/4220

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

特别是对我来说,我曾经有一个超级域在以前的版本 4.3.0 中出现此错误,但现在使用 4.4.0,我得到更多域具有相同的问题.

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.

当前解决方法:回滚到以前的版本并通过 Edge 运行(无论如何它都基于 Chromium).

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

这篇关于使用赛普拉斯的 e2e 测试中的身份验证错误:chrome-error://chromewebdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆