Search code examples
node.jsseleniumselenium-webdriverintern

http.request in intern functional test


I am trying to post to a url to get a username and password so I can log into my website to do functional testing. I start up selenium server and run this basic test with a request to a posting service.

define([
'intern!object',
'runtime/testConfig',
'intern/dojo/node!nconf',
'intern/dojo/node!http'

], function(registerSuite, conf, nconf, http) {

var tests = {

    name: 'Login test',

    'Test': function() {

        return http.request({
                host: 'posttestserver.com',
                path: '/post.php',
                json: true,
                method: 'POST',
                body: {
                    "userName": "sgfi98j",
                    "password": "sgfi98j",
                    "userEmail": "[email protected]",
                    "sourceCode": "TEST",
                    "region": "US"
                },
                headers: {
                    'Content-Type': 'application/json'
                }
            }, function(response) {
            // Continuously update stream with data
            var body = '';
            console.log("getting data");
            response.on('data', function(d) {
                body += d;
                console.log(".");
            });
            response.on('end', function() {
                console.log("done");
                console.log(body);
            });
            response.on('error', function(e) {
                console.log("ERROR :( ");
                console.log(e.message);
            });
        });
    }
};

registerSuite(tests);
});

I have tried many verions of this and either get no error or

Warning: FATAL ERROR
Error: [POST http://localhost:4444/wd/hub/session] connect ECONNREFUSED
Error: connect ECONNREFUSED
  at exports._errnoException  <util.js:746:11>
  at TCPConnectWrap.afterConnect [as oncomplete]  <net.js:1000:19> Use --force     to continue.

Am I missing something in my selenium/intern config?


Solution

  • I never really sorted out the error using the http module because I did have selenium running and it was still giving me the error. But I tried using dojo/request because it returns promises and it worked right away.

    define([
    "intern!object",
    "runtime/testConfig",
    "intern/dojo/node!nconf",
    "intern/dojo/request",
    "intern/chai!expect"
    
    ], function(registerSuite, conf, nconf, request, expect) {
    
    var tests = {
    
        name: "Login test",
    
        "Test": function() {
            request.post("http://requestb.in/qlnoyyql", {
                data: JSON.stringify({
                    userName: "gz4nio4",
                    password: "sdfgsdfgsdgf4",
                    userEmail: "[email protected]",
                    billingCode: "abc-123",
                    sourceCode: "TEST",
                    companyName: "gwzanio4",
                    region: "US",
                    partner: "NONE"
                }),
                headers: { 'Content-Type': 'application/json' }
            }).then(function(response) {
                console.log(response);
            }, function(err) {
                console.log(err);
            }, function(evt) {
                console.log(evt);
            });
        }
    };
    
    registerSuite(tests);
    });