how to pass dynamic arguments into functions , instead of hardcoded strings - webdriver-io

We are facing an issue while trying to pass argument stored in var .
The issue is we are storing the dynmically retrived div id in a variable and then passing it to .moveToObject function.
But the function throws back error ;
Error: invalid selector: No selector specified
Wanted to know how we can pass arguments stored in variables into various functions.
For instance in the code below , we are retiring sectionId via execute , storing it in sectionId variable .
then using it later on in moveToObject.
all functions seems to work when we are giving arguments inside "" quotes, but in our case we cannot do this. Please help us out how we can achieve this.
it('drag and drop should work', function() {
var sectionId = "";
// load page, then call function()
return driver
.url('http://localhost:9000')
.pause(7000)
.click('#layoutWizardButton')
.click('#tableWizardBuild')
.pause(3000)
.execute(function() {
sectionId = window.document.getElementById('Section1');
}
.moveToObject('#ribbon-radio-tile')
.buttonDown()
.moveToObject(sectionId) -- -- > errors out here
.buttonUp()
.pause(2000)
.end()
});

Try .moveToObject(sectionId.value)

Related

Passing multiple parameters using karate.call

I am trying to call an API in second feature file , passing arguments from first feature file . Say token and current page value which is returned from a first API response.These has to be passed as a param for second API
* def activeDetails =
"""
function(times){
for(i=0;i<=times;i++){
karate.log('Run test round: '+(i+1));
karate.call('getActiveRouteDetails.feature', { token: token, currentPage: i });
}
java.lang.Thread.sleep(1*1000);
}
"""
* call activeDetails totalPages
In my second feature , I am able to print the values passed , but unable to pass in params . Can you please help me
And print currentPage
And print token
And param pageNumber = '#currentPage'
And param token = token
There is a subtle difference when you are in a JavaScript block. Please read this: https://github.com/intuit/karate#karate-expressions
Make this change:
var result = karate.call('examples/getDetails.feature', { token: token, currentPage, i });
And please don't have variable names like current page, take the help of a JavaScript programmer friend if needed for help.
Also note that the best practice is to avoid JS code and loops as far as possible: https://github.com/intuit/karate#loops

Accessing json document keys in adapter procedure (IBM worklight)

I have a local jsonstore linked to a remote sql database through an adapter. I am trying to push local data to the remote database like this :
submit_data = function () {
accessor.getPushRequired().then(function (result) { alert(result.length);
accessor.push();
});
Let us say I only added documents (no replace/remove).
I want my add procedure to look like this:
function addGegegr(document) {
//WL.Logger.warn(document[56]);
return WL.Server.invokeSQLStoredProcedure({
procedure : "DBO.USP_TIM_add_sp",
parameters : [document.key1,document.key2,document.key3,...]
});
}
And same for the replace/remove procedure, they should be calling other stored procedures with the same parameters.
However, I get errors because it seems the variable "document" worklight is passing to the add function after push is called is just a string and not a json object.
So my question is : how do I access the json attributes from the adapter procedure? If not possible, can I pass those attributes to the push function?
P.S.: passing each of the n documents to add as parameter of push() does not work and causes add to be called n*n times instead of n...
Try using JSON.parse to turn the string into a JavaScript object.
function addGegegr(document) {
var doc = JSON.parse(document);
//... use doc as a regular JavaScript object
}
If JSON.parse is not available there, you can add it by including this code in the adapter implementation file.

Datatables: How to reload server-side data with additional params

I have a table which gets its data server-side, using custom server-side initialization params which vary depending upon which report is produced. Once the table is generated, the user may open a popup in which they can add multiple additional filters on which to search. I need to be able to use the same initialization params as the original table, and add the new ones using fnServerParams.
I can't figure out how to get the original initialization params using the datatables API. I had thought I could get a reference to the object, get the settings using fnSettings, and pass those settings into a new datatables instance like so:
var oSettings = $('#myTable').dataTable().fnSettings();
// add additional params to the oSettings object
$('#myTable').dataTable(oSettings);
but the variable returned through fnSettings isn't what I need and doesn't work.
At this point, it seems like I'm going to re-architect things so that I can pass the initialization params around as a variable and add params as needed, unless somebody can steer me in the right direction.
EDIT:
Following tduchateau's answer below, I was able to get partway there by using
var oTable= $('#myTable').dataTable(),
oSettings = oTable.fnSettings(),
oParams = oTable.oApi._fnAjaxParameters(oSettings);
oParams.push('name':'my-new-filter', 'value':'my-new-filter-value');
and can confirm that my new serverside params are added on to the existing params.
However, I'm still not quite there.
$('#myTable').dataTable(oSettings);
gives the error:
DataTables warning(table id = 'myTable'): Cannot reinitialise DataTable.
To retrieve the DataTables object for this table, please pass either no arguments
to the dataTable() function, or set bRetrieve to true.
Alternatively, to destroy the old table and create a new one, set bDestroy to true.
Setting
oTable.bRetrieve = true;
doesn't get rid of the error, and setting
oSettings.bRetrieve = true;
causes the table to not execute the ajax call. Setting
oSettings.bDestroy = true;
loses all the custom params, while setting
oTable.bDestroy = true;
returns the above error. And simply calling
oTable.fnDraw();
causes the table to be redrawn with its original settings.
Finally got it to work using fnServerParams. Note that I'm both deleting unneccessary params and adding new ones, using a url var object:
"fnServerParams": function ( aoData ) {
var l = aoData.length;
// remove unneeded server params
for (var i = 0; i < l; ++i) {
// if param name starts with bRegex_, sSearch_, mDataProp_, bSearchable_, or bSortable_, remove it from the array
if (aoData[i].name.search(/bRegex_|sSearch_|mDataProp_|bSearchable_|bSortable_/) !== -1 ){
aoData.splice(i, 1);
// since we've removed an element from the array, we need to decrement both the index and the length vars
--i;
--l;
}
}
// add the url variables to the server array
for (i in oUrlvars) {
aoData.push( { "name": i, "value": oUrlvars[i]} );
}
}
This is normally the right way to retrieve the initialization settings:
var oSettings = oTable.fnSettings();
Why is it not what you need? What's wrong with these params?
If you need to filter data depending on your additional filters, you can complete the array of "AJAX data" sent to the server using this:
var oTable = $('#myTable').dataTable();
var oParams = oTable.oApi._fnAjaxParameters( oTable );
oParams.push({name: "your-additional-param-name", value: your-additional-param-value });
You can see some example usages in the TableTools plugin.
But I'm not sure this is what you need... :-)

dojo 1.7 QueryReadStore parameters

I am new to Dojo, I am using QueryReadStore as the store for loading my TreeGrid, working fine. But the QueryReadStore appends some paramters to the url, parameters like parentId, count, sort etc., I have looked at this link http://dojotoolkit.org/reference-guide/1.7/dojox/data/QueryReadStore.html, but not able to understand.
Parameters are getting passed like this servlet/DataHandler?start=0&count=25
How to manipulate the parameters, like I want to set the value for parentId paramters so that I only get that particular row details.
In theory you wold have to create a new class by extending the "dojox.data.QueryReadStore", in the link you posted have an example for doing exactly what you want. See if you get it now(changed a bit):
dojo.require("dojox.data.QueryReadStore");
dojo.declare("custom.MyReadStore", dojox.data.QueryReadStore, {
fetch:function(request){
//append here your custom parameters:
var qs = {p1:"This is parameter 1",
q:request.query.name
}
request.serverQuery = qs;
// Call superclasses' fetch
return this.inherited("fetch", arguments);
}
});
So When come to create the QueryReadStore you actually create a object with the class you defined. something like this:
var queryReadStore = new custom.MyReadStore({args...})
Explore the request parameter passed to the function to see what else you can do.

Returning external data from a function in ActionScript

I have the following script that is calling a text file:
/* first create a new instance of the LoadVars object */
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(getreading):String{
var ODOMETER2:String=myVariables.ACADEMICWATER;
return ODOMETER2;
trace (ODOMETER2);
}
trace(getreading());
The text file contains the following:
ACADEMICWATER=3002&elec=89
I am able to import the value of 3002 into the function and I can trace it. However, I Should be able to trace it outside the function using trace(getreading()); as shown on the last line. This only returns an "UNDEFINED" value. I am stumped.
You are declaring an anonymous function (see AS3 Syntax and language / Functions) which can't be referenced by name. getreading is declared in your code as an untyped parameter of this function.
If you want to trace the result of this function, then you should declare a named function like this:
function getReading(): String {
var ODOMETER2:String=myVariables.ACADEMICWATER;
return ODOMETER2;
}
myVariables.onLoad = getReading;
trace(getReading());
getreading is not the name of the function in this case, but the name of a parameter to the anonymous function that is run on the onLoad event of the myVariables object.
Place the variable ODOMETER2 outside the function and set it's value inside the anonymous function. Then you will be able to access it outside the function as well.
/* first create a new instance of the LoadVars object */
var ODOMETER2:String;
myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(){
ODOMETER2=myVariables.ACADEMICWATER;
}
trace(ODOMETER2);
LoadVars.onLoad is an event handler. It is called by LoadVars as soon as it finishes with the asynchronous load operation. It takes a boolean argument, indicating success or failure of the operation. It does not return anything.
LoadVars.onLoad documentation
In that function, you typically act upon the data you received, like storing and processing it. Here's a very simple example showing some basic use cases:
var ODOMETER2:String;
var myVariables = new LoadVars();
myVariables.load("myFile.txt");
myVariables.onLoad = function(success) {
trace(success);
ODOMETER2 = myVariables.ACADEMICWATER;
processResults();
}
function processResults() {
trace(ODOMETER2);
trace(myVariables.ACADEMICWATER);
}
// traces:
// true
// 3002
// 3002