简体   繁体   English

Azure B2C用cypress登录

[英]Azure B2C login with cypress

I've an angular project and I tried to test the azure b2c login with cypress.我有一个 angular 项目,我尝试使用 cypress 测试 azure b2c 登录。 I'm a newBie and I tried in this way:我是新手,我尝试过这种方式:

 describe('login', () => {
    beforeEach(() => cy.clearCookies());
    it('get the access token test', () => {
       cy.visit('/');
       cy.get('button').click();
       cy.get('input#email').type('myemail@gmail.com');
       cy.get('input#password').type('mypassword');
       cy.get('button#next').click();
    });
 });

It works but when I completed the login I have an error:它可以工作,但是当我完成登录时出现错误:

{
  "type" : "https://www.jhipster.tech/problem/problem-with-message",
  "title" : "Unauthorized",
  "status" : 401,
  "detail" : "Not Authenticated",
  "path" : "/login",
  "message" : "error.http.401"
}

If I refresh the browser the test works correctly and I land on the homepage as expected.如果我刷新浏览器,测试会正常工作,并且我会按预期登陆主页。 But I can't understand why I've this error the first time.但我不明白为什么我第一次出现这个错误。

This issue is visible only with cypress.此问题仅在 cypress 中可见。 If I use all other browsers I can't see this issue.如果我使用所有其他浏览器,我看不到这个问题。

• You are getting the error first time when trying to login because you haven't mentioned the exact link or login URI of the Azure AD platform. • 第一次尝试登录时出现错误,因为您没有提及 Azure AD 平台的确切链接或登录 URI。 Also, since you have mentioned 'cy.visit('/')' in the cypress command which means the default URI link as configured in your Azure AD tenant.此外,由于您在 cypress 命令中提到了 'cy.visit('/')',这意味着在 Azure AD 租户中配置的默认 URI 链接。 And it takes some time to resolve this for the first time due to which you are encountering the error.由于您遇到错误,第一次解决此问题需要一些时间。

• Also, your webpage refresh timeout might also be low during which the redirection to the Azure AD tenant and its resolution in the background is not possible. • 此外,您的网页刷新超时也可能很低,在此期间无法重定向到 Azure AD 租户及其在后台的解析。 You need to enter the below commands in your cypress script to fix this instead of current 'cy.visit': -您需要在 cypress 脚本中输入以下命令来解决此问题,而不是当前的“cy.visit”:-

 ‘ it('get the access token test', onBeforeLoad: (contentWindow) => {
     cy.visit(‘https://login.microsoftonline.com/oauth2/v2.0/’);
      cy.wait(2000) ’

The above commands will ensure correct redirection and wait timeout to about 2000 ms, ie, 2 seconds so that your page loads up on the first time itself correctly.上述命令将确保正确的重定向和等待超时约 2000 毫秒,即 2 秒,以便您的页面在第一次正确加载。

Please find the below link for your reference: -请找到以下链接供您参考:-

https://docs.cypress.io/api/commands/visit#Provide-an-onBeforeLoad-callback-function https://docs.cypress.io/api/commands/visit#Provide-an-onBeforeLoad-callback-function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM