我使用這個casperjs應用的NodeJS:https://github.com/narainsagar/node-casperjs-aws-lambda爲什麼包裝在exports.handler中時,此代碼在Lambda上出錯?
我已經得到我的代碼在本地工作,當上傳到LAMBDA,但我需要我的包裏面的代碼exports.handler所以我可以傳遞數據和從來自API網關的功能。 (我已經用其他不使用這個應用程序的函數來做這件事。)當我把我的代碼放到exports.handler中時,它失敗了。我已經測試了將事件數據添加到lambda中的測試事件中,並且我嘗試了對其進行硬編碼。當我的代碼位於exports.handler內時,兩者都失敗。爲什麼exports.handler會破壞這段代碼?
工作代碼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//,
//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('https://vimeo.com/log_in');
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('https://vimeo.com/staceydavidgearz',
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響應:
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: '[email protected]', 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('https://vimeo.com/log_in');
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('https://vimeo.com/staceydavidgearz',
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');}
代碼中exports.handler失敗
身體
[]
日誌:
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
我改變exports.handler到casper.handler和運行,直到超時。在我幾乎立即獲得日誌之前。 – TDave00
挖掘到這個應用程序的index.js文件後,我發現了什麼。 http://stackoverflow.com/questions/42152784/how-to-retrieve-event-data-of-exports-handler-from-another-script – TDave00