I am trying to pass in a variable to use for executeScript as shown below:
var groupScript = '\'' + 'localStorage.setItem("groups", "[' + groupNames + ']"); + '\'''
which when printed to the console gives me:
'localStorage.setItem("groups", "[\\"lunch\\"]");'
If I try to run:
browser.executeScript( groupScript );
It doesn't create the localStorage variable. However, if I run the following (which just has the value of the variable), it does work:
browser.executeScript( 'localStorage.setItem("groups", "[\\"lunch\\"]");');
Can someone let me know what I need to do in order to get it to work with passing in the variable? Trying to create localStorage variable for my protractor test. Thanks!
There is a special arguments array containing the list of arguments passed into a script:
browser.executeScript('localStorage.setItem("groups", arguments[0]);', groupNames);
Related
I Tried this code to get multiple value in href but it does not work. any problem on this one ?
Print
You are missing a + sign between a string and a value.
The error is between this two
document.getElementById('CUS_CODE_MX').value '&AGE='
Correct format
document.getElementById('CUS_CODE_MX').value + '&AGE='
Every time you join a value and a string, you need a + sign
Even if you are joining two strings
'Hello'+ 'World'
Pliss avoid long js as an inline atribute. I will recommend you call a function as the onclick attribute.
Hope this helps :)
Print
It's better to use external script for that rather than inline format. And just add missing + to your code. Also, using variables would clean up the code.
function func() {
var CUS_CODE_MX = document.getElementById('CUS_CODE_MX').value;
var AGEID = document.getElementById('AGEID').value;
this.href = 'printsales.php?CUSTOMERID='+CUS_CODE_MX+'&AGE='+AGEID;
}
Print
I'm looking for an easy way to assign to a variable depending on the value of another variable.
device.slot2_clipList[clipNumber] = singleClipDetails;
what I'm trying to do is: replace the "2" with another variable, so that i can run the same operation while just changing the
var slotNumber, and write to the corresponding variable.
i tried
device.slot + device.slotNumber + _clipList[clipNumber]
but (obviously?), this doesn't work.
How can this be done? (Maybe I named the Question incorrectly, but that was the closest I could think of.)
Thanks
This is what bracket notation is for
var i = 2;
device['slot' + i + '_clipList'][clipNumber] = singleClipDetails;
device['slotNumber' + _clipList[clipNumber] ]
Explanation:
foo.bar in javascript is identical (even in performance) to foo['bar']. So any object property name can be built up from strings.
I am coding in node.js atm. I need to create a dynamic variable and invoke it.
e.g.:
username = 'im_a_user';
global['ws[' + username + ']'] = ws; //(yes, i want to store the connection with ws module)
but
ws[im_a_user].send('blabla');
doesn't work and node shuts down. So I want to know how global['ws[' + username + ']'] looks like for debbuging.
Do you know how I can print it - or even better, why im_a_user in ws[im_a_user].send('blabla'); isn't defined?
Thanks for your time!
Accessing object property is possible with brackets, but you have to give it a valid expression: a string literal or a variable. Looks like you are referencing a variable that is not defined. That's where Node chokes.
So try either with a variable that is defined
var key = 'im_a_user'
ws[key].send('blabla');
or a string literal
ws['im_a_user'].send('blabla');
Hardcoded i would initilize (a plugin in this case) like so, which is working:
var cal = $("#calendar").calendario({
caldata : {
'09-11-2015_1':['09-11-2015',0,19]
}
});
Now i want to pass the caldata option a variable with the content like that:
var init_events = $("#init_events").val();
var cal = $("#calendar").calendario({
caldata : init_events
});
init_events has the value {'09-11-2015_1':['09-11-2015',0,19]}
But that doesnt work. If I log the output of the option inside the plugin it just returns a string in the console where as if I log the first Code it logs an Object.
I tried jQuery.parseJSON(init_events) but this returns an Unexpected token error.
Any idea how i could get this working with passing a variable?
init_events is not valid JSON. JSON only allows double quotes around strings, not single quotes, so it should be:
{"09-11-2015_1":["09-11-2015",0,19]}
I've been having trouble setting a dynamic filter for oocharts.
I've looped through some things and have created an array (scenefilters) which I've joined to make a string. I've then tried to use this as a filter, which returns the error:
*JSONP.callbacks.request_63 && JSONP.callbacks.request_63({"error":"Invalid param {filters}: Filter string is not in valid format"});*
Now if I console.log the variable 'locationfilter2' and copy and paste it directly into the filter, it works fine - but it doesn't work just with the variable. Do I need to do something else to the variable to make sure it's a string?
var locationfilter = scenefilters.join(",");
var locationfilter2 = '"'+ locationfilter +'"';
var visits = new oo.Metric("54190402", "12m");
visits.setMetric("ga:visits");
visits.query.setFilter(locationfilter2);
visits.draw(timeline);
EDIT
I've tried outputting the variable to a textfield and copying it from there, same thing. The output is "ga:pagePath=~s101$,ga:pagePath=~s102$,ga:pagePath=~s103$,ga:pagePath=~s104$,ga:pagePath=~s105$,ga:pagePath=~s106$,ga:pagePath=~s107$,ga:pagePath=~s108$,ga:pagePath=~s109$,ga:pagePath=~s110$,ga:pagePath=~s111$,ga:pagePath=~s112$,ga:pagePath=~s113$,ga:pagePath=~s114$,ga:pagePath=~s115$,ga:pagePath=~s116$,ga:pagePath=~s117$,ga:pagePath=~s118$,ga:pagePath=~s119$,ga:pagePath=~s293$,ga:pagePath=~s301$"
And if I copy this directly into the filter, i.e. visits.query.setFilter("ga:pagePath=~s101$,ga:pagePath=~s102$,ga:pagePath=~s103$,ga:pagePath=~s104$,ga:pagePath=~s105$,ga:pagePath=~s106$,ga:pagePath=~s107$,ga:pagePath=~s108$,ga:pagePath=~s109$,ga:pagePath=~s110$,ga:pagePath=~s111$,ga:pagePath=~s112$,ga:pagePath=~s113$,ga:pagePath=~s114$,ga:pagePath=~s115$,ga:pagePath=~s116$,ga:pagePath=~s117$,ga:pagePath=~s118$,ga:pagePath=~s119$,ga:pagePath=~s293$,ga:pagePath=~s301$");
It works well. But if I use the variable I get the error. Anyone any ideas?
OK stupid one. Removed the ""s and it works fine.