前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Cypress学习5-table表格元素(别名使用Aliasing)

Cypress学习5-table表格元素(别名使用Aliasing)

作者头像
上海-悠悠
发布2020-05-13 14:34:34
7770
发布2020-05-13 14:34:34
举报

前言

页面上有些元素定位路径比较复杂,可以先定位到该元素使用别名,通过这个别名去操作元素,这样看起来简洁一些。

.as()使用别名定位元素

table表格上的元素定位使用示例

代码语言:javascript
复制
<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>
代码语言:javascript
复制
// 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

Get Comment

当点上面按钮后,发了个网络请求,于是我们需要判断这个请求是否请求成功,状态码返回200

代码语言:javascript
复制
// 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)
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • .as()使用别名定位元素
  • .as() 重新命名路由
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档