testing ext js and sencha touch

67
Testing Ext JS & Sencha Touch 2012 Mats Bryntse @bryntum

Upload: mats-bryntse

Post on 11-May-2015

9.602 views

Category:

Technology


4 download

DESCRIPTION

My SourceDevCon 2012 presentation slides.

TRANSCRIPT

Page 1: Testing Ext JS and Sencha Touch

Testing Ext JS & Sencha Touch

2012 Mats Bryntse@bryntum

Page 2: Testing Ext JS and Sencha Touch

• Why test your JavaScript?

• Testing Methodologies

• Introducing Siesta

• Writing testable JS

• Automating tests

Contents

Page 3: Testing Ext JS and Sencha Touch

var me = { name : ”Mats Bryntse”, age : 35+, lives : ”Helsingborg, Sweden”, does : ”Runs Bryntum”, site : ”www.bryntum.com”, twitter : ”@bryntum”};

About me

Page 4: Testing Ext JS and Sencha Touch

What we do

Ext JS/ST Components

Siesta (JS Test Tool)

Page 5: Testing Ext JS and Sencha Touch

First, a quick raise of hands:

How many of you...

Page 6: Testing Ext JS and Sencha Touch

• have a frontend test suite?

• have frontend test suite as part of your CI?

• run your test suite in all major browsers?

• have zero or fewer frontend tests for your app?

How many of you...

Page 7: Testing Ext JS and Sencha Touch

Why test your JavaScript?

Page 8: Testing Ext JS and Sencha Touch

A typical web app...

Interwebs

http://www.app.com

Page 9: Testing Ext JS and Sencha Touch

The backend

• Single controlled platform

• Simple to test and refactor

• Good IDEs and tools

C#Java

PHP

Page 10: Testing Ext JS and Sencha Touch

The frontend

• Multiple platforms & versions (Mac, Windows XP/Vista/7/8, Linux...)

• Multiple browser versions

• Hard to refactor

• JavaScript support in IDEs is still !== awesome

Page 11: Testing Ext JS and Sencha Touch

Conclusion

Too easy to #FAIL as a frontend developer

Testing helps you keep fails to a minimum.

Page 12: Testing Ext JS and Sencha Touch

But...

”... my code is bug free”

”...testing takes time away from adding new features (+ new bugs)”

”...it’s QA’s job to test”

”... it’s boring and I’ll quit my job”

Page 13: Testing Ext JS and Sencha Touch

A good investment

• Writing tests === investment in your code

• Takes time

• Will pay you back, though not instantly

Page 14: Testing Ext JS and Sencha Touch

Scenario: A nasty bug was found

3 options

Page 15: Testing Ext JS and Sencha Touch

[panic]

Page 16: Testing Ext JS and Sencha Touch

[patch it]

Page 17: Testing Ext JS and Sencha Touch

[proper fix]

Page 18: Testing Ext JS and Sencha Touch

A test suite...

=> confidence in your code

=> serves as extra documentation

=> easier refactoring

=> bugs happen once, easier to find

=======> quality of sleep

Page 19: Testing Ext JS and Sencha Touch

Code handover

• Test cases are really useful when handing over responsibility for a JS module

• Developer Bob quits his job. New guy gets responsibility of his JS code.

• How will new guy know what parts of the codebase is safe to change & refactor?

Page 20: Testing Ext JS and Sencha Touch

New guy studies codebase

/** * When I wrote this, only God and I understood what I was doing * Now, God only knows **/

/* I am not sure if we need this, but too scared to delete. */

// drunk, fix later

// This is my last day...

application.js

Page 21: Testing Ext JS and Sencha Touch

Code handover

New guy, scared

Page 22: Testing Ext JS and Sencha Touch

Code handover

• Without test suite, will new guy feel good about making major changes?

• Only minor cosmetic changes on the surface.

• System accumulates cruft over time. • Sounds familiar?

Page 23: Testing Ext JS and Sencha Touch

JS Testing Methodologies

Page 24: Testing Ext JS and Sencha Touch

Unit testing

Integration testing

Functional testing

Page 25: Testing Ext JS and Sencha Touch

Unit testing

• Testing the API of a single isolated ’unit’

• Typically pure JS, non UI / DOM• verify set/get method of a Model• verify date manipulation logic

• Many tools available

Page 26: Testing Ext JS and Sencha Touch

Unit testing

• Siesta

• Jasmine

• QUnit (jQuery)

• Google js-test (V8)

Page 27: Testing Ext JS and Sencha Touch

Integration testing

• Testing integration between units.

• E.g. verify cell editing plugin works in GridPanel

Page 28: Testing Ext JS and Sencha Touch

Functional Web testing

• UI testing of a component or an application as a whole.

• Send commands to the web page and evaluate result.

• ’click button’, ’type into login field’, ...

Page 29: Testing Ext JS and Sencha Touch

Functional Web testing

• Siesta

• Selenium

• JsTestDriver

Page 30: Testing Ext JS and Sencha Touch

Interacting with the DOM

Approaches to faking user input

• Synthetic events (JavaScript)

• Native events (via Java Applet)

Page 31: Testing Ext JS and Sencha Touch

Synthetic events+ Supported in all major browsers+ Compatible with mobile+ Don’t rely on native event queue

Tests can be run in parallell.

- Browsers don’t ”trust” synthetic events- Enter key on a focused link- Tab between input fields, etc...

- X-browser differencesDOM Events, Key events, key codes (http://unixpapa.com)

Page 32: Testing Ext JS and Sencha Touch

Native events

+ Java applets are supported in desktop browsers+ As close to a ’real’ user as possible

- Won’t work on iOS, Android.- No parallell tests since native event queue is used.

Page 33: Testing Ext JS and Sencha Touch
Page 34: Testing Ext JS and Sencha Touch

What is Siesta?

• Unit testing and DOM testing tool

• Optimized for Ext JS & Sencha Touch - also supports jQuery, NodeJS, or any JS

• Simple syntax, Extensible

• Automate using PhantomJS & Selenium.

Page 35: Testing Ext JS and Sencha Touch

Siesta Browser Harness

Page 36: Testing Ext JS and Sencha Touch

Siesta Browser Harness

• A UI for Siesta, view DOM, inspect assertions

• Define a tree of tests, in logical groups

• Global settings for the test suite autoCheckGlobals, preload, testClass

Page 37: Testing Ext JS and Sencha Touch

A Siesta Test

• is pure JavaScript, no magic.

StartTest(function(t) { $('body').html('JQuery was here'); t.contentLike(document.body,

'JQuery was here', 'Found correct text in DOM');

});

Page 38: Testing Ext JS and Sencha Touch

A Siesta Test

• is completely isolated in its own iframe(no teardown, cleanup required)

Page 39: Testing Ext JS and Sencha Touch

A Siesta Test

• can be extended with your own custom assertions and helper methods

StartTest(function(myTest) { var app = myTest.startApplication(); myTest.performLogin(app); myTest.isLoggedIn(app, ’Logged in ok’); myTest.is(app.nbrUsers, 1, ’Found 1 user’);});

Page 40: Testing Ext JS and Sencha Touch

Lifecycle of a UI test

Setup create grid, load stores

Wait for store to load, grid rows to render

Simulate click button, type into textbox

Verify isEqual(a, b)

Page 41: Testing Ext JS and Sencha Touch

Setup

var userGrid = new My.UserGrid({ … });

userGrid.render(Ext.getBody());

Page 42: Testing Ext JS and Sencha Touch

Wait

t.waitForRowsVisible(userGrid, function(){ // Do something});

t.waitForComponentQuery(“formpanel > textfield”);

t.waitForCompositeQuery(“grid => .x-grid-row”);

Page 43: Testing Ext JS and Sencha Touch

Simulate

// A list of actions to simulatet.chain(

{ desc : 'Should click trigger field',

action : 'click', target : 'testgrid triggerfield'

},{

desc : 'Should type into filter field', action : 'type',

target : 'testgrid triggerfield',text : 'FOO'

}, ...);

Page 44: Testing Ext JS and Sencha Touch

Verify

t.is(userStore.getCount(), 5, ”5 users found”);

t.willFireNTimes(store, 2, ”write”);

t.hasCls(grid.getEl(), ”myClass”, ”Found CSS”);

t.hasSize(myWindow, 300, 300, ”Window size ok”);

Page 45: Testing Ext JS and Sencha Touch

DEMO

Page 46: Testing Ext JS and Sencha Touch

Sencha Touch support

Simulate tap, doubleTap, swipe, longPress, etc...

Page 47: Testing Ext JS and Sencha Touch

Siesta.next

• Benchmarking

• Code coverage

• Multi-browser, multi-device testing• Crowd sourced test case tool “Code

Cowboy”

• Your suggestion?

Page 48: Testing Ext JS and Sencha Touch

Headless testing

• Headless browsers are great, fast

• Command line, easy to integrate with IDEs

• PhantomJS, GUI-less WebKit browser

Page 49: Testing Ext JS and Sencha Touch

Phantom JS

Created by Ariya Hidayat (Sencha Inc.)

Fast headless testing

Site scraping

SVG rendering, PDF/PNG output

Supports CoffeeScript

Page 50: Testing Ext JS and Sencha Touch

Headless browsers

+ Run tests on command line+ Faster+ Automation+ Doesn’t require an actual browser

- Not 100% accurate, but close.

Page 51: Testing Ext JS and Sencha Touch

Writing testable JS

Page 52: Testing Ext JS and Sencha Touch

Some ground rules

• Keep your JavaScript in JS files

• Never put JavaScript in your HTML page/tags

• Keep code organized in logical manageable files. Decide on some max nbr of lines/file.

Page 53: Testing Ext JS and Sencha Touch

No no no

Page 54: Testing Ext JS and Sencha Touch

Testable MVC code

• Fat model, skinny view

• Don’t pollute your views with business logic

• Testing pure JS is a lot easier than testing DOM-dependent JS

• Promotes reuse of your code

Page 55: Testing Ext JS and Sencha Touch

Mixing view and logic

Ext.define('UserForm', { extend: 'Ext.FormPanel', width: 400, height: 400,

// Returns true if User is valid isValid: function (userModel) { return userModel.name.length > 4 &&

userModel.password.length > 8; }});

Page 56: Testing Ext JS and Sencha Touch

Better

Ext.define('UserModel', { extend: 'Ext.data.Model ', name : '', password : '',

isValid : function () { return this.name.length > 4 &&

this.password.length > 8; }});

Page 57: Testing Ext JS and Sencha Touch

Refactored view

Ext.define('UserForm', { extend: 'Ext.FormPanel', width: 400, height: 400, // Returns true if User is valid isValid: function () { return this.model.isValid(); }});

Page 58: Testing Ext JS and Sencha Touch

Avoid private code

• Avoid overuse of private functions in closures

• If your code cannot be accessed it cannot be tested

Page 59: Testing Ext JS and Sencha Touch

Testing Ajax

• Try to avoid involving your database.

• Use either static JS files with mock data (async, slower)

• Or Mock the entire Ajax call (sync, faster)

Sinon.js, Jasmine-ajax etc.

Page 60: Testing Ext JS and Sencha Touch

Automation & Continuous Integration

Page 61: Testing Ext JS and Sencha Touch

Continuous Integration

• Once you have decided on your testing toolset, integrate it with your CI.

• Automatically run test suite on pre-commit or post-commit

• Nightly builds, full test suite execution, reporting via email etc.

Page 62: Testing Ext JS and Sencha Touch

CI Tools

• Jenkins

• TeamCity

• Cruise Control

• Test Swarm

Page 63: Testing Ext JS and Sencha Touch

JS Code Coverage

• JsCoverageSeems abandoned

• ScriptCoverGoogle Chrome Plugin

• JsTestDriverAdd-in module for coverage

• JesCov Rhino, Jasmine

Page 64: Testing Ext JS and Sencha Touch

Resources - Yahoohttp://screen.yahoo.com/

Page 65: Testing Ext JS and Sencha Touch

Resources - GTAC

Page 66: Testing Ext JS and Sencha Touch

”Without unit tests, you’re not refactoring. You’re just changing shit.”

Hamlet D’Arcy

Page 67: Testing Ext JS and Sencha Touch

Thanks for listening!

Questions?

2012 Mats Bryntse@bryntum