Search code examples
javascriptnode.jsjasminerestlerjasmine-node

callbacks in jasmine E2E testing using restler


I am testing restful api's using restler and jasmine nodejs modules . I have created test files to test a single api call and test group of api calls that need input from each other . How can I provide the feedback / output from one such api call to another api call using proper jasmine describe - it - expect block format . If I try put verifyOtp function in another it-block after getPhoneOtp then it don't execute after success of getting the OTP and thus fails . Right now I am reading the outputs in the console only and not able to use jasmine expect function since I am not able to include verifyOtp function in a it-block . Any suggestions would be appreciated . Here is the code .

var restler = require('restler');
var fs = require('fs');
var colors = require('colors');
var util = require('util');
var config = require('./config.js') ;
var baseUrl = config.baseUrl ;

describe("LEC phone api's tests", function () {

    var _token;
    var phone = Math.round(Math.random() * 1000000000);
    var otp;

    console.log("Test started on "+baseUrl) ;



    function verifyOtp(otpPassed) {

        var success = 0 ;
            restler.post(baseUrl + "phone/verifyotp", {
                data: {
                    _token: _token,
                    phoneNumber: phone,
                    otp: otpPassed

                },
                headers: {
                    'X-Requested-With': 'XMLHttpRequest'
                }
            }).on("success", function (data) {
                success = 1 ;
                console.log(colors.blue("Success of verify otp")) ;

                console.log(data);

                var userInfo = {
                    firstName: "John",
                    lastName: "Doe",
                    description: "I'm a getting used for testing .",
                    zipCode: "71151",
                    street: "Testing avenue",
                    city: "San Fransisco",
                    state: "California",
                    email: "john@doe.com",
                    image: ""

                };


            }).on("complete", function (data) {
                if(!success)
                console.log(colors.red(util.inspect(data))) ;
            });
    };


    it("should get the token", function () {
        var success = 0 ;
        restler.get(baseUrl + "/basic/token")
            .on("success", function (data) {
                success = 1;
                console.log(colors.blue("Success of get the Token")) ;
                console.log(data);
                _token = data.token;


            }).on("complete", function (data) {
                if(!success)
                    console.log(colors.red(util.inspect(data))) ;
            });

    });

    it("should get the OTP", function () {
        var success = 0 ;
        restler.post(baseUrl + "phone/getphoneotp", {
            data: {
                _token: _token,
                phoneNumber: phone
            },
            headers: {
                'X-Requested-With': 'XMLHttpRequest'
            }
        }).on("success", function (data) {
            success = 1;
            console.log(colors.blue("Success of get the OTP")) ;
            console.log(data);
            otp = data.otp;

            verifyOtp(otp);
        }).on("complete", function (data) {
            if(!success)
                console.log(colors.red(util.inspect(data))) ;        });
    });

});

Solution

  • I read a few answers on stack overflow about asynchronous testing in jasmine and was able to write the code with the help of that . Here is the code .

    var restler = require('restler');
    var fs = require('fs');
    var colors = require('colors');
    var util = require('util');
    var config = require('./config.js');
    var baseUrl = config.baseUrl;
    
    describe("LEC phone api's tests", function () {
    
        var apiCall;
        var _token;
        var phone = Math.round(Math.random() * 1000000000);
        var otp, otpPassed;
    
        console.log("Test started on " + baseUrl);
        it("should get the token", function () {
    
    
                restler.get(baseUrl + "/basic/token")
                    .on("success", function (data) {
                        success = 1;
                        console.log(colors.blue("Success of get the Token"));
                        console.log(data);
                        _token = data.token;
    
                        apiCall = 1;
    
                    }).on("complete", function (data) {
                        if (!success)
                            console.log(colors.red(util.inspect(data)));
                    });
    
        });
    
        it("should get the otp", function () {
    
            waitsFor(function () {
                return apiCall == 1;
            });
            runs(function () {
                var success = 0;
                restler.post(baseUrl + "phone/getphoneotp", {
                    data: {
                        _token: _token,
                        phoneNumber: phone
                    },
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                }).on("success", function (data) {
                    success = 1;
                    console.log(colors.blue("Success of get the OTP"));
                    console.log(data);
                    otp = data.otp;
    
    
                    otpPassed = otp ;
    
                    apiCall = 2 ;
                }).on("complete", function (data) {
                    if (!success)
                        console.log(colors.red(util.inspect(data)));
                });
            });
    
        });
    
        it("should verify the otp", function () {
    
    
            var success = 0;
            waitsFor(function() {
               return apiCall == 2 ;
            });
            runs(function() {
                restler.post(baseUrl + "phone/verifyotp", {
                    data: {
                        _token: _token,
                        phoneNumber: phone,
                        otp: otpPassed
    
                    },
                    headers: {
                        'X-Requested-With': 'XMLHttpRequest'
                    }
                }).on("success", function (data) {
                    success = 1;
                    console.log(colors.blue("Success of verify otp"));
    
                    console.log(data);
    
                    apiCall = 3 ;
    
                }).on("complete", function (data) {
                    if (!success)
                        console.log(colors.red(util.inspect(data)));
                });
    
            });
        });
    
    
    
        it("should register the user", function () {
    
            var success = 0;
            var userInfo = {
                firstName: "John",
                lastName: "Doe",
                description: "I'm a getting used for testing .",
                zipCode: "71151",
                street: "Testing avenue",
                city: "San Fransisco",
                state: "California",
                email: "john@doe.com",
                image: ""
    
            };
    
            waitsFor(function() {
                return apiCall == 3 ;
            }) ;
            runs(function() {
    
    
                fs.stat('index.jpeg', function (err, stats) {
                    console.log(stats);
                    console.info("user register started");
    
                    restler.post(baseUrl + 'user/register', {
                        multipart: true,
                        headers: {
                            'X-Requested-With': 'XMLHttpRequest'
                        },
                        data: {
                            token: _token,
                            phone: phone,
                            first_name: userInfo.firstName,
                            last_name: userInfo.lastName,
                            description: userInfo.description,
                            zip_code: userInfo.zipCode,
                            street: userInfo.street,
                            city: userInfo.city,
                            state: userInfo.state,
                            email: userInfo.email,
                            device_os: "ios",
                            device_token: "asdfas7sad6f899998j",
                            "folder_id": "0",
                            "image": restler.file("index.jpeg", null, stats.size, null, "image/jpeg")
                        }
                    }).on("success", function (data) {
                        success = 1;
                        console.log(colors.blue("Success of user register"));
    
                        console.log(data);
    
                    }).on("complete", function (data) {
                        if (!success)
                            console.log(colors.red(util.inspect(data)));
                    });
    
                });
    
            });
        });
    
    });