Dasman
Dasman

Reputation: 317

Protractor: httpGet is not defined; Need Response codes from Hyperlinks

Problem: I am able to count the number of links on a page.

I want to test if they are valid. While I could use .click -> this would validate a hyperlink thats a "404 Page Not Found" to Protractor.

I want to use error code or response codes. There are several SO answers on this, but for some reason none of them work all based around two errors:

Cant use the http get module

OR

Status Code is undefined. Any suggestions?

Spec.js:

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');
  });
});
    });
});

Conf.js:

exports.config = {
  seleniumAddress: 'http://localhost:4444/wd/hub',
  specs: ['spec.js']
};

Error:

1) Clicks on the correct Drupal hyperlink should find all links
  Message:
    Failed: httpGet is not defined

Upvotes: 0

Views: 694

Answers (1)

Dasman
Dasman

Reputation: 317

I was able to solve this with some SO help from other questions!

https://github.com/SDasman/Angular_Protractor_End2End_Tests/tree/master/LinkCheck

Upvotes: 0

Related Questions