前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Cypress学习6- Assertions断言使用(should, expect)

Cypress学习6- Assertions断言使用(should, expect)

作者头像
上海-悠悠
发布2020-05-13 14:35:09
3K0
发布2020-05-13 14:35:09
举报

前言

每个测试用例需要加断言,Cypress里面断言常用的有should, expect

隐式断言

.should()

可以使用.should()给当前用例加断言

  • should(‘have.class’, ‘success’) 断言元素的class属性值是 ‘success’
  • should(‘have.text’, ‘Column content’) 断言元素文本值 ‘Column content’
  • should(‘contain’, ‘Column content’) 断言元素文本包含 ‘Column content’
  • should(‘have.html’, ‘Column content’) 断言元素html文本’Column content’
  • should(‘match’, ‘td’) chai-jquery 使用 “is()”检查元素是否与选择器匹配
  • .invoke(‘text’) .should(‘match’, /column content/i) 文本与正则表达式匹配先使用invoke结合should
  • .contains(‘text’) 文本与正则表达式匹配元素文本包含,这种比上面更好
代码语言:javascript
复制
<table class="table table-bordered assertion-table">
                <thead>
                  <tr>
                    <th>#</th>
                    <th>Column heading</th>
                    <th>Column heading</th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">1</th>
                    <td>Column content</td>
                    <td>Column content</td>
                  </tr>
                  <tr>
                    <th scope="row">2</th>
                    <td>Column content</td>
                    <td>Column content</td>
                  </tr>
                  <tr class="success">
                    <th scope="row">3</th>
                    <td>Column content</td>
                    <td>Column content</td>
                  </tr>
                </tbody>
              </table>
代码语言:javascript
复制
cy.get('.assertion-table')
  .find('tbody tr:last').should('have.class', 'success')
  .find('td')
  .first()
  // checking the text of the  element in various ways
  .should('have.text', 'Column content')
  .should('contain', 'Column content')
  .should('have.html', 'Column content')
  // chai-jquery uses "is()" to check if element matches selector
  .should('match', 'td')
  // to match text content against a regular expression
  // first need to invoke jQuery method text()
  // and then match using regular expression
  .invoke('text')
  .should('match', /column content/i)

// a better way to check element's text content against a regular expression
// is to use "cy.contains"
// https://on.cypress.io/contains
cy.get('.assertion-table')
  .find('tbody tr:last')
  // finds first  element with text content matching regular expression
  .contains('td', /column content/i)
  .should('be.visible')

.and()

针对同一元素多个断言,可以使用 and 语法

代码语言:javascript
复制
cy.get('.assertions-link')
  .should('have.class', 'active')
  .and('have.attr', 'href')
  .and('include', 'cypress.io')

显示断言

expect()

针对项目 BDD 断言方式

代码语言:javascript
复制
expect(true).to.be.true
const o = { foo: 'bar' }
expect(o).to.equal(o)
expect(o).to.deep.equal({ foo: 'bar' })
// matching text using regular expression
expect('FooBar').to.match(/bar$/i)

assert()

针对项目 BDD 断言方式

代码语言:javascript
复制
const person = {
  name: 'Joe',
  age: 20,
}
assert.isObject(person, 'value is object')

Should with callback function

下面这段我也看不懂了,不翻译了 You can write your own complicated checks using .should(cb) function if included assertions are not enough. Pass a function to should() with any number of explicit assertions within it. The callback function will be retried until it passes all your explicit assertions or times out.

代码语言:javascript
复制
cy.get('.assertions-p').find('p')
  .should(($p) => {
    // return an array of texts from all of the p's
    let texts = $p.map((i, el) => // https://on.cypress.io/$
      Cypress.$(el).text())

    // jquery map returns jquery object
    // and .get() convert this to simple array
    texts = texts.get()

    // array should have length of 3
    expect(texts).to.have.length(3)

    // use second argument to expect(...) to provide clear
    // message with each assertion
    expect(texts, 'has expected text in each paragraph').to.deep.eq([
      'Some text from first p',
      'More text from second p',
      'And even more text from third p',
    ])
  })

Assert that element’s class includes heading-.

代码语言:javascript
复制
cy.get('.docs-header').find('div')
  // .should(cb) callback function will be retried
  .should(($div) => {
    expect($div).to.have.length(1)

    const className = $div[0].className

    expect(className).to.match(/heading-/)
  })
  // .then(cb) callback is not retried,
  // it either passes or fails
  .then(($div) => {
    expect($div).to.have.text('Introduction')
  })

You can throw any error from the callback function. The callback will be retried, but the assertions will not be shown as nicely in the Command Log UI as Chai assertions.

代码语言:javascript
复制
cy.get('.docs-header').find('div')
  .should(($div) => {
    if ($div.length !== 1) {
      // you can throw your own errors
      throw new Error('Did not find 1 element')
    }

    const className = $div[0].className

    if (!className.match(/heading-/)) {
      throw new Error(`Could not find class "heading-" in ${className}`)
    }
  })

We strongly recommend that your tests are deterministic. But sometimes you might need to match text between two elements, and you do not know what that text should be. Save the value from the first element, then compare it from a should(cb) callback.

代码语言:javascript
复制
let text
/**
* Normalizes passed text,
* useful before comparing text with spaces and different capitalization.
* @param {string} s Text to normalize
*/
const normalizeText = (s) => s.replace(/\s/g, '').toLowerCase()

cy.get('.two-elements')
.find('.first')
.then(($first) => {
  // save text from the first element
  text = normalizeText($first.text())
})

cy.get('.two-elements')
.find('.second')
.should(($div) => {
  // we can massage text before comparing
  const secondText = normalizeText($div.text())
  expect(secondText, 'second text').to.equal(text)
})
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-05-13,如有侵权请联系 cloudcommunity@tencent.com 删除

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 隐式断言
    • .should()
      • .and()
      • 显示断言
        • expect()
          • assert()
          • Should with callback function
          领券
          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档