Skip to content

Commit

Permalink
feat(get): make getVariable to be useable on any var
Browse files Browse the repository at this point in the history
  • Loading branch information
yoannmoinet committed Feb 9, 2016
1 parent 3d27677 commit b4f899f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/bots/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var actions = {

} else if (params.variable) {

returnValue = taskGet.getVariable(casper, params);
returnValue = taskGet.getVariable(casper, params.variable);
if (returnValue !== undefined) {
log('got global variable: ' + params.variable, 'SUCCESS');
return returnValue;
Expand Down
32 changes: 18 additions & 14 deletions src/bots/taskGet.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,26 +52,30 @@ function splitAccessors (variable) {
//Set the first accessor as the main object
//Get the value of window.object using casper.evaluate()
//Access continuously to the next property until the end of the list
function getVariable (casper, params) {
function getVariable (casper, variable, variableValue) {
var object;
var variableValue;
var accessors = splitAccessors(params.variable);
var accessors = splitAccessors(variable);

if (accessors) {
object = accessors.shift();
if (!accessors) {
return;
}

object = accessors.shift();
if (variableValue === undefined) {
variableValue = casper.evaluate(function (object) {
return window[object];
}, object);

accessors.forEach(function (property) {
if (variableValue === undefined) {
return;
}
variableValue = variableValue[property];
});
return variableValue;
} else {
variableValue = variableValue[object];
}
return;

accessors.forEach(function (property) {
if (variableValue === undefined) {
return;
}
variableValue = variableValue[property];
});
return variableValue;
}

//This function retrieves the first attribute or text of the selector
Expand Down

0 comments on commit b4f899f

Please sign in to comment.