aliasing.spec.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /// <reference types="cypress" />
  2. context('Aliasing', () => {
  3. beforeEach(() => {
  4. cy.visit('https://example.cypress.io/commands/aliasing')
  5. })
  6. it('.as() - alias a DOM element for later use', () => {
  7. // https://on.cypress.io/as
  8. // Alias a DOM element for use later
  9. // We don't have to traverse to the element
  10. // later in our code, we reference it with @
  11. cy.get('.as-table').find('tbody>tr')
  12. .first().find('td').first()
  13. .find('button').as('firstBtn')
  14. // when we reference the alias, we place an
  15. // @ in front of its name
  16. cy.get('@firstBtn').click()
  17. cy.get('@firstBtn')
  18. .should('have.class', 'btn-success')
  19. .and('contain', 'Changed')
  20. })
  21. it('.as() - alias a route for later use', () => {
  22. // Alias the route to wait for its response
  23. cy.intercept('GET', '**/comments/*').as('getComment')
  24. // we have code that gets a comment when
  25. // the button is clicked in scripts.js
  26. cy.get('.network-btn').click()
  27. // https://on.cypress.io/wait
  28. cy.wait('@getComment').its('response.statusCode').should('eq', 200)
  29. })
  30. })