zoukankan      html  css  js  c++  java
  • [Cypress] Stub a Post Request for Successful Form Submission with Cypress

    In this lesson well stub a POST request and use Cypress commands to fill in and submit a form. We’ll wait for the submission to resolve and then assert that the new item was added to the list.

    For example when we dealing with Form submition, we want to issue a new POST request and then check it should update number of todos.

        it.only('should post new todo to the backend', function () {
            // Serve the page
            cy.server();
    
            // Prepare a POST request
            cy.route({
                method: 'POST',
                url: '/api/todos',
                response: {id: 123, name: 'new todo', isComplete: false}
            }).as('save');
    
            // Call a custom command to load initial todos
            cy.seedAndVisit();
    
            // Enter a new todo
            cy.get('.new-todo')
                .type('new todo')
                .type('{enter}');
    
            // Wait network request to be finished
            cy.wait('@save');
    
            // Calculate the expected length of todos
            cy.get('.todo-list li')
                .should('have.length', 5);
        });
    
    // command
    Cypress.Commands.add('seedAndVisit', (seedData = 'fixture:todos') => {
      cy.server()
      cy.route('GET', '/api/todos', seedData).as('load')
    
      cy.visit('/')
    
      cy.wait('@load')
    });
  • 相关阅读:
    75
    74
    接口理论知识
    软件测试计划的编写
    软件测试的生命周期&软件测试工作流程
    软件测试分类体系系统学习
    Mysql之高级查询
    数据库的DML操作
    Mysql之数据完整性约束
    Mysql之DDL操作
  • 原文地址:https://www.cnblogs.com/Answer1215/p/9094594.html
Copyright © 2011-2022 走看看