<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2

      Use Cypress to test user registration

      Let’s write a test to fill out our registration form. Because we’ll be running this against a live backend, we need to generate the user’s information to avoid re-runs from trying to create new users that already exist. There are trade-offs with this approach. You should probably also clean out the application database before all of your tests start (how you accomplish this is pretty application-specific). Also, if your application requires email confirmation, I recommend you mock that on the backend and automatically set the user as confirmed during tests.

       

      Let's create a helper method first.

      support/generate.js

      import {build, fake} from 'test-data-bot'
      
      const userBuilder = build('User').fields(
          {
              username: fake(f => f.internet.userName()),
              password: fake(f => f.internet.password())
          }
      )
      
      export {userBuilder}

       

      Then, create tests:

      e2e/register.js

      import {userBuilder} from '../support/generate'
      
      describe('should register a new user', () => {
          it('should register a new user', () => {
              const user = userBuilder();
              cy.visit('/')
                  .getByText(/register/i)
                  .click()
                  .getByLabelText(/username/i)
                  .type(user.username)
                  .getByLabelText(/password/i)
                  .type(user.password)
                  .getByText(/submit/i)
                  .click()
                  .url()
                  .should('eq', `${Cypress.config().baseUrl}/`)
                  .window()
                  .its('localStorage.token')
                  .should('be.a', 'string')
          });
      });

       

      Cypress Driven Development

      Because Cypress allows you to use all the developer tools you’re used to from Google Chrome, you can actually use Cypress as your main application development workflow. If you’ve ever tried to develop a feature that required you to be in a certain state you’ve probably felt the pain of repeatedly refreshing the page and clicking around to get into that state. Instead, you can use cypress to do that and developer your application entirely in Cypress.

       

      Simulate HTTP Errors in Cypress Tests

      Normally I prefer to test error states using integration or unit tests, but there are some situations where it can be really useful to mock out a response to test a specific scenario in an E2E test. Let’s use the cypress server and route commands to mock a response from our registration request to test the error state.

          it(`should show an error message if there's an error registering`, () => {
              cy.server()
              cy.route({
                method: 'POST',
                url: 'http://localhost:3000/register',
                status: 500,
                response: {},
              })
              cy.visit('/register')
                .getByText(/submit/i)
                .click()
                .getByText(/error.*try again/i)
            })

       

      Test user login with Cypress

       To test user login we need to have a user to login with. We could seed the database with a user and that may be the right choice for your application. In our case though we’ll just go through the registration process again and then login as the user and make the same assertions we made for registration.

      import {userBuilder} from '../support/generate'
      
      describe('should register a new user', () => {
          it('should register a new user', () => {
              const user = userBuilder();
              cy.visit('/')
                  .getByText(/register/i)
                  .click()
                  .getByLabelText(/username/i)
                  .type(user.username)
                  .getByLabelText(/password/i)
                  .type(user.password)
                  .getByText(/submit/i)
                  .click()
      
                  // now we have new user
                  .getByText(/logout/i)
                  .click()
      
                  // login again
                  .getByText(/login/i)
                  .click()
                  .getByLabelText(/username/i)
                  .type(user.username)
                  .getByLabelText(/password/i)
                  .type(user.password)
                  .getByText(/submit/i)
                  .click()
      
                  // verify the user in localStorage
                  .url()
                  .should('eq', `${Cypress.config().baseUrl}/`)
                  .window()
                  .its('localStorage.token')
                  .should('be.a', 'string')
                  .getByTestId('username-display', {timeout: 500})
                  .should('have.text', user.username)
          });
      });

       

      Create a user with cy.request from Cypress

      We’re duplicating a lot of logic between our registration and login tests and not getting any additional confidence, so lets reduce the duplicate logic and time in our tests using cy.request to get a user registered rather than clicking through the application to register a new user.

      import {userBuilder} from '../support/generate'
      
      describe('should register a new user', () => {
          it('should register a new user', () => {
              const user = userBuilder();
              // send a http request to server to create a new user
              cy.request({
                  url: 'http://localhost:3000/register',
                  method: 'POST',
                  body: user
              })
              cy.visit('/')
                  .getByText(/login/i)
                  .click()
                  .getByLabelText(/username/i)
                  .type(user.username)
                  .getByLabelText(/password/i)
                  .type(user.password)
                  .getByText(/submit/i)
                  .click()
      
                  // verify the user in localStorage
                  .url()
                  .should('eq', `${Cypress.config().baseUrl}/`)
                  .window()
                  .its('localStorage.token')
                  .should('be.a', 'string')
                  .getByTestId('username-display', {timeout: 500})
                  .should('have.text', user.username)
          });
      });

       

      Keep tests isolated and focused with custom Cypress commands

      We’re going to need a newly created user for several tests so let’s move our cy.request command to register a new user into a custom Cypress command so we can use that wherever we need a new user.

       

      Because we need to create user very often in the test, it is good to create a command to simply the code:

      //support/commands.js
      
      
      import {userBuilder} from '../support/generate'
      
      Cypress.Commands.add('createUser', (overrides) => {
          const user = userBuilder(overrides);
          // send a http request to server to create a new user
          cy.request({
              url: 'http://localhost:3000/register',
              method: 'POST',
              body: user
          }).then(response => response.body.user)
      })

      We chain .then() call is to get the created user and pass down to the test.

       

      describe('should register a new user', () => {
          it('should register a new user', () => {
              cy.createUser().then(user => {
                  cy.visit('/')
                  .getByText(/login/i)
                  .click()
                  .getByLabelText(/username/i)
                  .type(user.username)
                  .getByLabelText(/password/i)
                  .type(user.password)
                  .getByText(/submit/i)
                  .click()
      
                  // verify the user in localStorage
                  .url()
                  .should('eq', `${Cypress.config().baseUrl}/`)
                  .window()
                  .its('localStorage.token')
                  .should('be.a', 'string')
                  .getByTestId('username-display', {timeout: 500})
                  .should('have.text', user.username)
              })
          });
      });

       

      posted @ 2018-12-05 15:54  Zhentiw  閱讀(361)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 无码国内精品久久人妻蜜桃| 久热久精久品这里在线观看| 67194熟妇人妻欧美日韩| 香蕉久久夜色精品国产成人| 人妻丝袜无码专区视频网站| 人人人澡人人肉久久精品| 国产婷婷综合在线视频| 精品午夜福利短视频一区| 社旗县| 日韩av在线一卡二卡三卡| 无码人妻aⅴ一区二区三区69岛| 男女做爰真人视频直播| 国产综合亚洲区在线观看| 亚洲综合久久精品国产高清| 精品卡通动漫亚洲AV第一页| 少妇xxxxx性开放| 亚洲精品自产拍在线观看动漫| 亚洲色大成永久WW网站| 泽州县| 中文成人无字幕乱码精品区| 国产拍拍拍无码视频免费| 国产亚洲精品AA片在线爽| 国产激情一区二区三区在线| 精品国产成人a在线观看| 国产精品黄色片| 中文国产人精品久久蜜桃| 熟妇人妻系列aⅴ无码专区友真希 亚洲精品喷潮一区二区三区 | 8av国产精品爽爽ⅴa在线观看| 国产精品中文字幕一区| 男男车车的车车网站w98免费| 日韩一区二区三区日韩精品| 2021国产精品视频网站| 无码人妻久久久一区二区三区| 国产精品色内内在线播放| 天堂V亚洲国产V第一次| 晋宁县| 欧美日韩一线| 欧美日韩精品久久久免费观看| 久久人与动人物a级毛片| 一本加勒比hezyo无码人妻| 国产精品大全中文字幕|