Transcript
Page 1: Adventures In JavaScript Testing

Adventures inJavaScript Testing

Thomas Fuchs

Page 3: Adventures In JavaScript Testing

traditionaljavascript „testing“

Page 4: Adventures In JavaScript Testing
Page 5: Adventures In JavaScript Testing

alert(“please use me only where appropriate!”);

Page 6: Adventures In JavaScript Testing

why?

Page 7: Adventures In JavaScript Testing

"JavaScript is this thing for funny mouse trails, right?"

Page 8: Adventures In JavaScript Testing

"BUT You CAN't DEBUG JavaScript!"

Page 9: Adventures In JavaScript Testing

"WE DON'T NEED NO STINKIN' TESTS

FOR TEN LINES OF CODE!"

Page 10: Adventures In JavaScript Testing

"ANYWAY, THERE IS NO WAY TO

AUTOMATICALLY TEST JAVASCRIPT!"

Page 11: Adventures In JavaScript Testing

all of this is pure FUD

Page 12: Adventures In JavaScript Testing

unit testing javascript

Page 13: Adventures In JavaScript Testing
Page 14: Adventures In JavaScript Testing

Borrows from

Test::Unit

Page 15: Adventures In JavaScript Testing

include unittest.js(requires Prototype)

Page 16: Adventures In JavaScript Testing

a DIV is required for containing the test results

(defaults to "testlog")

Page 17: Adventures In JavaScript Testing

create a new instance ofTest.Unit.Runner…

Page 18: Adventures In JavaScript Testing

…this will excute all methods starting with

"test"

Page 19: Adventures In JavaScript Testing

assert away!

Page 20: Adventures In JavaScript Testing

(syntax strangenessfor more convenience

when calling assertions)

Page 21: Adventures In JavaScript Testing

assert(expression)

assert(true) => PASSassert(false) => FAILassert(2*2==4) => PASS

Page 22: Adventures In JavaScript Testing

assertEqual(expected, actual)

assertEqual(‘a’,’a’) => PASSassertEqual(‘a’,’b’) => FAILassertEqual(1,1) => PASSassertEqual(1,’1’) => PASS

does not compare type

Page 23: Adventures In JavaScript Testing

assertEnumEqual(expected, actual)

assertEnumEqual([1,2],[1,2]) => PASS

assertEqual does not compare enum contents

assertEqual([1,2],[1,2]) => FAIL

Page 24: Adventures In JavaScript Testing

assertNotEqual(expected, actual)

assertNotEqual(‘a’,’a’) => FAILassertNotEqual(‘a’,’b’) => PASSassertNotEqual(1,1) => FAILassertNotEqual(1,’1’) => FAIL

does not compare type

Page 25: Adventures In JavaScript Testing

assertMatch(expected, actual)

assertMatch(/a/,’a’) => PASSassertMatch(/a/,’bab’) => PASSassertMatch(/a/,’b’) => FAIL

assertMatch(/^moo$/,’moo’) => PASS

Page 26: Adventures In JavaScript Testing

assertIdentical(expected, actual)

assertIdentical(‘a’,’a’) => PASSassertIdentical(1,’1’) => FAIL

compares type

Page 27: Adventures In JavaScript Testing

assertNotIdentical(expected, actual)

assertNotIdentical(‘a’,’a’) => FAILassertNotIdentical(1,’1’) => PASS

compares type

Page 28: Adventures In JavaScript Testing

assertType(expected, actual)

assertType(String,’a’) => PASSassertType(Number,1) => PASS

assertType(Array,[1,2]) => PASS

checks for a specific constructor

Page 29: Adventures In JavaScript Testing

assertRaise(exceptionName, method)

assertRaise(‘ElementDoesNotExistError’, function(){ new Effect.Opacity(‘invalid-element’) });

related effects.js code

Page 30: Adventures In JavaScript Testing

assertRespondsTo(method, obj)

Page 31: Adventures In JavaScript Testing

assertVisible(element)

assertNotVisible(element)

also checks if any parent elements are hidden and

fails/passes on that

Page 32: Adventures In JavaScript Testing

info(message)

Displays arbitrary messages in the "Message" column of the test result

Page 33: Adventures In JavaScript Testing

assertXYZ(params, message)

Page 34: Adventures In JavaScript Testing

"WELL, WHAT ABOUTFUNCTIONS THAT

RUN ASYNCHRONOUSLY?"

Page 35: Adventures In JavaScript Testing

wait(milliseconds, method)

should be last statement in the test (but can be nested)

Page 36: Adventures In JavaScript Testing

Benchmarking

Page 37: Adventures In JavaScript Testing

"WELL, NICE. BUT IT'S TEDIOUS TO RUN ALL THOSE

TESTS MANUALLY!"

Page 38: Adventures In JavaScript Testing

rake to the rescue

Page 39: Adventures In JavaScript Testing

rake test:javascripts

Page 40: Adventures In JavaScript Testing
Page 41: Adventures In JavaScript Testing
Page 42: Adventures In JavaScript Testing

javascript_test plugin

Page 43: Adventures In JavaScript Testing

WEBrick

rake test:javascripts

2. controls browsers

1. launchesweb server

3. run tests

lists resultsSUCCESSFAILUREERROR

Page 44: Adventures In JavaScript Testing

$ script/generate javascript_test muhaha exists test/javascript create test/javascript/muhaha_test.html

Page 45: Adventures In JavaScript Testing

justadd your

tests

Page 46: Adventures In JavaScript Testing

(symlink required:app/test/javascript/assets to

app/vendor/plugins/javascript_test/assets)

Page 47: Adventures In JavaScript Testing

Browser definitions(add more!)

Page 48: Adventures In JavaScript Testing

$ script/plugin install http://dev.rubyonrails.org/svn/rails/plugins/javascript_test

Page 49: Adventures In JavaScript Testing

…and if something breaks?

Page 50: Adventures In JavaScript Testing

http://www.flickr.com/photos/teagrrl/78941282/

Firebug sliced bread

Page 51: Adventures In JavaScript Testing

Firebug

Debugger

Page 52: Adventures In JavaScript Testing

Firebug

knowyourAjax

Page 53: Adventures In JavaScript Testing

Safari Web Inspector

Page 54: Adventures In JavaScript Testing

Safari Debugging

Page 55: Adventures In JavaScript Testing

Microsoft Script Debugger for IE

http://blogs.msdn.com/ie/archive/2004/10/26/247912.aspx

Page 56: Adventures In JavaScript Testing

Test with all browsers

you want to support

Page 57: Adventures In JavaScript Testing
Page 58: Adventures In JavaScript Testing

get a setup that doesn‘t suck

Page 59: Adventures In JavaScript Testing

Source: http://www.flickr.com/photos/icathing/26603225/

Page 60: Adventures In JavaScript Testing

+ +

2 VMs:IE 6IE 7

Page 61: Adventures In JavaScript Testing

one machine to rule them all

+ +

Page 62: Adventures In JavaScript Testing

#prototype (freenode)and

Rails Spinoffs Google Group