I wanted to implement this as a page object so I could use a simple one line expect statement for each pages spec file. In afterthought I think this could have been achieved in a much simpler manner using an API testing framework such as cheerio.js but here is how to implement it using protractor and jasmine (using ES2015 syntax so update node to the current version) ! Please remember to install the request, bluebird and request-promise npm packages.
PageObject.js
crawlLinks(){
const request = require('request');
const Promise = require('bluebird');
const rp = require('request-promise');
return $$('a').then(function(elems){
return Promise.map(elems, elem => {
return elem.getAttribute("href").then(function(url){
if(url){
var options = {
method: 'GET',
uri: url,
resolveWithFullResponse: true
};
return rp(options).then(function(response){
console.log('The response code for ' + url + ' is ' + response.statusCode);
return response.statusCode === 200;
});
}
});
}).then((allCodes) => {
console.log(allCodes);
return Promise.resolve(allCodes);
});
});
}
Test
it("should not have broken links", function(){
expect(pageObject.crawlLinks()).not.toContain(false);
});