我有一个测试用例,其中有一个在新选项卡中打开的链接。由于 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');
});
下面的代码应该可以实现您想要实现的目标。还有一整套包含有关如何测试在新选项卡中打开的链接的建议的配方 https://github.com/cypress-io/cypress-example-recipes/blob/master/examples/testing-dom__tab-handling-links/cypress/e2e/tab_handling_anchor_links_spec.cy.js.
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 https://on.cypress.io/variables-and-aliases
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)