assertions.spec.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /// <reference types="cypress" />
  2. context('Assertions', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/commands/assertions')
  5. })
  6. describe('Implicit Assertions', () => {
  7. it('.should() - make an assertion about the current subject', () => {
  8. // https://on.cypress.io/should
  9. cy.get('.assertion-table')
  10. .find('tbody tr:last')
  11. .should('have.class', 'success')
  12. .find('td')
  13. .first()
  14. // checking the text of the <td> element in various ways
  15. .should('have.text', 'Column content')
  16. .should('contain', 'Column content')
  17. .should('have.html', 'Column content')
  18. // chai-jquery uses "is()" to check if element matches selector
  19. .should('match', 'td')
  20. // to match text content against a regular expression
  21. // first need to invoke jQuery method text()
  22. // and then match using regular expression
  23. .invoke('text')
  24. .should('match', /column content/i)
  25. // a better way to check element's text content against a regular expression
  26. // is to use "cy.contains"
  27. // https://on.cypress.io/contains
  28. cy.get('.assertion-table')
  29. .find('tbody tr:last')
  30. // finds first <td> element with text content matching regular expression
  31. .contains('td', /column content/i)
  32. .should('be.visible')
  33. // for more information about asserting element's text
  34. // see https://on.cypress.io/using-cypress-faq#How-do-I-get-an-element’s-text-contents
  35. })
  36. it('.and() - chain multiple assertions together', () => {
  37. // https://on.cypress.io/and
  38. cy.get('.assertions-link')
  39. .should('have.class', 'active')
  40. .and('have.attr', 'href')
  41. .and('include', 'cypress.io')
  42. })
  43. })
  44. describe('Explicit Assertions', () => {
  45. // https://on.cypress.io/assertions
  46. it('expect - make an assertion about a specified subject', () => {
  47. // We can use Chai's BDD style assertions
  48. expect(true).to.be.true
  49. const o = { foo: 'bar' }
  50. expect(o).to.equal(o)
  51. expect(o).to.deep.equal({ foo: 'bar' })
  52. // matching text using regular expression
  53. expect('FooBar').to.match(/bar$/i)
  54. })
  55. it('pass your own callback function to should()', () => {
  56. // Pass a function to should that can have any number
  57. // of explicit assertions within it.
  58. // The ".should(cb)" function will be retried
  59. // automatically until it passes all your explicit assertions or times out.
  60. cy.get('.assertions-p')
  61. .find('p')
  62. .should(($p) => {
  63. // https://on.cypress.io/$
  64. // return an array of texts from all of the p's
  65. // @ts-ignore TS6133 unused variable
  66. const texts = $p.map((i, el) => Cypress.$(el).text())
  67. // jquery map returns jquery object
  68. // and .get() convert this to simple array
  69. const paragraphs = texts.get()
  70. // array should have length of 3
  71. expect(paragraphs, 'has 3 paragraphs').to.have.length(3)
  72. // use second argument to expect(...) to provide clear
  73. // message with each assertion
  74. expect(paragraphs, 'has expected text in each paragraph').to.deep.eq([
  75. 'Some text from first p',
  76. 'More text from second p',
  77. 'And even more text from third p',
  78. ])
  79. })
  80. })
  81. it('finds element by class name regex', () => {
  82. cy.get('.docs-header')
  83. .find('div')
  84. // .should(cb) callback function will be retried
  85. .should(($div) => {
  86. expect($div).to.have.length(1)
  87. const className = $div[0].className
  88. expect(className).to.match(/heading-/)
  89. })
  90. // .then(cb) callback is not retried,
  91. // it either passes or fails
  92. .then(($div) => {
  93. expect($div, 'text content').to.have.text('Introduction')
  94. })
  95. })
  96. it('can throw any error', () => {
  97. cy.get('.docs-header')
  98. .find('div')
  99. .should(($div) => {
  100. if ($div.length !== 1) {
  101. // you can throw your own errors
  102. throw new Error('Did not find 1 element')
  103. }
  104. const className = $div[0].className
  105. if (!className.match(/heading-/)) {
  106. throw new Error(`Could not find class "heading-" in ${className}`)
  107. }
  108. })
  109. })
  110. it('matches unknown text between two elements', () => {
  111. /**
  112. * Text from the first element.
  113. * @type {string}
  114. */
  115. let text
  116. /**
  117. * Normalizes passed text,
  118. * useful before comparing text with spaces and different capitalization.
  119. * @param {string} s Text to normalize
  120. */
  121. const normalizeText = (s) => s.replace(/\s/g, '').toLowerCase()
  122. cy.get('.two-elements')
  123. .find('.first')
  124. .then(($first) => {
  125. // save text from the first element
  126. text = normalizeText($first.text())
  127. })
  128. cy.get('.two-elements')
  129. .find('.second')
  130. .should(($div) => {
  131. // we can massage text before comparing
  132. const secondText = normalizeText($div.text())
  133. expect(secondText, 'second text').to.equal(text)
  134. })
  135. })
  136. it('assert - assert shape of an object', () => {
  137. const person = {
  138. name: 'Joe',
  139. age: 20,
  140. }
  141. assert.isObject(person, 'value is object')
  142. })
  143. it('retries the should callback until assertions pass', () => {
  144. cy.get('#random-number')
  145. .should(($div) => {
  146. const n = parseFloat($div.text())
  147. expect(n).to.be.gte(1).and.be.lte(10)
  148. })
  149. })
  150. })
  151. })