【问题标题】:Cypress - fake internet failure赛普拉斯 - 假互联网故障
【发布时间】:2019-09-18 05:02:51
【问题描述】:

我正在使用 Cypress 编写我的代码,并想测试互联网故障或服务器不返回任何响应的情况。我正在查看赛普拉斯文档,但没有发现任何帮助。我能找到的最接近的是

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

问题是我不能用 undefined 替换响应,因为 cypress 不允许。如果我删除它,赛普拉斯将停止嘲笑我的 API。有什么帮助吗?

【问题讨论】:

    标签: testing cypress


    【解决方案1】:

    您可以使用“重定向”。

    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);
    })
    })
    
    

    响应将是空字符串。看下面的截图,

    如果你使用

    response: {}
    

    它将返回空响应正文。

    【讨论】:

    • 这里的响应还是空字符串,不是undefined或者null。空响应应该是这个意思吗?
    • 我认为在这种情况下,它将返回正文中带有空字符串的响应。我也修改了空体的答案。如果您不想返回任何响应,则路由命令可能不是正确的方法。
    猜你喜欢
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2018-04-28
    相关资源
    最近更新 更多