1

I am writing Protractor e2e testing .I have more than one .js file (spec.js). I need to call one file from another. Can anyone suggest me an idea..

Noor
  • 9
  • 5

2 Answers2

1

The solution of this problem is simple is to implement Module Exports, It allows you to require specific java script files in a different directory or same folder. This is a easy to achieve method - I have written a format for using this take a look through this github repo i created for the project, it implements what you are trying to accomplish.

For the javascript file you want to import use the format like this

var bindObjects;
var authServer;
var socketData = [];

var log_ValidConnections = true;
var log_InvalidConnections = true;
var log_GetFriendsRequest = false;

// For imports, to get the functions in another file
module.exports = {
   init : function(binds) {
    bindObjects = binds;
    authServer = bindObjects['AUTH.Server'];

    console.log("SocketServer - Syncing with AUTH Servers");
    initServer(bindObjects['APP.SOCKET.IO']);
    console.log("SocketServer - Loaded");
},

setBinding : function(data) {
    bindObjects = data;
},

getSocketData : function() {
    return socketData;
},

getSocket : function(username) {
    return getSocket(username);
},

getSocketData : function(username) {
    return getData(username);
}
};

and for the module importing the above javascript

function setBinding(callInit) {
    var socketServer        = require("./sockets"); // Dont add .js
    var bindObjects         = {
    'WEB.Server': webServer,
    'CHAT.Server': chatServer,
    'AUTH.Server': authServer,
    'SOCKET.Server': socketServer,
    'JSON.WEB.Tokens': jsonWeb,
    'APP.Instance': app,
    'APP.Http': http,
    'APP.SOCKET.IO': io,
    'APP.EXPRESS': express,
    'APP.DEBUG': debugMode
};

if(callInit) {
    webServer.init(bindObjects);
    chatServer.init(bindObjects);
    authServer.init(bindObjects);
    socketServer.init(bindObjects);
} else {
    webServer.setBinding(bindObjects);
    chatServer.setBinding(bindObjects);
    authServer.setBinding(bindObjects);
    socketServer.setBinding(bindObjects);
}
}

// to make the module re-bindable create a exports
module.exports = { 
rebindObjects : function() {
    setBinding(false);
}
};
Callum
  • 725
  • 6
  • 17
0

You should use a service instead to avoid muddying up the controllers with code that calls functions in other controllers. If you're sharing functions, just define them in a service and inject that service where needed.

angular.module('myApp', [])
  .factory('myService', function() {
    return {
      sharedFunction: function (foo, bar) {
        return foo + bar;
      }
    }
};

Answer Source (also possible duplicate of this question)

I don't do angular, so this might not be the answer you need but ill leave a link here just hoping that it will become useful (Answer originally by thomastuts)

Community
  • 1
  • 1
Callum
  • 725
  • 6
  • 17
  • I am using protractor with node. My second file(.js) will make use of first file's output. Is that possible if I am using 'require'. – Noor Oct 20 '15 at 23:04
  • Yes it is possible, let me post another answer with a solution – Callum Oct 21 '15 at 00:04
  • Let me put my question like this. My first .js returns array as output(col[0]). If I m using same .js file , I simply call first file's output in my second file. But how should I do this if I am using two .js file. How should I feed my first file's output to second file... – Noor Oct 21 '15 at 17:05
  • Hmm, can you post a example code so i know what your dealing with? – Callum Oct 21 '15 at 17:23