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.
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.
Share Improve this question asked Sep 25, 2014 at 10:29 Krzysztof PiszkoKrzysztof Piszko 1,4453 gold badges13 silver badges17 bronze badges 7- 2 I haven't used protractor yet, but with karma and jasmine, you can share data using requirejs. If you want I can give some more details. – kihu Commented Sep 25, 2014 at 10:43
- As far as I know I can only use: Protractor API, Selenium API, Jasmine, JS. It's not my private project so unfortunately I can't use other stuff. UNLESS, I understood you wrong and this is "built into" js. @Edit: Wait, you mean that requirejs is built into Jasmine? If yes, then by all means elaborate. – Krzysztof Piszko Commented Sep 25, 2014 at 10:51
- no, requirejs is a saparate library requirejs – kihu Commented Sep 25, 2014 at 10:55
- That's a darn shame then. Thanks anyway! – Krzysztof Piszko Commented Sep 25, 2014 at 10:56
- Otherwise. perhaps you could declare your data as a global variable in first test, then it would be available for other tests to read. I'm not 100% sure it will work, though. – kihu Commented Sep 25, 2014 at 11:01
4 Answers
Reset to default 8I 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/api/globals.html
Let me know if it works.
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;
});
There is a simple way to share data and even functions among Protractor
spec JavaScript files. They run in node.js
with a built-in way to define modules and use and reuse them - http://nodejs/docs/latest/api/modules.html.
Assuming the following folder's stracture:
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
});
});
I included this in my config js file where i declare my variables for protractor.
(function () {
this.defaultPassword = function () {
return 'superPassword';
};
}());
Usage in other file is like:
var userLogin = { 'Email': '[email protected]', 'Password': defaultPassword },