[Cypress] Stub a Post Request for Successful Form Submission with Cypress
2024-09-08 01:53:18
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')
});
最新文章
- TestNG中的数据源DataProvider概述
- Jmeter调试工具---Debug Sampler
- Weblogic 启动报错:java.lang.NoClassDefFoundError
- chat
- ubuntu下php开发环境搭建,nginx+(cgi)php5fpm+memcached+xdebug
- 使用Preference保存设置
- POJ 3045 Cow Acrobats (贪心)
- NetCore1.1+Linux部署初体验
- javascript 推箱子游戏介绍及问题
- Core文件简单介绍及生成设置方法
- Python——爬取人口迁徙数据(以腾讯迁徙为例)
- 《SSO CAS单点系列》之 APP原生应用如何访问CAS认证中心
- 九、.net core用orm继承DbContext(数据库上下文)方式操作数据库
- 如何查看.net framework 版本
- Java NIO中的通道Channel(二)分散/聚集 Scatter/Gather
- Git每次进入都需要输入用户名和密码的问题解决
- Mac 流程图
- Java设计模式(10)代理模式(Proxy模式)
- 【原创】Junit4详解二:Junit4 Runner以及test case执行顺序和源代码理解
- HTML/CSS权值继承
热门文章
- strupr函数
- Mac使用bootcamp安装win8.1出现网卡驱动没有安装问题
- [转]在 Linux 下使用 RAID
- java环境搭建(及安装问题“No repository found containing”解决) 并创立第一个java程序
- css每次的初始化代码
- Android 基础知识图谱
- 编写高质量的js之恰当选用if和switch
- Java程序员2016年终总结
- Android控件的继承关系
- lldb e、@weakify(self) 网络请求400错误