todo.spec.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /// <reference types="cypress" />
  2. // Welcome to Cypress!
  3. //
  4. // This spec file contains a variety of sample tests
  5. // for a todo list app that are designed to demonstrate
  6. // the power of writing tests in Cypress.
  7. //
  8. // To learn more about how Cypress works and
  9. // what makes it such an awesome testing tool,
  10. // please read our getting started guide:
  11. // https://on.cypress.io/introduction-to-cypress
  12. describe('example to-do app', () => {
  13. beforeEach(() => {
  14. // Cypress starts out with a blank slate for each test
  15. // so we must tell it to visit our website with the `cy.visit()` command.
  16. // Since we want to visit the same URL at the start of all our tests,
  17. // we include it in our beforeEach function so that it runs before each test
  18. cy.visit('https://example.cypress.io/todo')
  19. })
  20. it('displays two todo items by default', () => {
  21. // We use the `cy.get()` command to get all elements that match the selector.
  22. // Then, we use `should` to assert that there are two matched items,
  23. // which are the two default items.
  24. cy.get('.todo-list li').should('have.length', 2)
  25. // We can go even further and check that the default todos each contain
  26. // the correct text. We use the `first` and `last` functions
  27. // to get just the first and last matched elements individually,
  28. // and then perform an assertion with `should`.
  29. cy.get('.todo-list li').first().should('have.text', 'Pay electric bill')
  30. cy.get('.todo-list li').last().should('have.text', 'Walk the dog')
  31. })
  32. it('can add new todo items', () => {
  33. // We'll store our item text in a variable so we can reuse it
  34. const newItem = 'Feed the cat'
  35. // Let's get the input element and use the `type` command to
  36. // input our new list item. After typing the content of our item,
  37. // we need to type the enter key as well in order to submit the input.
  38. // This input has a data-test attribute so we'll use that to select the
  39. // element in accordance with best practices:
  40. // https://on.cypress.io/selecting-elements
  41. cy.get('[data-test=new-todo]').type(`${newItem}{enter}`)
  42. // Now that we've typed our new item, let's check that it actually was added to the list.
  43. // Since it's the newest item, it should exist as the last element in the list.
  44. // In addition, with the two default items, we should have a total of 3 elements in the list.
  45. // Since assertions yield the element that was asserted on,
  46. // we can chain both of these assertions together into a single statement.
  47. cy.get('.todo-list li')
  48. .should('have.length', 3)
  49. .last()
  50. .should('have.text', newItem)
  51. })
  52. it('can check off an item as completed', () => {
  53. // In addition to using the `get` command to get an element by selector,
  54. // we can also use the `contains` command to get an element by its contents.
  55. // However, this will yield the <label>, which is lowest-level element that contains the text.
  56. // In order to check the item, we'll find the <input> element for this <label>
  57. // by traversing up the dom to the parent element. From there, we can `find`
  58. // the child checkbox <input> element and use the `check` command to check it.
  59. cy.contains('Pay electric bill')
  60. .parent()
  61. .find('input[type=checkbox]')
  62. .check()
  63. // Now that we've checked the button, we can go ahead and make sure
  64. // that the list element is now marked as completed.
  65. // Again we'll use `contains` to find the <label> element and then use the `parents` command
  66. // to traverse multiple levels up the dom until we find the corresponding <li> element.
  67. // Once we get that element, we can assert that it has the completed class.
  68. cy.contains('Pay electric bill')
  69. .parents('li')
  70. .should('have.class', 'completed')
  71. })
  72. context('with a checked task', () => {
  73. beforeEach(() => {
  74. // We'll take the command we used above to check off an element
  75. // Since we want to perform multiple tests that start with checking
  76. // one element, we put it in the beforeEach hook
  77. // so that it runs at the start of every test.
  78. cy.contains('Pay electric bill')
  79. .parent()
  80. .find('input[type=checkbox]')
  81. .check()
  82. })
  83. it('can filter for uncompleted tasks', () => {
  84. // We'll click on the "active" button in order to
  85. // display only incomplete items
  86. cy.contains('Active').click()
  87. // After filtering, we can assert that there is only the one
  88. // incomplete item in the list.
  89. cy.get('.todo-list li')
  90. .should('have.length', 1)
  91. .first()
  92. .should('have.text', 'Walk the dog')
  93. // For good measure, let's also assert that the task we checked off
  94. // does not exist on the page.
  95. cy.contains('Pay electric bill').should('not.exist')
  96. })
  97. it('can filter for completed tasks', () => {
  98. // We can perform similar steps as the test above to ensure
  99. // that only completed tasks are shown
  100. cy.contains('Completed').click()
  101. cy.get('.todo-list li')
  102. .should('have.length', 1)
  103. .first()
  104. .should('have.text', 'Pay electric bill')
  105. cy.contains('Walk the dog').should('not.exist')
  106. })
  107. it('can delete all completed tasks', () => {
  108. // First, let's click the "Clear completed" button
  109. // `contains` is actually serving two purposes here.
  110. // First, it's ensuring that the button exists within the dom.
  111. // This button only appears when at least one task is checked
  112. // so this command is implicitly verifying that it does exist.
  113. // Second, it selects the button so we can click it.
  114. cy.contains('Clear completed').click()
  115. // Then we can make sure that there is only one element
  116. // in the list and our element does not exist
  117. cy.get('.todo-list li')
  118. .should('have.length', 1)
  119. .should('not.have.text', 'Pay electric bill')
  120. // Finally, make sure that the clear button no longer exists.
  121. cy.contains('Clear completed').should('not.exist')
  122. })
  123. })
  124. })