SlideShare une entreprise Scribd logo
1  sur  15
CasperJS
Howard 2013/12/22
CasperJS

CasperJS is an open source navigation scripting & testing
utility written in Javascript for the PhantomJS WebKit
headless browser and SlimerJS (Gecko).
It eases the process of defining a full navigation scenario
and provides useful high-level functions, methods &
syntactic sugar
Doing common tasks
 such as:
 defining & ordering browsing navigation steps
 filling & submitting forms

 clicking & following links
 capturing screenshots of a page (or part of it)
 testing remote DOM
 logging events
 downloading resources, including binary ones
 writing functional test suites, saving results as JUnit XML
 scraping Web contents
Installation

 Prerequisites
 PhantomJS
 PhantomJS is a headless WebKit scriptable with a JavaScript API.
 It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and
SVG.
 http://phantomjs.org/download.html

 CasperJS
目標

 列舉出目前 IPTECH TW
的目前可檢索的站台
A minimal scraping script
 What did we just do?
 we created a new Casper instance

 we started it and opened http://casperjs.org/
 once the page has been loaded, we asked to print
the title of that webpage (the content of its <title>
tag)
 then we opened another url, http://phantomjs.org/

 once the new page has been loaded, we asked to
print its title too
 we executed the whole process
start(), then(), run(), exit()


start()
 Signature: start(String url [, Function then])
 Configures and starts Casper, then open the provided url and optionally adds the step provided by the then argument



then()
 Signature: then(Function then)



run()
 Signature: run(fn onComplete[, int time])
 Runs the whole suite of steps and optionally executes a callback when they’ve all been done. Obviously, calling this method is
mandatory in order to run the Casper navigation suite.



exit()
 Signature: exit([int status])
 Exits PhantomJS with an optional exit status code.



Note that the current Casper instance automatically binds the this keyword for you within step functions
start(), then(), run(), exit()
start(), then(), run(), exit()

?
casper module – fill
 Signature: fill(String selector, Object values[, Boolean submit])
 Fills the fields of a form with given values and optionally submits it.
 Fields are referenced by their name attribute.
click
 click(Number x, Number y)
 click(String selector)
 Performs a click on the first element found matching the provided selector expression or
at given coordinates if two numbers are passed

 selector expression
 CSS3 selector strings
 Xpath expressions
evaluate
 Signature: evaluate(Function fn[, arg1[, arg2[, …]]])
 Evaluates an expression in the current page DOM context
Understanding evaluate()
 Think of the evaluate() method as a gate between the CasperJS environment and the
one of the page you have opened;
 Every time you pass a closure to evaluate(), you’re entering the page and execute code
as if you were using the browser console.
waitForSelector
 Signature: waitForSelector(String selector[, Function then, Function onTimeout, Number
timeout])
 Waits until an element matching the provided selector expression exists in remote DOM to
process any next step
references
 http://docs.casperjs.org/en/latest/quickstart.html
 http://docs.casperjs.org/en/latest/modules/index.html
 http://docs.casperjs.org/en/latest/modules/casper.html#start
 http://docs.casperjs.org/en/latest/modules/casper.html#then
 http://docs.casperjs.org/en/latest/modules/casper.html#run
 http://docs.casperjs.org/en/latest/modules/casper.html#exit

 http://docs.casperjs.org/en/latest/modules/casper.html#fill
 http://docs.casperjs.org/en/latest/selectors.html
 http://docs.casperjs.org/en/latest/modules/mouse.html#click

Contenu connexe

Tendances

Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"Fwdays
 
Continuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptContinuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptLars Thorup
 
System webpack-jspm
System webpack-jspmSystem webpack-jspm
System webpack-jspmJesse Warden
 
Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)Deutsche Post
 
ServiceWorker: New game changer is coming!
ServiceWorker: New game changer is coming!ServiceWorker: New game changer is coming!
ServiceWorker: New game changer is coming!Chang W. Doh
 
Роман Лютиков "Web Apps Performance & JavaScript Compilers"
Роман Лютиков "Web Apps Performance & JavaScript Compilers"Роман Лютиков "Web Apps Performance & JavaScript Compilers"
Роман Лютиков "Web Apps Performance & JavaScript Compilers"Fwdays
 
Running JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java WorldRunning JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java Worldirbull
 
NodeJS: n00b no more
NodeJS: n00b no moreNodeJS: n00b no more
NodeJS: n00b no moreBen Peachey
 
Vagrant plugin development intro
Vagrant plugin development introVagrant plugin development intro
Vagrant plugin development introBudh Ram Gurung
 
Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS偉格 高
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsBudh Ram Gurung
 
Javascript asynchronous
Javascript asynchronousJavascript asynchronous
Javascript asynchronouskang taehun
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverMongoDB
 
Node4J: Running Node.js in a JavaWorld
Node4J: Running Node.js in a JavaWorldNode4J: Running Node.js in a JavaWorld
Node4J: Running Node.js in a JavaWorldIan Bull
 
Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...
Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...
Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...Rob Friesel
 

Tendances (20)

Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
Игорь Фесенко "Web Apps Performance & JavaScript Compilers"
 
Continuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptContinuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScript
 
System webpack-jspm
System webpack-jspmSystem webpack-jspm
System webpack-jspm
 
Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)Javascript Test Automation Workshop (21.08.2014)
Javascript Test Automation Workshop (21.08.2014)
 
ServiceWorker: New game changer is coming!
ServiceWorker: New game changer is coming!ServiceWorker: New game changer is coming!
ServiceWorker: New game changer is coming!
 
Роман Лютиков "Web Apps Performance & JavaScript Compilers"
Роман Лютиков "Web Apps Performance & JavaScript Compilers"Роман Лютиков "Web Apps Performance & JavaScript Compilers"
Роман Лютиков "Web Apps Performance & JavaScript Compilers"
 
Running JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java WorldRunning JavaScript Efficiently in a Java World
Running JavaScript Efficiently in a Java World
 
NodeJS: n00b no more
NodeJS: n00b no moreNodeJS: n00b no more
NodeJS: n00b no more
 
Vagrant plugin development intro
Vagrant plugin development introVagrant plugin development intro
Vagrant plugin development intro
 
Node ppt
Node pptNode ppt
Node ppt
 
Module, AMD, RequireJS
Module, AMD, RequireJSModule, AMD, RequireJS
Module, AMD, RequireJS
 
Nodejs Intro Part One
Nodejs Intro Part OneNodejs Intro Part One
Nodejs Intro Part One
 
Nodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web ApplicationsNodejs Intro - Part2 Introduction to Web Applications
Nodejs Intro - Part2 Introduction to Web Applications
 
Javascript asynchronous
Javascript asynchronousJavascript asynchronous
Javascript asynchronous
 
ECMAScript 6 and the Node Driver
ECMAScript 6 and the Node DriverECMAScript 6 and the Node Driver
ECMAScript 6 and the Node Driver
 
Node4J: Running Node.js in a JavaWorld
Node4J: Running Node.js in a JavaWorldNode4J: Running Node.js in a JavaWorld
Node4J: Running Node.js in a JavaWorld
 
Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...
Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...
Like a Genie from a Lamp: Headless JavaScript Unit Testing with Jasmine and P...
 
Zombiejs
ZombiejsZombiejs
Zombiejs
 
Nodejs intro
Nodejs introNodejs intro
Nodejs intro
 
Ansible 202 - sysarmy
Ansible 202 - sysarmyAnsible 202 - sysarmy
Ansible 202 - sysarmy
 

En vedette

Acceptance Testing - Automatisierte Web App Tests mit CasperJS
Acceptance Testing - Automatisierte Web App Tests mit CasperJSAcceptance Testing - Automatisierte Web App Tests mit CasperJS
Acceptance Testing - Automatisierte Web App Tests mit CasperJSBjörn Wilmsmann
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineAnup Singh
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmineTimothy Oxley
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma Christopher Bartling
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingLars Thorup
 

En vedette (7)

Acceptance Testing - Automatisierte Web App Tests mit CasperJS
Acceptance Testing - Automatisierte Web App Tests mit CasperJSAcceptance Testing - Automatisierte Web App Tests mit CasperJS
Acceptance Testing - Automatisierte Web App Tests mit CasperJS
 
JAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & JasmineJAVASCRIPT Test Driven Development & Jasmine
JAVASCRIPT Test Driven Development & Jasmine
 
Intro to testing Javascript with jasmine
Intro to testing Javascript with jasmineIntro to testing Javascript with jasmine
Intro to testing Javascript with jasmine
 
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma JavaScript Test-Driven Development with Jasmine 2.0 and Karma
JavaScript Test-Driven Development with Jasmine 2.0 and Karma
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit Testing
 
Symfony Best Practices
Symfony Best PracticesSymfony Best Practices
Symfony Best Practices
 
CasperJS
CasperJSCasperJS
CasperJS
 

Similaire à CasperJS

Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"
Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"
Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"Provectus
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationAjax Experience 2009
 
Jackson beyond JSON: XML, CSV
Jackson beyond JSON: XML, CSVJackson beyond JSON: XML, CSV
Jackson beyond JSON: XML, CSVTatu Saloranta
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
 
KSQL - Stream Processing simplified!
KSQL - Stream Processing simplified!KSQL - Stream Processing simplified!
KSQL - Stream Processing simplified!Guido Schmutz
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Developmentvito jeng
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorialKat Roque
 
JBossWS Project by Alessio Soldano
JBossWS Project by Alessio SoldanoJBossWS Project by Alessio Soldano
JBossWS Project by Alessio SoldanoJava User Group Roma
 
April 2010 - JBoss Web Services
April 2010 - JBoss Web ServicesApril 2010 - JBoss Web Services
April 2010 - JBoss Web ServicesJBug Italy
 
Calabash-android
Calabash-androidCalabash-android
Calabash-androidAdnan8990
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineRicardo Silva
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and MonoidsHugo Gävert
 
Scalable Angular 2 Application Architecture
Scalable Angular 2 Application ArchitectureScalable Angular 2 Application Architecture
Scalable Angular 2 Application ArchitectureFDConf
 
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Codemotion
 

Similaire à CasperJS (20)

Casper js
Casper jsCasper js
Casper js
 
Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"
Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"
Василевский Илья (Fun-box): "автоматизация браузера при помощи PhantomJS"
 
Laurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus PresentationLaurens Van Den Oever Xopus Presentation
Laurens Van Den Oever Xopus Presentation
 
Jackson beyond JSON: XML, CSV
Jackson beyond JSON: XML, CSVJackson beyond JSON: XML, CSV
Jackson beyond JSON: XML, CSV
 
iOS build that scales
iOS build that scalesiOS build that scales
iOS build that scales
 
J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
 
JavaScript
JavaScriptJavaScript
JavaScript
 
Scala at Netflix
Scala at NetflixScala at Netflix
Scala at Netflix
 
KSQL - Stream Processing simplified!
KSQL - Stream Processing simplified!KSQL - Stream Processing simplified!
KSQL - Stream Processing simplified!
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Development
 
Ajax tutorial
Ajax tutorialAjax tutorial
Ajax tutorial
 
JBossWS Project by Alessio Soldano
JBossWS Project by Alessio SoldanoJBossWS Project by Alessio Soldano
JBossWS Project by Alessio Soldano
 
April 2010 - JBoss Web Services
April 2010 - JBoss Web ServicesApril 2010 - JBoss Web Services
April 2010 - JBoss Web Services
 
Calabash-android
Calabash-androidCalabash-android
Calabash-android
 
Event-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 EngineEvent-driven IO server-side JavaScript environment based on V8 Engine
Event-driven IO server-side JavaScript environment based on V8 Engine
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
 
Full Stack Scala
Full Stack ScalaFull Stack Scala
Full Stack Scala
 
Scalable Angular 2 Application Architecture
Scalable Angular 2 Application ArchitectureScalable Angular 2 Application Architecture
Scalable Angular 2 Application Architecture
 
[2015/2016] JavaScript
[2015/2016] JavaScript[2015/2016] JavaScript
[2015/2016] JavaScript
 
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
Massimo Artizzu - The tricks of Houdini: a magic wand for the future of CSS -...
 

Plus de LearningTech

Plus de LearningTech (20)

vim
vimvim
vim
 
PostCss
PostCssPostCss
PostCss
 
ReactJs
ReactJsReactJs
ReactJs
 
Docker
DockerDocker
Docker
 
Semantic ui
Semantic uiSemantic ui
Semantic ui
 
node.js errors
node.js errorsnode.js errors
node.js errors
 
Process control nodejs
Process control nodejsProcess control nodejs
Process control nodejs
 
Expression tree
Expression treeExpression tree
Expression tree
 
SQL 效能調校
SQL 效能調校SQL 效能調校
SQL 效能調校
 
flexbox report
flexbox reportflexbox report
flexbox report
 
Vic weekly learning_20160504
Vic weekly learning_20160504Vic weekly learning_20160504
Vic weekly learning_20160504
 
Reflection &amp; activator
Reflection &amp; activatorReflection &amp; activator
Reflection &amp; activator
 
Peggy markdown
Peggy markdownPeggy markdown
Peggy markdown
 
Node child process
Node child processNode child process
Node child process
 
20160415ken.lee
20160415ken.lee20160415ken.lee
20160415ken.lee
 
Peggy elasticsearch應用
Peggy elasticsearch應用Peggy elasticsearch應用
Peggy elasticsearch應用
 
Expression tree
Expression treeExpression tree
Expression tree
 
Vic weekly learning_20160325
Vic weekly learning_20160325Vic weekly learning_20160325
Vic weekly learning_20160325
 
D3js learning tips
D3js learning tipsD3js learning tips
D3js learning tips
 
git command
git commandgit command
git command
 

CasperJS

  • 2. CasperJS CasperJS is an open source navigation scripting & testing utility written in Javascript for the PhantomJS WebKit headless browser and SlimerJS (Gecko). It eases the process of defining a full navigation scenario and provides useful high-level functions, methods & syntactic sugar
  • 3. Doing common tasks  such as:  defining & ordering browsing navigation steps  filling & submitting forms  clicking & following links  capturing screenshots of a page (or part of it)  testing remote DOM  logging events  downloading resources, including binary ones  writing functional test suites, saving results as JUnit XML  scraping Web contents
  • 4. Installation  Prerequisites  PhantomJS  PhantomJS is a headless WebKit scriptable with a JavaScript API.  It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.  http://phantomjs.org/download.html  CasperJS
  • 5. 目標  列舉出目前 IPTECH TW 的目前可檢索的站台
  • 6. A minimal scraping script  What did we just do?  we created a new Casper instance  we started it and opened http://casperjs.org/  once the page has been loaded, we asked to print the title of that webpage (the content of its <title> tag)  then we opened another url, http://phantomjs.org/  once the new page has been loaded, we asked to print its title too  we executed the whole process
  • 7. start(), then(), run(), exit()  start()  Signature: start(String url [, Function then])  Configures and starts Casper, then open the provided url and optionally adds the step provided by the then argument  then()  Signature: then(Function then)  run()  Signature: run(fn onComplete[, int time])  Runs the whole suite of steps and optionally executes a callback when they’ve all been done. Obviously, calling this method is mandatory in order to run the Casper navigation suite.  exit()  Signature: exit([int status])  Exits PhantomJS with an optional exit status code.  Note that the current Casper instance automatically binds the this keyword for you within step functions
  • 10. casper module – fill  Signature: fill(String selector, Object values[, Boolean submit])  Fills the fields of a form with given values and optionally submits it.  Fields are referenced by their name attribute.
  • 11. click  click(Number x, Number y)  click(String selector)  Performs a click on the first element found matching the provided selector expression or at given coordinates if two numbers are passed  selector expression  CSS3 selector strings  Xpath expressions
  • 12. evaluate  Signature: evaluate(Function fn[, arg1[, arg2[, …]]])  Evaluates an expression in the current page DOM context
  • 13. Understanding evaluate()  Think of the evaluate() method as a gate between the CasperJS environment and the one of the page you have opened;  Every time you pass a closure to evaluate(), you’re entering the page and execute code as if you were using the browser console.
  • 14. waitForSelector  Signature: waitForSelector(String selector[, Function then, Function onTimeout, Number timeout])  Waits until an element matching the provided selector expression exists in remote DOM to process any next step
  • 15. references  http://docs.casperjs.org/en/latest/quickstart.html  http://docs.casperjs.org/en/latest/modules/index.html  http://docs.casperjs.org/en/latest/modules/casper.html#start  http://docs.casperjs.org/en/latest/modules/casper.html#then  http://docs.casperjs.org/en/latest/modules/casper.html#run  http://docs.casperjs.org/en/latest/modules/casper.html#exit  http://docs.casperjs.org/en/latest/modules/casper.html#fill  http://docs.casperjs.org/en/latest/selectors.html  http://docs.casperjs.org/en/latest/modules/mouse.html#click