赛普拉斯获得href属性 [英] Cypress get href attribute

查看:110
本文介绍了赛普拉斯获得href属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试用例,其中我有一个链接在新选项卡中打开,并且因为cypress不支持多选项卡,我想获得该链接的href属性,然后在同一个选项卡中打开它,我`我试图这样做,但由于某种原因它不起作用。

I have a test case in which i have a link which opens in a new tab, and since cypress doesn't support multi tab, i wanna get 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)
     })
})

我还建议阅读赛普拉斯文件关于分配和使用变量的最佳方法: https://on.cypress.io/variables-和 - 别名

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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆