lindiwo 2013-05-25
SuperPlan(11)WinnerSellerWebClient-PhantomJS
13.PhantomJS
13.1Installation
DownloadMACversionfromherehttp://phantomjs.org/download.html
Andunzipthefiletoworkingdirectory.
>cd/Users/carl/tool/phantomjs-1.9.0-macosx
Createasoftlinktothe/optdirectory
>sudoln-s/Users/carl/tool/phantomjs-1.9.0-macosx/opt/phantomjs-1.9.0
>sudoln-s/opt/phantomjs-1.9.0/opt/phantomjs
Trytomodifythepathinthesystem
>vi~/.profile
exportPATH=/opt/phantomjs/bin:$PATH
>.~/.profile
Verificationtheinstallation.
>phantomjs--version
1.9.0
13.2QuickStart
HelloWorldExample
>vihello.js
console.log('sillycat');
phantom.exit();
>phantomjshello.js
sillycat
Itisveryimportanttocallphantom.exitatsomepointinthescript,otherwisePhantomJSwillnotbeterminatedatall.
PageLoading
>vipageloading.js
varpage=require('webpage').create();
page.open('http://www.google.com',function(){
page.render('google.png');
phantom.exit();
});
Itisreallymagicforjavasciptfunctionallikethatwithbuilt-inwebkit.
Itwillvisitthegooglehomepageandrendertoapngfile.Itislikeascreenshotofthepage.
>viloadspeed.js
varpage=require('webpage').create(),
system=require('system'),
t,address;
if(system.args.length===1){
console.log('Usage:loadspeed.js<someURL>');
phantom.exit();
}
t=Date.now();
address=system.args[1];
page.open(address,function(status){
if(status!=='success'){
console.log('FAILtoloadtheaddress');
}else{
t=Date.now()-t;
console.log('Loadingtime'+t+'msec');
}
phantom.exit();
});
>phantomjsloadspeed.jshttp://www.taobao.com
CodeEvaluation
ShowtheTitleofDocument
>vipagetitle.js
varpage=require('webpage').create();
varsystem=require('system');
varurl;
if(system.args.length===1){
console.log('Usage:phantomshowtitle.js<someURL>');
phantom.exit();
}
url=system.args[1];
page.open(url,function(status){
vartitle=page.evaluate(function(){
returndocument.title;
});
console.log('Pagetitleis'+title);
phantom.exit();
});
>phantomjsshowtitle.jshttp://www.digby.com
NetLog
>vinetlog.js
varpage=require('webpage').create(),
system=require('system'),
address;
if(system.args.length===1){
console.log('Usage:netlog.js<someURL>');
phantom.exit(1);
}else{
address=system.args[1];
page.onResourceRequested=function(req){
console.log('requested:'+JSON.stringify(req,undefined,4));
};
page.onResourceReceived=function(res){
console.log('received:'+JSON.stringify(res,undefined,4));
};
page.open(address,function(status){
if(status!=='success'){
console.log('FAILtoloadtheaddress');
}
phantom.exit();
});
}
>phantomjsnetlog.jshttp://www.google.com
Andtherearealotofexamplesforfurtherreading.
https://github.com/ariya/phantomjs/wiki/Examples
https://github.com/ariya/phantomjs/wiki/Headless-Testing
https://github.com/ariya/phantomjs/wiki/Screen-Capture
https://github.com/ariya/phantomjs/wiki/Page-Automation
https://github.com/ariya/phantomjs/wiki/Network-Monitoring
14.Integration(Backbone+Require+Jasmine+Phantom+Grunt+Bootstrap)
Firstofall,getthesampleproject
>gitclonehttps://github.com/ghiden/backbone-requirejs-jasmine-phantomjs-grunt-setup.git
>cdbackbone-requirejs-jasmine-phantomjs-grunt-setup
Itisnotrunning,becausetheversionofgrunt.Ineedtospeedmoretimeonthat.
comesoon...
15.BackBone
comesoon…
16.Jasmine
comesoon...
References:
https://github.com/mohitjain/learning_basics_backbone
gruntsample
http://gruntjs.com/sample-gruntfile
integration
http://hdnrnzk.me/2013/01/10/backbone-requirejs-jasmine-phantomjs-and-grunt/
https://github.com/ghiden/backbone-requirejs-jasmine-phantomjs-grunt-setup
webpagetesting
http://www.webryan.net/2013/02/web-page-test-based-on-phontomjs/
phantom
http://phantomjs.org/
https://github.com/ariya/phantomjs/wiki/Quick-Start