jperelli jperelli - 4 months ago 33
AngularJS Question

Protractor: Is it possible to test that there are no 404 in my app?

I'm new to protractor, I want to write a test to see that there are no anchors with urls giving 404 errors.

I've seen this How to test html links with protractor?, but is for one determined link only, I want to do it for all links in the page.

The test should pass for the http status 200, as stated here How to use protractor to get the response status code and response text?

I have two questions:


  • Does this test makes sense in protractor?

  • Is it possible to test this? If so, how?


Answer

I think its definitely doable and would make sense to do it if the scope is limited since this is not a typical UI test that selenium-webdriver is used for. You could do something like, find all links, get underneath url and fire a GET request using a module like request. Here is a pseudo code.

var request = require('request');
var assert = require('assert');
element.all(by.tagName('a')).then(function(link) {
   var url = link.getAttribute('href');
   if(url) {
       request(url, function (error, response, body) {
           assert.equal(response.statusCode, 200, 'Status code is not OK');
       });
    }
});
Comments