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

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

Dasman
1#
Dasman Published in 2017-12-07 20:02:36Z

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
Dasman
2#
Dasman Reply to 2017-12-12 20:31:57Z

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

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

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.298603 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO