首页 > 解决方案 > 如何处理柏树中的新标签。如果子域是动态的?

问题描述

网址:- https://localhost:8008/

子域网址: https://localhost:8008/datasearch?actionId=772675

我无法将赛普拉斯中的新选项卡作为 cy.visit(' https://localhost:8008/datasearch?actionId=772675 ') 处理,因为在输入数据并单击提交按钮后,操作 ID 会不断变化。因为子域会在新选项卡上打开选项卡。我需要检查断言在新选项卡中打开的输出。

问候

库沙尔

标签: cypress

解决方案


我假设在您的情况下,如果您的提交按钮位于“a”标签内,那么您可以执行类似的操作。如果您单击提交按钮,那么它将作为新选项卡打开,因此理想情况下,您应该在单击之前从“a”标签或提交按钮获取链接并传递给 aconst并使用cy.visit()

context('Open as new tab in cypress', () => {
        it('Test to open a new tab/subdomain in cypress', () => {
          cy.visit('https://localhost:8008/');
          cy.get('.mt-2 > .btn-primary').then(($ele)=>{   // if the button is in an '<a>' tag, you could grab something like this
            const newUrl =  Cypress.$($ele).attr('href'); 
            cy.visit(newUrl);
          })
        })
    })

推荐阅读