testing - Cypress - 虚假的互联网故障

标签 testing cypress

我正在使用 Cypress 编写我的代码,并希望测试 Internet 故障或服务器未返回任何响应的情况。我正在查看 Cypress 文档,但没有发现任何有用的信息。我能找到的最接近的东西是

cy.route({
    method: 'POST',
    url: 'my_url',
    response: {},
})
.as('validate');

问题是我无法将响应替换为未定义,因为 cypress 不允许这样做。如果我删除它,Cypress 将停止模拟我的 API。有帮助吗?

最佳答案

您可以使用“重定向”。

https://docs.cypress.io/api/commands/route.html#Simulate-a-server-redirect

describe('response will be empty', function() 
{
it('Open web page',()=> {
    let url = 'https://www.google.com/maps';
    cy.server();
    cy.route(
    {
        method: 'GET',
        url: '**/maps/**',
        //you can replace the status code you like 
        status: 503,
        response: {
            redirect: ''
        }
    }
    );
    cy.visit(url);
})
})

响应将为空字符串。看下面的截图, enter image description here

如果你使用

response: {}

它将返回空的响应体。

关于testing - Cypress - 虚假的互联网故障,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57985381/

相关文章:

javascript - 如何模拟在 Cypress.io 中丢失的窗口焦点

html - Cypress:如何验证生成的复选框已选中并且具有属性 aria-checked ="true"

ruby-on-rails-3 - 你如何在 Rails 3 上使用 RR(双 ruby )模拟一个带有 block 的方法

testing - MSW 请求处理程序在编剧测试中不起作用

ruby - 在 RSpec 中自动创建测试 ID

azure - 为什么 Cypress 和 Azure DevOps 的测试自动化仅取得部分成功?

node.js - 使用 Node 生成唯一的测试数据

unit-testing - 从不同的可执行文件收集单元测试输出的框架?

parallel-processing - 如何在单机上使用 cypress 并行运行具有不同数据集的单个测试

javascript - 当父元素具有 CSS 属性 : display: none 时, Cypress 如何断言元素可见