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