【问题标题】:Cypress, how do you visit two different domains for a single test?赛普拉斯,您如何访问两个不同的域进行一次测试?
【发布时间】:2021-12-07 22:36:55
【问题描述】:

我有以下几点:

describe('Page not authenticated', () => {
    it('Page has authentication', () => {
        cy.visit('https://myauthsite.com/logout');
        cy.visit('/', { failOnStatusCode: true });
        cy.url().should('include', 'https://myauthsite.com/');
    });
})

出于内部原因,该软件包已设置为始终具有持久会话,因此我需要进行测试以访问特定 URL,该 URL 将显式注销并“终止”会话,然后访问我的主页我试图测试以确保它确实将我重定向到正确的页面。但是,我遇到了这个错误

You may only `cy.visit()` same-origin URLs within a single test.

The previous URL you visited was:

  > 'https://myauthsite.com'

You're attempting to visit this URL:

  > 'https://myactualsite.com'

You may need to restructure some of your test code to avoid this problem.

https://on.cypress.io/cannot-visit-different-origin-domain

列出的许多解决方法主要是地址检查属性而不是访问 url,或者将访问分成两个不同的测试用例,但在我的情况下,我真的需要它在自己的封装测试中,是不可能?

【问题讨论】:

    标签: cypress


    【解决方案1】:

    cy.request() 不受相同的跨域请求策略的约束,因此如果您可以通过 HTTP 请求注销就可以了。

    【讨论】:

      【解决方案2】:

      我尝试使用chromeWebSecurity=false,但没有帮助。

      我目前正在使用API​​登录,设置cookie,后来读取“Google Sign in with Cypress”''

      cy.request({
            method:"POST",
            url:API_URL,
            headers: {
               'Content-Type': 'application/json',
            },
            body: api_body}).then((response =>{
            cy.log(response.body)
            cy.setCookie("token_name",response.body['token']);
            cy.visit(destination_url);
         }))
      

      API_URL 是您的 API 登录网址:

      token_name: Login manually to UI, Using the web developer tool you can get the required token_name
      

      这解决了我的多域登录问题,您可以使用它进行多域导航。

      【讨论】:

        猜你喜欢
        • 2023-02-14
        • 2021-05-23
        • 2022-12-10
        • 2019-07-29
        • 1970-01-01
        • 2021-06-13
        • 1970-01-01
        • 1970-01-01
        • 2022-10-24
        相关资源
        最近更新 更多