如果我导航到某个页面,为什么会遇到 cy.origin 问题

问题描述 投票:0回答:2

我试图检查我是否在正确的页面上,但收到此错误:

expected '' to include http://localhost:4200

这是我的代码

cy.visit('https://login');
  cy.get('#username').type(username);
  cy.get('#password').type(password);
  cy.get('#kc-login').click()
  cy.wait(7000)
  cy.url().should('include', 'http://localhost:4200/machines')`

我不明白为什么我的字符串是空的

如果我不使用

cy.visit()
,我会遇到
cy.origin()
问题,我不明白。我没有导航到另一个页面。

我不想使用

cy.origin()
,我不明白为什么我必须使用它。

我找到了

experimentalSkipDomainInjection
设置,但仍然不起作用。

cypress cross-domain
2个回答
6
投票

如果您对 keycloak 感到困扰,请安装此插件 cypress-keycloak

您的问题没有足够的细节来准确回答,但这是要使用的模式

describe('thing', () => {
  beforeEach(() => {
    cy.login({
      root: 'https://keycloak.babangsund.com',
      realm: 'stage',
      username: 'babangsund',
      password: 'bacon',
      client_id: 'frontend',
      redirect_uri: 'https://babangsund.com/',
    });
    
    cy.loginOTP({                               // or login with OTP
      root: 'https://keycloak.babangsund.com',
      realm: 'stage',
      username: 'babangsund',
      password: 'bacon',
      client_id: 'frontend',
      redirect_uri: 'https://babangsund.com/',
      otp_secret: 'OZLDC2HZKM3QUC...', // e.g. 32 chars
      otp_credential_id: '5e231f20-8ca7-35e1-20a694b60181ca9', // e.g. 36 chars
    });
  });

  afterEach(() => {
    cy.logout({
      root: 'https://keycloak.babangsund.com',
      realm: 'stage',
      // using Keycloak < 18.0.0:
      // redirect_uri: 'https://babangsund.com/',
      // using Keycloak >= 18.0.0:
      post_logout_redirect_uri: 'https://babangsund.com/',
    });
  });
})

3
投票

您的

cy.visit()
看起来像是拼写错误。

如果您使用

localhost
,您应该指定完整的 URL

cy.visit('http://localhost/login');
cy.get('#username').type(username);
cy.get('#password').type(password);
cy.get('#kc-login').click()
  
//cy.wait(7000)  // don't do this

cy.url({timeout: 7000}).should('include', 'http://localhost:4200/machines')
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.