赛普拉斯获取 href 属性

时间:2023-04-21
本文介绍了赛普拉斯获取 href 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个测试用例,其中有一个在新选项卡中打开的链接.由于赛普拉斯不支持多个选项卡,我想获取该链接的 href 属性,然后在同一个选项卡中打开它.我正在尝试这样做,但由于某种原因它不起作用.

I have a test case in which I have a link which opens in a new tab. Since Cypress doesn't support multiple tabs, I want to get the href attribute of that link and then open it in the same tab. I'm trying to do it this way, but for some reason it doesn't work.

it('Advertise link should refer to Contact page', () => {
    var link = document.querySelector("div.footer-nav > ul > li:nth-child(2) > a").href;
    cy.visit(link);
    cy.title().should('include', 'Text');
});

推荐答案

下面的代码应该可以实现您想要实现的目标.还有一个完整的 提供有关如何测试在新标签页中打开的链接的建议.

The code below should do what you're trying to achieve. There is also an entire recipe with suggestions on how to test links that open in new tabs.

it('Advertise link should refer to Contact page', () => {
   cy.get('div.footer-nav > ul > li:nth-child(2) > a')
     .should('have.attr', 'href').and('include', 'contact')
     .then((href) => {
       cy.visit(href)
     })
})

我还建议通读 Cypress 文档,了解分配和使用变量的最佳方法:https://on.cypress.io/variables-and-aliases

I would also suggest reading through the Cypress document on the best ways to assign and work with variables: https://on.cypress.io/variables-and-aliases

这篇关于赛普拉斯获取 href 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:使用 Puppeteer 检测和测试 Chrome 扩展程序 下一篇:Postman:如何在 Postman 自动化中检查字段是否返回 null

相关文章