I am trying to count the number of hyperlinks on a page and test them to see if they work. I have the ability to count all the links and display that count in JavaScript but I can't seem to return that value in protractor on Command Line.
Update Code based on Answers:
browser.waitForAngularEnabled(false);
describe('Clicks on the correct Drupal hyperlink', function() {
it('should find all links', function () {
browser.get('file:///C:/Users/Dasman/Documents/PROTRACTOR_E2E_TESTING/TestSite.html');
let allLinks = element.all(by.tagName('a'));
allLinks.count().then(function(link_tally){
console.log('There are a total of ' + link_tally + " links on this page with proper tags.")
})
browser.sleep(2000);
// A Protracterized httpGet() promise
function httpGet(siteUrl) {
var http = require('http');
var defer = protractor.promise.defer();
http.get(siteUrl, function(response) {
var bodyString = '';
response.setEncoding('utf8');
response.on("data", function(chunk) {
bodyString += chunk;
});
response.on('end', function() {
defer.fulfill({
statusCode: response.statusCode,
bodyString: bodyString
});
});
}).on('error', function(e) {
defer.reject("Got http.get error: " + e.message);
});
return defer.promise;
}
it('should return 200 and contain proper body', function() {
httpGet(allLinks).then(function(result) {
allLinks.count().then(function(statusCode){
console.log('Status code is: ' + statusCode)
})
expect(result.statusCode).toBe(200);
expect(result.bodyString).toContain('Apache');
});
});
});
});
In addition I want to "check" the links to see if they open. Is there a way to go to a URL and click all the links and have them open on separate windows? or get an attribute if the link works or has a valid URL?
I originally xpath the id and clicked the link and as the test ran - I visually verified. But with 15-20 links on a average page - I need a more automated way.
Ernst's answer pointed me to the solution, but it did need some code restructuring and encapsulation: https://github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/LinkCheck