Protractor passing variable between describe functions - javascript

I am trying to pass a variable representing an array between describe functions and I am having no luck. I have used protractor before but never passing variables between describes before. any help would be appreciated.
I combed through the stack over flow pages and could not find a solution.
I even tried to put each describe into a function but protractor said specs not found.
describe('WFN Admin Login Test', function() {
var EC = protractor.ExpectedConditions;
it('Load WFN home page - completed', function() {
browser.get('https://wfn-iat.adp.com/public/index.htm');
expect(browser.getCurrentUrl()).toEqual('https://wfn-iat.adp.com/public/index.htm');
});
var fs = require("fs");
var text = fs.readFileSync("/Users/hoflerj/Desktop/Protractor/clients/clientids.txt").toString('utf-8');
var file = text.split("\n");
console.log(file);
var arrayClient = file;
arrayClient.forEach(function(client){
//call other describe function to pass client variable below
});
});
describe('Get_CycleStatus', function() {
var EC = protractor.ExpectedConditions;
it('Enter Client-ID ', function () {
var search1 = ($$('input[id="toolbarQuickSearch"]').get(0));
browser.wait(EC.elementToBeClickable(search1),20,000).then(function() {
search1.clear().sendKeys('midrfrate'); //----client array variable here
search1.sendKeys(protractor.Key.BACK_SPACE);
browser.sleep(2000);
});
var dropdown = element(by.linkText("midrfrate"));//----client array variable
dropdown.click();
browser.sleep(2000);
});
I will eventually do a loop so that I can input the next client name any help how to do this with protractor would be most helpful. Also after setting up this global variable how would i use a loop to send this to another describe statement?

In your describe call the desired params with browser.params.paramName (in your case browser.params.arrayClient)
For your loop use an array or an object like so:
params: {
array:[ {
glob:'arrauClient',
glob2:'blabla'
}]
then call it in your describe with browser.params.glo

Related

Nodejs Webscraping function using Cheerio returns before finished

I'm currently working on a simple web scraping nodejs program. It is based on cheerio and I get items from a website and extract some information from there.
As far as I understand it all functions I call inside the foreach loop are sync so they should execute from top to bottom. And because the foreach loop is also only a normal loop, which executes sync in js, the function should return my finished array. But instead it is getting undefined and when I log it inside directly to console it works(?).
function getIntensiv(){
var intensivregister = [];
request.post({url: 'SOMEURL', form: {SOMEFORM}}, function(err,res,body){
var $ = cheerio.load(body);
$('#dataList').children('tbody').children('tr').each(function(i, elem){
var name = $(elem).children('td').first().text().trim().split("\n")[0].trim();
var zipcity = $(elem).children('td').first().children('small').last().text();
var streetnr = $(elem).children('td').first().children('br').last().prev().text();
intensivregister.push({'name': name, 'zipcity': zipcity, 'streetnr': streetnr});
});
console.log(intensivregister); //works and prints the finished array
return intensivregister; //returns undefined before function finished
});
}
I would appreciate it if you could explain me where my mistake is and help me fix it.
function getIntensiv(){
const cheerio = require('cheerio')
const request = require('request')
var intensivregister = [];
request.get({url: 'https://www.w3schools.com/html/html_tables.asp'}, function(err,res,body){
var $ = cheerio.load(body);
$('#customers').children('tbody').children('tr').each(function(i, elem){
var name = $(elem).children('td').first().text().trim().split("\n")[0].trim();
var zipcity = $(elem).children('td').first().children('small').last().text();
var streetnr = $(elem).children('td').first().children('br').last().prev().text();
intensivregister.push({'name': name, 'zipcity': zipcity, 'streetnr': streetnr});
});
console.log(intensivregister); //works and prints the finished array
return null; //returns undefined before function finished
});
return null; //***<---This is returning and not the above return. If no return statement is written then undefined is passed.***
};
var retrunVal = getIntensiv()
console.log(retrunVal);
Please find the highlighted comment
Ok I figured out that my idea of javascript was not how you should use it. I worked around my problem with getting rid of the idea of returning values from functions (which comes mainly from my experiences from async programming) and instead using callback parameters which I give to my function and call at the end of my request.
function getIntensiv(callback){
var intensivregister = [];
request.post(...);
**callback(intensivregister);**
}
What also is working (and I think a better solution) is working with promises e.g. with request-promise and calling the callback in the finally call.

fcm firebase function undefined value - first attempt, defined - second attempt

I have an Android application and I intend to implement send notification through fcm. I have written below Javascript code for it but when I try to follow someone for the first time, the values are undefined, however, for the second time it works perfectly as expected...
Undefined first attempt
Defined second attempt
i tried nesting those three functions inside one, and also tried declaring variables locally but not solved.
"use-strict"
const functions = require('firebase-functions');
const admin = require("firebase-admin");
admin.initializeApp();
var notificationDB;
var followedUserRef;
var followingUserRef;
var followingText;
var followingImageProfile;
var followedDToken_id;
var following;
var followingFullname;
exports.sendNotification =functions.database.ref("Notifications/{followed}/{notification_id}").onWrite((snapshot,context)=>{
const followed = context.params.followed;
const notification_id = context.params.notification_id;
notificationDB = admin.database().ref(`Notifications/${followed}/${notification_id}`);
console.log("Notification DB : "+notificationDB);
notificationDB.on("value",(snapshotOne)=>{
following = snapshotOne.val().userid;
followingText = snapshotOne.val().text;
});
followedUserRef = admin.database().ref(`Users/${followed}`);
followedUserRef.on("value",(snapshot)=>{
if(snapshot.val().token_id===null){
followedDToken_id = "null";
}else
followedDToken_id = snapshot.val().token_id;
});
followingUserRef = admin.database().ref(`Users/${following}`);
followingUserRef.on("value",(snapshot)=>{
followingFullname = snapshot.val().fullname;
followingImageProfile = snapshot.val().imageurl;
});
console.log("Following: "+following);
console.log("Followed: "+followed);
console.log("followed user Ref: "+followedUserRef)
console.log("NAME: "+followingFullname);
console.log("FOLLOWING TEXT: "+followingText);
console.log("tokken: "+followedDToken_id);
console.log("IMAGE: "+followingImageProfile);
});
expected result should show on first attempt, but it fails at first and get the values at second time.
Your code is not entirely executed sequentially as you seem to expect, there are requests sent to the server and you need to do your console logs in the callbacks. For example here
notificationDB = admin.database().ref(`Notifications/${followed}/${notification_id}`);
console.log("Notification DB : "+notificationDB);
notificationDB.on("value",(snapshotOne)=>{
following = snapshotOne.val().userid;
followingText = snapshotOne.val().text;
});
following will only be initialized upon response, which will be executed after you console log. So, you need to refactor your code with the following principles in mind:
you initialize your variables
afterwards you call the functions which send requests
in the callback you do your console log
Even more elegant would be to use the Promise API.

How to test node data chunking function

I'm working on a project which uses node and we're trying to achieve 100% coverage of our functions. This is the only function we haven't tested, and it's within another function.
var userInput = "";
req.on("data", function(data){
userInput += data;
});
How do you go about testing this function? We tried exporting the function from another file but no luck.
I should mention that we are using tape as a testing module.
You need to trigger this "data" event on req. So that this callback will be called.
For instance, let's suppose you have req on your test, you could do something like that (this is Mocha):
req.trigger('data', 'sampleData');
expect(userInput).to.equal('sampleData');
req.emit('data', {sampleData: 'wrongOrRightSampleDataHere'}) should do it.
When instantiating the http or hence the req object make sure you instantiate a new one, that no other test receives this event.
To be more complete...
var assert = require('assert')
function test() {
var hasBeenCalledAtLeastOnce = false
var userInput = "";
// req must be defined somewhere though
req.on("data", function(data){
userInput += data;
if(hasBeenCalledAtLeastOnce) {
assert.equal(userInput, "HelloWorld", "userInput is in fact 'HelloWorld'")
}
hasBeenCalledAtLeastOnce = true
});
req.emit('data', "Hello")
req.emit('data', "World")
}
test()

Access parameter of Node.js module function

im totally new to node.js and i couldn't find a similar question for my problem. I'm sure it's easy to solve for one of u guys... at least i guess.
I'm trying to get a special paragraph of a wikipage using the npm mediawiki module for node.js! I get the paragraph using the pre-defined function as following:
bot.page(title).complete(function (title, text, date) {
//extract section '== Check ==' from wikipage&clean string
var result = S(text).between('== Check ==', '==').s;
});
Thats working. What i want is: to use "result" outside of that code block in other functions. I think it has something to do with callbacks but im not sure how to handle it as this is a pre-defined function from the mediawiki module.
The example function of the module to get a wikipage looks as following:
/**
* Request the content of page by title
* #param title the title of the page
* #param isPriority (optional) should the request be added to the top of the request queue (defualt: false)
*/
Bot.prototype.page = function (title, isPriority) {
return _page.call(this, { titles: title }, isPriority);
};
which uses the following function of the module:
function _page(query, isPriority) {
var promise = new Promise();
query.action = "query";
query.prop = "revisions";
query.rvprop = "timestamp|content";
this.get(query, isPriority).complete(function (data) {
var pages = Object.getOwnPropertyNames(data.query.pages);
var _this = this;
pages.forEach(function (id) {
var page = data.query.pages[id];
promise._onComplete.call(_this, page.title, page.revisions[0]["*"], new Date(page.revisions[0].timestamp));
});
}).error(function (err) {
promise._onError.call(this, err);
});
return promise;
}
There's also a complete callback function and i dont know how to use it:
/**
* Sets the complete callback
* #param callback a Function to call on complete
*/
Promise.prototype.complete = function(callback){
this._onComplete = callback;
return this;
};
How can i access the "result" variable by using callbacks outside the function of the module? I don't know how to handle the callback as it is a pre-defined function of a module...
What i want is: to use "result" outside of that code block in other functions.
You can't. You need to use the result inside that code block (that code block is called a callback function btw.). You can still pass them to other functions, you just need to do it inside that callback function:
bot.page(title).complete(function (title, text, date) {
//extract section '== Check ==' from wikipage&clean string
var result = S(text).between('== Check ==', '==').s;
other_function(result); // <------------- this is how you use it
});

JAWR i18n: unit testing javascript when using messages

Our application currently shares messages between the Java and Javascript side. They are stored as resource bundles in the class path, and we have a custom controller that returns all the messages as Json. The client side code look like this:
// This calls the controller to get all the messages
var messages = MessageBundle();
var text = messages.get('my.message', 1);
This is great because we can mock "messages" in our unit tests.
I want to start using JAWR for this, since we already use it for other things. The problem is JAWR generates the following Javascript object:
var text = messages.my.message(1);
This means the code cannot be unit tested anymore unless the unit tests also define a global "messages" variable with the right nested objects. Is there a way around this? Any idea how to extend JAWR to make this unit-testable?
Currently my work around is:
function messages() {
var args = Array.prototype.slice.call(arguments);
var messageId = args.shift();
var messageFunc = window.messages;
messageId.split('.').forEach(function(part) {
messageFunc = messageFunc[part];
});
return messageFunc(args);
}
// Same syntax as the old one, but uses the JAWR object behind the scenes
// This function is easy to mock for a unit test
var text = messages('my.message', 1);
Thanks for any ideas!
Maybe next samples can help you.
1)
function messagesTester(funcPath,id) {
var args=funcPath.split('.'),root=window.messages;
for(var i=0;i<args.length;i++)root=root[args[i]];
return root(id);
// or if more that one parameter for *func*, then, for example:
// return root.apply(null,Array.prototype.slice(arguments,1));
}
var text = messagesTester('my.message',1);
2)
function messagesTester(funcPath) {
var args=funcPath.split('.'),root=window.messages;
for(var i=0;i<args.length;i++)root=root[args[i]];
return root;
}
// var text = messagesTester('my.message')( /*arguments list*/ );
var text = messagesTester('my.message')(1);

Categories

Resources