Introduction to CasperJS (Fast Web UI Testing Framework)

18
CasperJS Intro to a Fast Testing Framework

description

Introduction to CasperJS (Fast Web UI Testing Framework)

Transcript of Introduction to CasperJS (Fast Web UI Testing Framework)

Page 1: Introduction to CasperJS (Fast Web UI Testing Framework)

CasperJSIntro to a Fast Testing Framework

Page 2: Introduction to CasperJS (Fast Web UI Testing Framework)

Agenda

1. What’s Casper2. Why Casper3. Installation4. Live demo5. Debugging6. Refactoring7. Advanced topics

Page 3: Introduction to CasperJS (Fast Web UI Testing Framework)

● Web UI testing framework● Similar to Selenium Watir● Built on top of PhantomJS● Started around Sep 2011● 3329 stars on Github

What’s CasperJS?

Page 4: Introduction to CasperJS (Fast Web UI Testing Framework)

Why Casper?

● Fasto No browser UIo Webkit based

● Execution Speed● Write front end UI testing using JS● Did I mention how fast test runs?

Page 5: Introduction to CasperJS (Fast Web UI Testing Framework)

How to install and run

● Requirements: python + phantomJS● Available on Windows, OSX, Linux● Install

o OSX: $ brew install casperjs --develo Node: $ npm install -g casperjs

● Runo $ casperjs test mytest.js

Page 6: Introduction to CasperJS (Fast Web UI Testing Framework)

test.assertTextExists("Todo App")

test.assert(1 == 1)

test.assertDoesntExist("#justin_bieber")

test.assertEquals(1+1, 2)

test.assertElementCount(".good_justin_biebe

r_songs", 0)

Assertions

Page 7: Introduction to CasperJS (Fast Web UI Testing Framework)

casper.click("#create_task")

casper.clickLabel("My link is beautiful",

"a");

casper.clickLabel("But my button is

sexier", "button");

Navigation clicks

Page 8: Introduction to CasperJS (Fast Web UI Testing Framework)

WTF is then()?

“then() basically adds a new navigation step in a stack. A step is a

javascript function which can do two different things:

- waiting for the previous step - if any - being executed

- waiting for a requested url and related page to load”

Niko, Creator of CasperJS

Source: http://stackoverflow.com/questions/13785670/passing-variable-from-this-evaluate-to-casper-then

Page 9: Introduction to CasperJS (Fast Web UI Testing Framework)

casper.waitForSelector("#sun", function() { alert("Go out and play"); });

casper.waitWhileSelector(".dark_clouds", function() { alert("Go out and

play"); });

casper.waitFor(function check() {

return this.evaluate(function() {

return $(".dark_clouds").length == 0

});}, function then() { alert("Go out and play!"); });

Waiting for element to load

Page 10: Introduction to CasperJS (Fast Web UI Testing Framework)

casper.waitUntilVisible("#natalie_portman", function() {});casper.waitWhileVisible(".one_direction_fans", function() {});casper.waitForSelectorTextChange("#inbox_unread_count", function() {});casper.start("http://why.univer.se/").waitForText("42", function() {});casper.start("http://foo/").waitForUrl(/login\.html$/, function() {});

casper.waitForResource("scarlett_johansson.png", function() {});casper.wait(1000, function() {}); // No!!

Waiting for element to load (cont.)

Page 11: Introduction to CasperJS (Fast Web UI Testing Framework)

casper.sendKeys('form#contact input#name',

'Chuck Norris');

casper.fill('form#contact', {

'name': 'Chuck Norris'}, true);

Type text, fill forms

Page 12: Introduction to CasperJS (Fast Web UI Testing Framework)

casper.evaluate(function() {

return $('.tasks').length;});

Execute Javascript

Page 13: Introduction to CasperJS (Fast Web UI Testing Framework)

casper.click("#link");

x = require('casper').selectXPath;

casper.click(x("//a[@id='link']"));

CSS3 VS XPath selectors

Page 14: Introduction to CasperJS (Fast Web UI Testing Framework)

Debugging Casper

● casper.echo(casper.getHTML()

● casper.capture("screenshot.png")

● casper.on("remote.message", function (msg)

{ casper.echo('Browser says: ' + msg) }● Verbose mode$ casper --verbose --log-level=debug● Use safari (for rendering issues)

Page 15: Introduction to CasperJS (Fast Web UI Testing Framework)

Refactoring

common.js

casper.createTask = function(title) {

this.sendKeys('#new-todo', title);

return this.sendKeys('#new-todo', this.page.event.key.Enter);

};

mytest.js

casper.createTask('Code feature 1');

Page 16: Introduction to CasperJS (Fast Web UI Testing Framework)

Advanced topics

1. Setup/tear down2. Support for file downloads, HTTP auth3. Support for browser back/forward, scroll, reload, viewport site4. Support for page events, alerts handling5. Support for Continuous Integration

a. Jenkins XUnit output$ casperjs test mytest.js --xunit=outcome.xmlb. GruntJS

6. Support for HTTPS$ casperjs tumblr.js --ssl-protocol=any

7. Weak support for Drag and Drop

Page 17: Introduction to CasperJS (Fast Web UI Testing Framework)

About me+us● Author: Herve Vu Roussel (see source at GitHub)● Find me at: [email protected]

This presentation was made for Javascript Ho Chi Minh City meetup group

You can find us at:● http://www.meetup.com/JavaScript-Ho-Chi-Minh-City/● https://www.facebook.com/JavaScriptHCMC● https://plus.google.com/u/0/communities/116105314977285194967● http://www.slideshare.net/JavascriptMeetup

Page 18: Introduction to CasperJS (Fast Web UI Testing Framework)

PLEASE CLAP