Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testcafe: How to error on 4xx or 5xx response?

I'm working on a legacy app that makes a lot of calls to external sources. I'm trying to refactor this app, and I've written testcafe tests to help inform me when I've made a mistake. I'm NOT running my tests with --skip-js-errors, but when I get 404 errors and the console prints this out:

enter image description here

the test doesn't stop. I'd like errors like these to be something I am informed about. How do I make 4xx and 5xx network responses fail testcafe? I'm using angular 1.2 if that matters. If I could, I would change all the remote calls to throw an exception on 4xx or 5xx, but this is legacy code I don't understand, and I'm sure doing that would break a feature.

like image 215
Daniel Kaplan Avatar asked Sep 20 '18 16:09

Daniel Kaplan


People also ask

What does 5xx server error mean?

5xx Status Codes. A 5xx code means the problem was caused by the server. With a 5xx code, the request can be present with no changes and you will get the requested result when the server has been fixed. With a 4xx code, typically the client or user has to fix an error before trying again, but there are some exceptions.

What is 4xx and 5xx?

In REST, both 4xx and 5xx types of codes denote an error. The main difference between the two is whose fault that error is. A 4xx code indicates an error caused by the user, whereas 5xx codes tell the client that they did everything correctly and it's the server itself who caused the problem.


1 Answers

I suggest you extend RequestLogger to check a request. You can throw an error based on the request status. For example:

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);                                                                                                                
    });                                                                                                                                      
});  
like image 74
Marion Avatar answered Oct 21 '22 06:10

Marion