Search code examples
node.jspostmannewmanpostman-collection-runner

Added cert and key files in newman js file but still i could see error messages


I would need to add certificate and key inside the newman's .js file. Please advise.

Below is my sample (modified) code,

var https = require('https');
var fs = require('fs');
const newman = require('newman');
    newman.run({
        collection: require("/Users/cloud/Documents/sample.json"), // can also provide a URL or path to a local JSON file.
        environment: require("/Users/cloud/Documents/env.json"),
        insecure: true,
        sslClientCert: fs.readFileSync("/Users/cloud/Documents/certs/test.crt"),
        sslClientKey: fs.readFileSync("/Users/cloud/Documents/certs/test.key"),
        reporters: 'htmlextra',
        reporter: {
            htmlextra: {
                export: '/Users/cloud/Documents/report', // If not specified, the file will be written to `newman/` in the current working directory.
                darkTheme: true, // optional, tells the reporter to use the `Dark Theme` template
                title: 'My new report title'
            }
        }
    }, function (err) {
        if (err) {throw err;}
        console.log('collection run 8 complete!');
    });

I have added the cert and key file and i still get SSL handshake error. But when i run in newman using below command it works. I am not able to pass the client key and client cert in js file

newman run "collectio.json" -e "env.json" --insecure --ssl-client-key <keypath> --ssl-client-cert <cert path>

Below is the error message i see in html report, but it works well in newman CLI,

Assertion: 

write EPROTO 4514592192:error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:../deps/openssl/openssl/ssl/record/rec_layer_s3.c:1536:SSL alert number 40

Unexpected token u in JSON at position 0

In my collection snippet i have used,

var jsonData = JSON.parse(responseBody);
console.log(jsonData);
pm.globals.set("JWT", jsonData.access_token);

Thanks in advance.


Solution

  • You need to use the following options (sslClientCert and sslClientKey) within the Newman run object:

    newman.run({
            collection: require("/Users/cloud/Documents/sample.json"), // can also provide a URL or path to a local JSON file.
            environment: require("/Users/cloud/Documents/env.json"),
            insecure: true,
            sslClientCert: fs.readFileSync("/Users/cloud/Documents/certs/test.crt"),
            sslClientKey: fs.readFileSync("/Users/cloud/Documents/certs/test.key"),
            reporters: 'htmlextra',
            reporter: {
                htmlextra: {
                    export: '/Users/cloud/Documents/report', // If not specified, the file will be written to `newman/` in the current working directory.
                    darkTheme: true, // optional, tells the reporter to use the `Dark Theme` template
                    title: 'My new report title'
                }
            }
        }, function (err) {
            if (err) {throw err;}
            console.log('collection run 8 complete!');
        });