Top Banner
http://x-team.com PhantomJS CasperJS
14

CasperJS and PhantomJS for Automated Testing

Apr 08, 2017

Download

Software

X-Team
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CasperJS and PhantomJS for Automated Testing

http://x-team.com

PhantomJS CasperJS

Page 2: CasperJS and PhantomJS for Automated Testing

http://x-team.com

PhantomJS

• Headless version of Webkit

• Window-less, command line web browser

• Programmable through JavaScript

• Supports WebDriver out of the box

Page 3: CasperJS and PhantomJS for Automated Testing

http://x-team.com

Installation

• Install Node.js

• sudo npm install -g phantomjs

• (http://phantomjs.org/)

Page 4: CasperJS and PhantomJS for Automated Testing

How to use it?

var page = require('webpage').create();

page.open('http://www.google.com/', function (status) {

// Page is loaded. Output content.

console.log(page.content);

phantom.exit();

});

Page 5: CasperJS and PhantomJS for Automated Testing

How to use it?

var page = require('webpage').create();

page.open('http://www.google.com/', function (status) {

// Page is loaded. Output content.

console.log(page.content);

phantom.exit();

});

> phantomjs myscript.js

Page 6: CasperJS and PhantomJS for Automated Testing

http://x-team.com

What else can i do ?

• Inject/execute arbitrary JS code

• Perform actions

• Take screenshots

• Monitor performance

Page 7: CasperJS and PhantomJS for Automated Testing

PhantomJS is not a test framework. It’s a virtual browser.

Page 8: CasperJS and PhantomJS for Automated Testing

http://x-team.com

CasperJS

• API & test framework layer on top of PhantomJS

• Provides all the things you’re used to:

• Clicking, Typing, Waiting, Assertions, etc.

• Events, screenshots (even specific elements), & more.

Page 9: CasperJS and PhantomJS for Automated Testing

http://x-team.com

Installation

• Install Node.js

• sudo npm install -g casperjs

• (http://casperjs.org/)

Page 10: CasperJS and PhantomJS for Automated Testing

How to use it?

var casper = require('casper').create();

casper.start('http://google.com/', function() {

this.fill('form[action="/search"]', { 'q': 'ghost' });

this.click('#gbqfba');

});

casper.then(function() {

this.echo(this.getTitle());

});

casper.run();

Page 11: CasperJS and PhantomJS for Automated Testing

How to use it?var casper = require('casper').create();

casper.start('http://google.com/', function() {

this.fill('form[action="/search"]', { 'q': 'ghost' });

this.click('#gbqfba');

});

casper.then(function() {

this.echo(this.getTitle());

});

casper.run();

#> casperjs myscript.js

Page 12: CasperJS and PhantomJS for Automated Testing

How to use it?

var casper = require('casper').create();

casper.test.begin('Google tests', 1, function(test) {

casper.start('http://www.google.com/', function() {

test.assertTitleMatch(/Google/, 'Title contains Google');

}).run(function() {

test.done();

});

});

#> casperjs myscript.js

Page 13: CasperJS and PhantomJS for Automated Testing

References

• http://docs.casperjs.org/en/latest/

• http://phantomjs.org/documentation/

• https://github.com/BBC-News/wraith

Page 14: CasperJS and PhantomJS for Automated Testing

http://x-team.comCrash Courses