Testcafe:如何在4xx或5xx响应上出错?

时间:2018-09-20 16:47:55

标签: angularjs node.js automated-tests testcafe

我正在开发一个旧版应用程序,该应用程序需要大量调用外部资源。我正在尝试重构该应用,并且我编写了testcafe测试以帮助我在犯错时通知我。我没有使用--skip-js-errors运行测试,但是当我收到404错误并且控制台将其打印出来时:

enter image description here

测试不会停止。我希望将此类错误告知我。 如何使4xx和5xx网络响应的testcafe失败?如果这很重要,我将使用angular 1.2。如果可以,我将更改所有远程调用以在4xx或5xx上引发异常,但这是我不了解的旧代码,我敢肯定这样做会破坏功能。

1 个答案:

答案 0 :(得分:4)

我建议您扩展RequestLogger来检查请求。您可以根据请求状态抛出错误。例如:

import EventEmitter from 'events';                                                                                                           
import { RequestHook } from 'testcafe';                                                                                                      

fixture `test`                                                                                                                               
    .page('https://testcafe.devexpress.com/Details2/')                                                                                       

class FailedRequestsLogger extends RequestHook {                                                                                             
    constructor (...args) {                                                                                                                  
        super(...args);                                                                                                                      

        this.events = new EventEmitter();                                                                                                    
        this.failedRequestPromise = new Promise(resolve => this.events.once('failed-request', resolve));                                     
    }                                                                                                                                        

    onRequest (request) {                                                                                                                    
    }                                                                                                                                        

    onResponse (response) {                                                                                                                  
        if (response.statusCode >= 400)                                                                                                      
            this.events.emit('failed-request', response.statusCode);                                                                         
    }                                                                                                                                        

    waitForFailedRequest (action) {                                                                                                          
        return Promise.race([                                                                                                                
                action(),                                                                                                                    
                this.failedRequestPromise.then(statusCode => Promise.reject(new Error(`Request failed with the ${statusCode} status code`))) 
        ])                                                                                                                                   
    }                                                                                                                                        
}                                                                                                                                            

const logger = new FailedRequestsLogger();                                                                                                   

test.requestHooks(logger)('test', async t => {                                                                                               
    await logger.waitForFailedRequest(async () => {                                                                                          
        await t.click('body');                                                                                                              
        await t.wait(10000);                                                                                                                
    });                                                                                                                                      
});