我有一个测试用例,其中有一个在新选项卡中打开的链接。由于 Cypress 不支持多个选项卡,因此我想获取该链接的 href
属性,然后在同一选项卡中打开它。我正在尝试这样做,但由于某种原因它不起作用。
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');
});
最佳答案
下面的代码应该可以实现您想要实现的目标。还有一整个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
关于javascript - Cypress 获取 href 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48348354/