0

How to check HTTPS Response Status for all links in a webpage using nightwatch?

 "HttpResponse": function (client) {
        var http = require("http");
        client.maximizeWindow()
            .url('https://www.google.co.in');
        client.elements('css selector', ' a', function (result) {
            console.log(result.value.length);

            for (var a=0;a<result.value.length;a++) {
                client.elementIdAttribute(result.value[a].ELEMENT, 'href', function (link) {
                    console.log('Link Text' + link.value);
                    var request = http.request({
                        host: "www.google.co.in",
                        port: 80,
                        path: link.value.split('google.co.in')[1],
                        method: "HEAD"
                    }, function (response) {
                        client
                            .assert.equal(response.statusCode, 200, 'Check status');
                        client.end();
                    }).on("error", function (err) {
                        console.log(err);
                        client.end();
                    }).end();
                })
            }
        });

This Does not work for me.

I get the below Error :

Failed [equal]: (Check status)  - expected "200" but got: "302"
    at ClientRequest.<anonymous> 
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:565:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:116:23)
    at Socket.socketOnData (_http_client.js:454:20)
    at addChunk (_stream_readable.js:252:12)
    at readableAddChunk (_stream_readable.js:239:11)
    at Socket.Readable.push (_stream_readable.js:197:10)
    at TCP.onread (net.js:589:20)

expected is 200 but it throws 302

Sailesh Botcha
  • 185
  • 1
  • 3
  • 10
  • Google might be redirecting you because of your physical location, to your localized (country-specific) Google index. – Milan Velebit Nov 06 '17 at 09:00
  • some href assertion passes with 200 status code for some it fails with 302 status do i need to do any modification or add any more request parameters ?? – Sailesh Botcha Nov 06 '17 at 16:16
  • Do those that pass (with 200) route via google too? – Milan Velebit Nov 06 '17 at 16:50
  • not sure @MilanVelebit but when i give console log link.value , it would display the entire url .. my requirement is i have some list of link in web page instead of clicking on each link seperately I wanted to capture all the links href attribute and check the HTTPS Reponse Status for each of it in nightwatch js . is it possible to check please help me on this – Sailesh Botcha Nov 08 '17 at 11:46

0 Answers0