7

I'm attempting to build an automated test with Nightwatch.js in order to verify that software download links are working correctly. I don't want to download the files, as they are quite large, I just want to verify that the corresponding link is returning a 200 HTTP response to make sure the links are pointing to the proper place.

Any idea for ways to test links to downloadable files with Nightwatch.js?

Here's what I currently have:

/**
 * Test Software Downloads
 * 
 * Verify that software downloads are working 
 */

module.exports = {
    "Download redirect links": function (browser) {

        // download links
        var downloadLinks = {
            "software-download-latest-mac": "http://downloads.company.com/mac/latest/",
            "software-download-latest-linux": "http://downloads.company.com/linux/latest/",
            "software-download-latest-win32": "http://downloads.company.com/windows/32/latest/",
            "software-download-latest-win64": "http://downloads.company.com/windows/64/latest/"
        };

        // loop through download links
        for (var key in downloadLinks) {
            if (downloadLinks.hasOwnProperty(key)) {

                // test each link's status
                browser
                    .url(downloadLinks[key]);
            }
        }

        // end testing
        browser.end();
    }
};
Kevinleary.net
  • 8,851
  • 3
  • 54
  • 46

1 Answers1

5
  1. Use the node http module and make a "HEAD" request
  2. For example: assert the filesize

test.js

var http = require("http");

module.exports = {
  "Is file avaliable" : function (client) {
    var request = http.request({
        host: "www.google.com",
        port: 80,
        path: "/images/srpr/logo11w.png",
        method: "HEAD"
    }, function (response) {
      client
        .assert.equal(response.headers["content-length"], 14022, 'Same file size');
      client.end();
    }).on("error", function (err) {
      console.log(err);
      client.end();
    }).end();
  }
};

References

Community
  • 1
  • 1
  • This solution should be perfectly fine in most situations, but what if the browser is driven by means of a remote webdriver server and/or the download link only works when various session cookie, IP address or network access requirements are met? It would be useful with a solution that uses the actual browser to check if the link works. (If the webdriver protocol supports it.) – Otto G Apr 23 '18 at 13:15
  • @OttoG you could do the same with [puppeteer](https://github.com/GoogleChrome/puppeteer) in headless mode – e382df99a7950919789725ceeec126 Apr 24 '18 at 17:05