My goal is to count the number of hyperlinks on a page and check if they are functioning properly. While I can successfully count all the links and display the tally in JavaScript, I am facing difficulties returning that value in protractor via 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');
});
});
});
});
Moreover, I aim to "check" the links to verify their functionality. Is there a method to visit a URL and click on each link to open them in separate windows? Or retrieve an attribute indicating whether the link works or has a valid URL?
Initially, I used xpath to locate the id and manually clicked on the link while visually verifying as the test progressed. However, given the average page contains 15-20 links, I require a more automated approach.
Ernst's answer guided me towards the solution, although it required some restructuring and encapsulation of code: https://github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/LinkCheck