Vote count: 0
I'm using this casperjs nodejs app: http://ift.tt/2lw7dI1
I've gotten my code to work locally and when uploaded to Lambda, but I need to wrap my code inside exports.handler so I can pass data to and from the function from the API gateway. (I already do this with other functions that are not using this app.) When I put my code inside exports.handler it fails. I have tested adding the event data to a test event inside lambda and I've tried hardcoding it. Both fail when my code is inside exports.handler. Why is exports.handler breaking this code?
Working code w/o exports.handler:
var ua = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1'
var casper = require('casper').create({
viewportSize: {width: 768, height: 1024},
userAgent: ua
});
var login = {email: 'fakeemail@gmail.com', pw: 'fakepw'}
var usrObj = {
succ: {},
err: []
};
//exit function
function exit() {
setTimeout(function() {
casper.exit();
casper.bypass(1);
}, 10);
}
//success & error message function
function message(dat) {
if (dat === 'credentials') {
console.log('Error: Login credentials are missing');
return exit();
}
else if (dat) {
console.log(dat);
return exit();
}
if (usrObj['err'].length > 0) {
console.log('Error not empty...');
console.log(usrObj.err);
return exit();
}
else if (usrObj['succ']) {
console.log('Success not empty...');
console.log(JSON.stringify(usrObj.succ));
return exit();
}
}
//trim login credentials
login.email = login.email.trim();
login.pw = login.pw.trim();
if (login.email && login.pw) {
casper.start('http://ift.tt/1Sq8L30');
casper.waitForSelector('form#login_form',
function success() {
this.echo(this.getCurrentUrl());
this.sendKeys('form#login_form input[name="email"]', login.email);
this.sendKeys('form#login_form input[name="password"]', login.pw);
this.click('form#login_form input[type=submit][value="Log in with email"]');
},
function fail() {
message('Error with Vimeo [page not loading]')
}
);
casper.waitForSelector('#page_header>h1>a',
function success() {
this.echo(this.getCurrentUrl());
usrObj['succ']['uname'] = this.getHTML('span.topnav_user_name');
usrObj['succ']['profile'] = this.getElementAttribute('li.topnav_user_profile>a', 'href');
var test = [];
if (!usrObj.succ.uname) {test.push('Username not retrieved')}
if (!usrObj.succ.profile) {test.push('Profile link not retrieved')}
if (test.length > 0) {message(test.join('<br />'));}
//else {message();}
},
function fail() {
message('Login not successful');
}
);
casper.thenOpen('http://ift.tt/2lwmo3V',
function success() {
this.echo('Stacey David Profile: ' + this.getTitle());
this.echo(this.getCurrentUrl());
var finish = function() {
usrObj['succ']['foll'] = true;
message();
}
//var foll = this.getHTML('button[data-fatal-attraction="container:profile_page|component:follow"] > span');
var foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
this.click('button[data-fatal-attraction="container:profile_page|component:follow"]');
setTimeout(function() {
foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
message('Can\'t follow SD');
}
else {finish();}
}, 250);
}
else {
finish();
}
},
function fail() {
message('Not going to Stacey David profile page.');
}
);
casper.run();
}
else {message('credentials');}
Code failing inside exports.handler:
var ua = 'Mozilla/5.0 (iPad; CPU OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1';
var casper = require('casper').create({
viewportSize: {width: 768, height: 1024},
userAgent: ua//,
//verbose: true,
//logLevel: 'debug'
});
exports.handler = function (event, context) {
var login = {};
var usrObj = {
succ: {},
err: []
};
//exit function
var exit = function() {
setTimeout(function() {
casper.page.close();
casper.exit();
casper.bypass(1);
}, 10);
};
//success & error message function
var message = function(dat) {
if (dat === 'credentials') {
usrObj['err'].push('Error: Login credentials are missing');
}
else if (dat) {
usrObj['err'].push(dat);
}
if (usrObj['err'].length > 0) {
console.log('Error not empty...');
console.log(JSON.stringify(usrObj.err));
context.fail(JSON.stringify(usrObj.err));
return exit();
}
else if (usrObj['succ']) {
console.log('Success not empty...');
console.log(JSON.stringify(usrObj.succ));
context.succeed(JSON.stringify(usrObj.succ));
return exit();
}
};
//trim login credentials
login.email = event.email;
login.pw = event.pw;
if (login.email && login.pw) {
casper.start('http://ift.tt/1Sq8L30');
casper.waitForSelector('form#login_form',
function success() {
this.echo(this.getCurrentUrl());
this.sendKeys('form#login_form input[name="email"]', login.email);
this.sendKeys('form#login_form input[name="password"]', login.pw);
this.click('form#login_form input[type=submit][value="Log in with email"]');
},
function fail() {
message('Error with Vimeo [page not loading]')
}
);
casper.waitForSelector('#page_header>h1>a',
function success() {
this.echo(this.getCurrentUrl());
usrObj['succ']['uname'] = this.getHTML('span.topnav_user_name');
usrObj['succ']['profile'] = this.getElementAttribute('li.topnav_user_profile>a', 'href');
var test = [];
if (!usrObj.succ.uname) {test.push('Username not retrieved')}
if (!usrObj.succ.profile) {test.push('Profile link not retrieved')}
if (test.length > 0) {message(test.join('<br />'));}
//else {message();}
},
function fail() {
message('Login not successful');
}
);
casper.thenOpen('http://ift.tt/2lwmo3V',
function success() {
this.echo('Stacey David Profile: ' + this.getTitle());
this.echo(this.getCurrentUrl());
var finish = function() {
usrObj['succ']['foll'] = true;
message();
}
var foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
this.click('button[data-fatal-attraction="container:profile_page|component:follow"]');
setTimeout(function() {
foll = this.getElementAttribute('button[data-fatal-attraction="container:profile_page|component:follow"] > svg', 'viewBox');
if (foll === '0 0 10 10') {
message('Can\'t follow SD');
}
else {finish();}
}, 250);
}
else {
finish();
}
},
function fail() {
message('Not going to Stacey David profile page.');
}
);
casper.run();
}
else {message('credentials');}
};
Lambda response:
Body
[]
Logs:
Calling casperJS: /var/task/node_modules/casperjs/bin/casperjs [ '/var/task/casperjs-script.js' ] { PHANTOMJS_EXECUTABLE: '/var/task/phantomjs' }
child process exited with code 1
asked 31 secs ago
Why does this code error on Lambda when wrapped inside exports.handler?
Aucun commentaire:
Enregistrer un commentaire