Skip to content

Commit

Permalink
feat: transmit attributes between tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
rtsun95 committed Jan 29, 2016
1 parent c41aebe commit 3505937
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
12 changes: 12 additions & 0 deletions src/bots/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@ var config = function (casper, pid) {
pid + '/' +
params.name);
},
get: function (params) {
if (params.attribute) {

var elementAttribute = casper.getElementAttribute(params.selector, params.attribute)
if (elementAttribute) {
log('got', params.attribute + ' of ' + params.selector, 'SUCCESS');
return elementAttribute;
}
return log('no ' + params.attribute +' for ' + params.selector, 'WARNING');

}
},
wait: function (params) {
var vals = ['wait for'];
Object.keys(params).forEach(function (key) {
Expand Down
28 changes: 27 additions & 1 deletion src/bots/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,27 @@ if (!casper.cli.has('tasks')) {
var opts = require(casper.cli.get('tasks'));
var tasks = opts.tasks;
var config = opts.config;
var store = {};

function replaceHandlebars (string) {
for (key in store) {
string = string.replace(new RegExp('{{' + key + '}}','g'), store[key]);
}
return string
}

function parseTask (task) {
var handlebarRegex = new RegExp('{{([^{}]+)}}', 'g');

for (param in task.params) {
var paramValue = task.params[param];
if (handlebarRegex.test(paramValue)) {
task.params[param] = replaceHandlebars(paramValue);
}
}

return task;
}

actions.navigate(config.url, function () {
casper.then(function () {
Expand All @@ -95,7 +116,12 @@ actions.navigate(config.url, function () {
if (task.type && actions[task.type]) {
casper.then(function () {
log('starting task', task, 'INFO_BAR');
return actions[task.type](task.params);
task = parseTask(task);
var response = actions[task.type](task.params);
if (task.type === "get") {
store[task.params.key] = response;
}
return response
});
}
});
Expand Down

0 comments on commit 3505937

Please sign in to comment.