Nightwatch - set & get globals - javascript

Basically what I need to do is to get a config file from a csv file and load it as an array into a global variable each time I am running tests in before function.
Lets say mynightwatch.conf.js contains following, example global variable myGlobalArr
"globals": {
myGlobalArr : []
}
And in globals.js I am using readFileSync to read a file and get it's text as an array to a variable
before: function(done) {
var file = fs.readFileSync('path').toString().split('\n')
}
How can I pass an array which is stored in file to myGlobalArr and then call it from inside a Page Object to use it?
Thanks

Related

sapui5 component-preload.js files problem

I'm developing a sapui5 application. I had in index.html one js file, data.js, where I have const:
const a = 'x';
In index.html, I imported the script:
<scrip src="resources/data.js">
In my onInit() function of main.controller.js I can do this:
var myvar = a; (a is "x")
I created successfully component-preload to improve performance.
I delete the import of data.js from index.html, because I can see in chrome devtool debugger my const a defined in data.js by this way, in component-preloaded.js data.js module:
"mynamespace/resources/data.js":"minify content of data.js";
This works! I can use functions of data.js successfully, but in main.controller.js:
var myvar = a;
I get an exception, "reference not found, a is not defined," same exception in all consts, vars and objects.
What is the problem? Why is a not defined by this way?
Is it necessary do something more in component.js or manifest.json to my const a have been defined in all my controller's?
Thanks.
You have a few options.
Through the individual definitions:
First one, that seems to match your original idea the most is include it by adding it to the require call of your component / controllers / custom controls. You'll probably have to return an object instead of simply declaring the variables though, since it avoids global scope.
sap.ui.define([
"sap/ui/core/UIComponent",
"mynamespace/resources/data"
], function(UIComponent, myData) {
"use strict";
//use the content of myData any way you like
return UIComponent.extend("mynamespace.Component", {
};
};
I recommend sticking it in a JSON model so you can use it as binding as well. If you want to use a JSON model you have several other options.
The first is to create the JSON model with reference to your file. Generally you'd do this in your component file.
new JSONModel("relative/path/to/data")
Through manifest.json
Two ways, one as a file, one without an additional file
"models": {
"mydata": {
"type": "sap.ui.model.json.JSONModel",
"uri": "path/to/data.json",
}
}
If you don't want to load a separate file through ajax, you can add your predefined data as a model in the manifest as well. Take this snippet:
"models": {
"mydata": {
"type": "sap.ui.model.json.JSONModel",
"settings": {
"myvar": "a"
}
}
}
That'll create a JSON model containing whatever is inside the settings bit, so in this case it'll create a model called mydata that has an object with a member called myvar with value a, meaning you can pick it up using the usual model syntax:
this.getOwnerComponent().getModel('mydata').getProperty('/myvar');
.. or use it in your view's bindings
"{mydata>/myvar}"
I tend to use the last option, as it does not load an extra file, and all apps already have a manifest.
I will do it as you said next time I need global const. Thank you.
To solve what I already had wrong, I have minified and compressed all my js files with problems in a single file, and I call it from the index as it worked with data.js.
<script src="/resources/allinone.js"></script>
I know this isn't the best practise but translate all my const of all files to json would take me a long time.

node.js dynamic module(file) export

how can I exports this dynamic module?
// ./data/example5.js
module.exports = {title : example5, recentCheck : "2018-08-22"}
The contents change in real time. And I execute the followed function once a minute and connect the module.
var files = fs.readdirSync(`./data/`);
var i = 0;
var link = [];
while(i<files.length){ // read all file
link[i] = require(`../data/${files[i]}`);
 
//main.js
setInterval(start,10000);
I try to create and connect a new module file once a minute, but the first stored file(module) is extracted. The modulefile is being saved in real time correctly.
Shutting down and running the node will extract the changed modules correctly.
How do I handle a dynamically changing module?
I would recommend saving the data in a JSON file and then reading the data from the file rather than trying to use it as a module.
Just make the objects you're updating variables in the module that you're including.
Make a function called getVariable, and simply return the variable.
Include getVariable in your main module.

Require module in Node JS

I used Node JS for web application development. I have a confusion in require() module. I am requiring a JS file located in file_handler directory.
What is the difference between both of the following?
// in server.js
var chat = require("./file_handler/chat.js"); // Does not work
OR
var chat = require("./file_handler/chat.js")(); // It works
Why is the extra parenthesis in the last of the statement?
In the first line the exported function is assigned to chat variable so then you can call it like next like chat();
In the second one the return of exported function is returned to chat variable.
It is actually based on what you export in your module. If you export the object you need, you can just directly do require('module'). If you export a function which returns the object you need, you have to execute that exported function require('module')() to get the desired object.
Read the documentation https://nodejs.org/api/modules.html

NodeJS - Get variable from another file without redefining it each call?

So I have 2 files a mapgen.js and a main.js. In mapgen.js there is a function that generates a giant 2d array. I want to use this aray in main.js but don't want the function that generates the map to run everytime its 'required' in main.js. I also want to be able to edit the map array eventually.
Example: (not real code just wrote some crap to show what the issue is)
mapgen.js:
var map;
function mapGen(){
//make the map here
this function takes like 2 seconds and some decent CPU power, so
don't want it to ever run more than once per server launch
map = map contents!
}
main.js
var map = require mapgen.js;
console.log(map.map);
//start using map variable defined earlier, but want to use it without
having to the run the big funciton again, since it's already defined.
I know i have to module.exports somewhere but I dont think that will solve my problem still. I would write it to a file but is that not much slower to read and edit than keeping it in the ram? Previously I had gotten past this by keeping everything in 1 file but now I need to clean it all up.
Requiring the module won't automatically invoke the function. You can do that in the main.js file.
mapgen.js
module.exports = function mapGen() {
return [/* hundreds of items here. */];
};
main.js
// Require the module that constructs the array.
const mapGen = require('./mapgen');
// Construct the array by invoking the mapGen function and
// store a reference to it in 'map'.
const map = mapGen(); // `map` is now a reference to the returned array.
// Do whatever you want with 'map'.
console.log(map[0]); // Logs the first element.
I'm not an expert but if you put one condition in mapgen.js that don't work ?
var map;
function mapGen(){
if(!map){
//your code here
map = map contents!
}
}
Combine that with global variable and/or module.exports See
How to use global variable in node.js?

about node.js / javascript sharing variable between file

I have some files that need database access so I have a file like this:
...
var dynamo = new AWS.DynamoDB.DocumentClient();
module.exports.getDatabase= function(){
return dynamo;
};
...
I wonder if different .js files use it like this:
var DataUtil = require('./shared/dataUtils.js');
...
var database = DataUtil.getDatabase();
....
are they using the same instance of the object? or just instantiating a copy for each of the .js file using the requiring?
Yes, it's the same instance. When you require a module, it's only loaded when it's not already loaded. So there's only one instance of a module in a node program.
From the documentation:
Modules are cached after the first time they are loaded. This means
(among other things) that every call to require('foo') will get
exactly the same object returned, if it would resolve to the same
file.
In your case, you'll have only one instance of AWS.DynamoDB.DocumentClient.

Categories

Resources