针对localhost测试时,赛普拉斯API请求中止

问题描述

请考虑以下用于注册工作流程的测试(我的实际代码的简化版本)。

提交表单时,会向POST / register /发出API请求。当针对实时URL(通过cypress.json baseUrl配置)运行cypress时,此请求可以正常工作。但是,对本地主机URL运行cypress时,此请求将失败,因为“终止”。

(如果我通过本地主机手动使用该应用程序,则可以注册而没有任何错误

// Visit the homepage
cy.visit('/');
cy.location('pathname').should('eq','/');

// Click the link to register
cy.get('#register').click();
cy.location('pathname').should('eq','/register/');

// Fill out the form
cy.get('#name').type('foo bar');
cy.get('#email').type('foo.bar@gmail.com');
cy.get('#password').type('qwerty123!');

// Submit the form
cy.get('#submit').click();  // <-- API REQUEST IS ABORTED

// A successful registration should redirect us to the homepage
cy.location('pathname').should('eq','/');

关于在localhost上运行cypress,是否有明显的缺失?

解决方法

您是否还运行后端(处理POST /register请求的应用程序)?


当您针对实时网址运行cypress时,api请求将转到“生产”后端(在大多数情况下)并开始运行。