• [Cypress] Combine Custom Cypress Commands into a Single Custom Command


    It’s pretty likely that we’ll need to login as a particular user for many tests, let’s take our cy.request command and turn it into a custom command to make it easier for the places we’ll be needing a logged in user.

    commands.js:

    Cypress.Commands.add('createUser', overrides => {
      const user = buildUser(overrides)
      cy.request({
        url: 'http://localhost:3000/register',
        method: 'POST',
        body: user,
      }).then(response => ({...response.body.user, ...user}))
    })
    
    Cypress.Commands.add('login', user => {
      return cy
        .request({
          url: 'http://localhost:3000/login',
          method: 'POST',
          body: user,
        })
        .then(response => {
          window.localStorage.setItem('token', response.body.user.token)
          return {...response.body.user, ...user}
        })
    })
    
    Cypress.Commands.add('loginAsNewUser', () => {
      cy.createUser().then(user => {
        cy.login(user)
      })
    })

    spec.js:

    describe('authenticated calculator', () => {
      it('displays the username', () => {
        cy.loginAsNewUser().then(user => {
          cy.visit('/')
            .findByTestId('username-display')
            .should('have.text', user.username)
            .findByText(/logout/i)
            .click()
            .findByTestId('username-display')
            .should('not.exist')
        })
      })
    })
  • 相关阅读:
    U1
    std::vector的内存释放
    (转)浅谈PostgreSQL的索引
    PostgreSQL的generate_series函数应用
    linux 里的`反引号
    wireshark抓取本地回环数据包
    linux shell中 if else以及大于、小于、等于逻辑表达式介绍
    c++利用互斥锁实现读写锁
    c++互斥锁的实现
    大小端,"字节序"
  • 原文地址:https://www.cnblogs.com/Answer1215/p/12404055.html
Copyright © 2020-2023  润新知