http.request in intern functional test

480 views Asked by At

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?

2

There are 2 answers

0
mziemer On BEST ANSWER

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);
});
1
C Snover On
  1. The ECONNREFUSED error indicates there is no Selenium server running at the specified target.

  2. Your code here will never work because http.request does not return a Promise, it returns a ClientRequest object. If you are performing an asynchronous operation you need to return a Promise or use this.async. See the asynchronous test documentation which describes both of these options in greater detail.