Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share data between javascript files?

I have (I think) kind of unique problem with js. I'm writing tests using protractor stuff and Jasmine and I need to share data between js files. Is there any way to do that? All the solutions I've found are for webpages and I use just js files.

I look forward to your swift response, if there is any information missing, please let me know and I'll add it immediately.

like image 840
Krzysztof Piszko Avatar asked Sep 25 '14 10:09

Krzysztof Piszko


2 Answers

I have not tested this myself, but maybe you can try to put stuff in the global scope using:

global.mySharedData = {someKey: 'some value'}

// in one of your test files
it('should do something', function() {
  global.mySharedData = {someKey: 'some value'}
});

...

// This is in another test suite
it('should do something', function() {
  var valueFromFirstTest = global.mySharedData.someKey;
});

http://nodejs.org/api/globals.html

Let me know if it works.

like image 152
Andres D Avatar answered Sep 29 '22 12:09

Andres D


If you need to share dynamic data between files you can also do the following. Here's a working example. What I needed to do was take parts of the URL and use them across different files.

it('should click on one of the clickable profiles', function(){

        //Get entity type and entity id before clicking the link
        tableEls.get(1).all( by.xpath('./td') ).get(0).element( by.xpath('./a') ).getAttribute('href').then(function(text){

            var hrefTokens  = text.split('/');
            var entityID    = hrefTokens[ hrefTokens.length - 1 ];
            var entityType  = hrefTokens[ hrefTokens.length - 2 ];

            browser.params.entityID   = entityID;
            browser.params.entityType = entityType;
        });

        tableEls.get(1).all( by.xpath('./td') ).get(0).element( by.xpath('./a') ).click();
        browser.sleep(2000);
    });

I simply assigned the values that I needed to use in other files to the browser.params. So in my other files I can access them like this

    it('Retrieving JSON Data ...', function(){

        var entityID   = browser.params.entityID;
        var entityType = browser.params.entityType;
   });
like image 39
Haseeb Avatar answered Sep 29 '22 12:09

Haseeb