Cypress get href attribute(赛普拉斯获取 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 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:赛普拉斯获取 href 属性


基础教程推荐
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- npm start 错误与 create-react-app 2022-01-01