【问题标题】:Cypress - stall next page navigationCypress - 停止下一页导航
【发布时间】:2021-05-26 01:46:08
【问题描述】:

假设我知道下一页导航的 URL,有没有办法阻止执行导航的下一个操作,无论导航是由 href 还是 js 引导的?试过cy.intercept 不行。

// doesn't work
cy.intercept({ hostname: 'cross.domain' }, (req) => {})
cy.contains('Next').click()

【问题讨论】:

  • 您是否介意分享更多关于您的特定用例以及您最终要在测试中实现的目标?
  • @Kerry,如代码所示我试图点击一个元素,但想阻止页面导航到另一个域,这是赛普拉斯禁止的。
  • 请看@Undistraction 的问题How can I test a client-side redirect to a 3rd party site with Cypress,对javascript 重定向很有用。

标签: cypress


【解决方案1】:

你可以使用JQ .attr():


    cy.get(`aTagSelector`) //This part is recommended to check if you click the required button with the link to cross origin
        .should('have.attr', 'href')
        .and('include', `expectedUrl`)
    cy.get(`aTagSelector`)
        .then($aTagElement => {
            $aTagElement.attr("href", '/');
            cy.get($aTagElement)
                .click({force: true})
        })

【讨论】:

  • 您不需要Cypress.$(aTagElement).attr(),因为aTagElement 已经是一个jquery 对象。按照惯例,你会在它前面加上$,即.then($aTagElement => {,以避免混淆。
  • 你能解释一下为什么用/替换href值吗?
  • / 打开您所在的同一页面。您可以将其替换为您希望出现的任何网址。
  • 如果点击基于href的没有触发导航怎么办?
  • @RosenMihaylov,我想说非基于 href 的导航更受欢迎,例如使用 location.href、window.open()、window.replace()。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-16
相关资源
最近更新 更多