How to exchange data between javascript files?

I (I think) have some unique problem with js. I write tests using protractor and Jasmine materials, and I need to exchange data between js files. Is there any way to do this? All the solutions I found are for web pages and I use only js files.

I look forward to your prompt reply, if any information is missing, let me know and I will add it immediately.

+6
source share
4 answers

I have not tested this myself, but maybe you can try putting material 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.

+6
source

There is an easy way to share data and even functions between Protractor JavaScript file specifications. They work in node.js with a built-in way of defining modules and using and reusing them - http://nodejs.org/docs/latest/api/modules.html .

Assume the following folder break:

 o e2e |-- utils.js |-- a-spec.js |-- b-spec.js 

In utils.js :

 exports.sharedData = { num: 42, str: 'hi' }; exports.foo = function (x) { return x + 1; }; 

In a-spec.js :

 var utils = require('./utils.js'); // Note './' describe("The a page", function () { it("should give the ultimative answer", function () { expect(element(by.binding("answer")).getTest()) .toBe(utils.sharedData.num); // Using shared data }); }); 
+1
source

I have included this in my js configuration file, where I declare my variables for the protractor.

 (function () { this.defaultPassword = function () { return 'superPassword'; }; }()); 

Use in another file:

 var userLogin = { 'Email': ' something@gmail.com ', 'Password': defaultPassword }, 
+1
source

If you need to exchange dynamic data between files, you can also do the following. Here is a working example. I needed to make parts of the URL and use them in 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 just assigned the values ​​that I need to use in other files to 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; }); 
+1
source

Source: https://habr.com/ru/post/975824/


All Articles