Cypress web自动化5-table表格元素(别名使用Aliasing)
前言
页面上有些元素定位路径比较复杂,可以先定位到该元素使用别名,通过这个别名去操作元素,这样看起来简洁一些。
.as()使用别名定位元素
table表格上的元素定位使用示例
<table class="as-table table table-bordered">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr>
<td>
Row 1: Cell 1 <button class="btn btn-primary btn-success">Changed</button>
</td>
<td>
Row 1: Cell 2 <button class="btn btn-primary">Change</button>
</td>
</tr>
<tr>
<td>
Row 2: Cell 1 <button class="btn btn-primary">Change</button>
</td>
<td>
Row 2: Cell 2 <button class="btn btn-primary">Change</button>
</td>
</tr>
</tbody>
</table>
// The following DOM command chain is unwieldy.
// To avoid repeating it, let's use `.as()`!
cy.get('.as-table')
.find('tbody>tr').first()
.find('td').first()
.find('button').as('firstBtn')
// To reference the alias we created, we place an
// @ in front of its name
cy.get('@firstBtn').click()
cy.get('@firstBtn')
.should('have.class', 'btn-success')
.and('contain', 'Changed')
.as() 重新命名路由
当点页面上某个按钮,发网络请求的时候,我们可以判断它的reponse
当点上面按钮后,发了个网络请求,于是我们需要判断这个请求是否请求成功,状态码反回200
// Alias the route to wait for its response
cy.server()
cy.route('GET', 'comments/*').as('getComment')
// we have code that gets a comment when
// the button is clicked in scripts.js
cy.get('.network-btn').click()
// https://on.cypress.io/wait
cy.wait('@getComment').its('status').should('eq', 200)
QQ交流群:939110556
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
2019-05-11 appium+python自动化64-使用Uiautomator2执行driver.keyevent()方法报错解决
2018-05-11 appium+python自动化45-夜神模拟器连不上(adb server version (36) doesn't match this client (39); killing...)