querying.spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /// <reference types="cypress" />
  2. context('Querying', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/commands/querying')
  5. })
  6. // The most commonly used query is 'cy.get()', you can
  7. // think of this like the '$' in jQuery
  8. it('cy.get() - query DOM elements', () => {
  9. // https://on.cypress.io/get
  10. cy.get('#query-btn').should('contain', 'Button')
  11. cy.get('.query-btn').should('contain', 'Button')
  12. cy.get('#querying .well>button:first').should('contain', 'Button')
  13. // ↲
  14. // Use CSS selectors just like jQuery
  15. cy.get('[data-test-id="test-example"]').should('have.class', 'example')
  16. // 'cy.get()' yields jQuery object, you can get its attribute
  17. // by invoking `.attr()` method
  18. cy.get('[data-test-id="test-example"]')
  19. .invoke('attr', 'data-test-id')
  20. .should('equal', 'test-example')
  21. // or you can get element's CSS property
  22. cy.get('[data-test-id="test-example"]')
  23. .invoke('css', 'position')
  24. .should('equal', 'static')
  25. // or use assertions directly during 'cy.get()'
  26. // https://on.cypress.io/assertions
  27. cy.get('[data-test-id="test-example"]')
  28. .should('have.attr', 'data-test-id', 'test-example')
  29. .and('have.css', 'position', 'static')
  30. })
  31. it('cy.contains() - query DOM elements with matching content', () => {
  32. // https://on.cypress.io/contains
  33. cy.get('.query-list')
  34. .contains('bananas')
  35. .should('have.class', 'third')
  36. // we can pass a regexp to `.contains()`
  37. cy.get('.query-list')
  38. .contains(/^b\w+/)
  39. .should('have.class', 'third')
  40. cy.get('.query-list')
  41. .contains('apples')
  42. .should('have.class', 'first')
  43. // passing a selector to contains will
  44. // yield the selector containing the text
  45. cy.get('#querying')
  46. .contains('ul', 'oranges')
  47. .should('have.class', 'query-list')
  48. cy.get('.query-button')
  49. .contains('Save Form')
  50. .should('have.class', 'btn')
  51. })
  52. it('.within() - query DOM elements within a specific element', () => {
  53. // https://on.cypress.io/within
  54. cy.get('.query-form').within(() => {
  55. cy.get('input:first').should('have.attr', 'placeholder', 'Email')
  56. cy.get('input:last').should('have.attr', 'placeholder', 'Password')
  57. })
  58. })
  59. it('cy.root() - query the root DOM element', () => {
  60. // https://on.cypress.io/root
  61. // By default, root is the document
  62. cy.root().should('match', 'html')
  63. cy.get('.query-ul').within(() => {
  64. // In this within, the root is now the ul DOM element
  65. cy.root().should('have.class', 'query-ul')
  66. })
  67. })
  68. it('best practices - selecting elements', () => {
  69. // https://on.cypress.io/best-practices#Selecting-Elements
  70. cy.get('[data-cy=best-practices-selecting-elements]').within(() => {
  71. // Worst - too generic, no context
  72. cy.get('button').click()
  73. // Bad. Coupled to styling. Highly subject to change.
  74. cy.get('.btn.btn-large').click()
  75. // Average. Coupled to the `name` attribute which has HTML semantics.
  76. cy.get('[name=submission]').click()
  77. // Better. But still coupled to styling or JS event listeners.
  78. cy.get('#main').click()
  79. // Slightly better. Uses an ID but also ensures the element
  80. // has an ARIA role attribute
  81. cy.get('#main[role=button]').click()
  82. // Much better. But still coupled to text content that may change.
  83. cy.contains('Submit').click()
  84. // Best. Insulated from all changes.
  85. cy.get('[data-cy=submit]').click()
  86. })
  87. })
  88. })