connectors.spec.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /// <reference types="cypress" />
  2. context('Connectors', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/commands/connectors')
  5. })
  6. it('.each() - iterate over an array of elements', () => {
  7. // https://on.cypress.io/each
  8. cy.get('.connectors-each-ul>li')
  9. .each(($el, index, $list) => {
  10. console.log($el, index, $list)
  11. })
  12. })
  13. it('.its() - get properties on the current subject', () => {
  14. // https://on.cypress.io/its
  15. cy.get('.connectors-its-ul>li')
  16. // calls the 'length' property yielding that value
  17. .its('length')
  18. .should('be.gt', 2)
  19. })
  20. it('.invoke() - invoke a function on the current subject', () => {
  21. // our div is hidden in our script.js
  22. // $('.connectors-div').hide()
  23. // https://on.cypress.io/invoke
  24. cy.get('.connectors-div').should('be.hidden')
  25. // call the jquery method 'show' on the 'div.container'
  26. .invoke('show')
  27. .should('be.visible')
  28. })
  29. it('.spread() - spread an array as individual args to callback function', () => {
  30. // https://on.cypress.io/spread
  31. const arr = ['foo', 'bar', 'baz']
  32. cy.wrap(arr).spread((foo, bar, baz) => {
  33. expect(foo).to.eq('foo')
  34. expect(bar).to.eq('bar')
  35. expect(baz).to.eq('baz')
  36. })
  37. })
  38. describe('.then()', () => {
  39. it('invokes a callback function with the current subject', () => {
  40. // https://on.cypress.io/then
  41. cy.get('.connectors-list > li')
  42. .then(($lis) => {
  43. expect($lis, '3 items').to.have.length(3)
  44. expect($lis.eq(0), 'first item').to.contain('Walk the dog')
  45. expect($lis.eq(1), 'second item').to.contain('Feed the cat')
  46. expect($lis.eq(2), 'third item').to.contain('Write JavaScript')
  47. })
  48. })
  49. it('yields the returned value to the next command', () => {
  50. cy.wrap(1)
  51. .then((num) => {
  52. expect(num).to.equal(1)
  53. return 2
  54. })
  55. .then((num) => {
  56. expect(num).to.equal(2)
  57. })
  58. })
  59. it('yields the original subject without return', () => {
  60. cy.wrap(1)
  61. .then((num) => {
  62. expect(num).to.equal(1)
  63. // note that nothing is returned from this callback
  64. })
  65. .then((num) => {
  66. // this callback receives the original unchanged value 1
  67. expect(num).to.equal(1)
  68. })
  69. })
  70. it('yields the value yielded by the last Cypress command inside', () => {
  71. cy.wrap(1)
  72. .then((num) => {
  73. expect(num).to.equal(1)
  74. // note how we run a Cypress command
  75. // the result yielded by this Cypress command
  76. // will be passed to the second ".then"
  77. cy.wrap(2)
  78. })
  79. .then((num) => {
  80. // this callback receives the value yielded by "cy.wrap(2)"
  81. expect(num).to.equal(2)
  82. })
  83. })
  84. })
  85. })