pragmatic parallels: java and javascript

81
2007 JavaOne SM Conference | Session TS-9624 | Session 9624 Practical Parallels: From Development on the Java™ Platform to Development With the JavaScript™ Programming Language Dave Johnson CTO and Co-founder Nitobi www.nitobi.com

Upload: davejohnson

Post on 17-May-2015

4.088 views

Category:

Technology


0 download

DESCRIPTION

JavaOne presentation looking at the different tools available to JavaScript developers for debugging, performance and deployment

TRANSCRIPT

Page 1: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 |

Session 9624

Practical Parallels: From Development on the Java™ Platform to Development With the JavaScript™ Programming Language

Dave Johnson

CTO and Co-founderNitobiwww.nitobi.com

Page 2: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 2

Goal

Learn about the tools and techniques for testing, debugging, and deployingJavaScript™ programming languages applications.

Page 3: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 3

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 4: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 4

Who I Am

• Nitobi Enterprise Ajax Podcast• Enterprise Ajax book (Addison Wesley)

• blogs.nitobi.com/dave

Page 5: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 5

What Do I Do?

• Nitobi co-founder• Located in Vancouver, Canada• Ajax user-interface components

for the enterprise

Page 6: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 6

Page 7: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 7

Clients

Page 8: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 8

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 9: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 9

http://www.flickr.com/photos/magtravels/85630949/

Page 10: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 10

Quality Assurance

• Widely introduced during WWII• Munitions industry demanded better quality

and more reliable “products”• Quality systems evolved to ISO 9001

“Rely on prevention rather than cure”

Page 11: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 11

It’s About the Bugs

• Testing is about finding bugs• Added bonus!

Now withcode

coverage!

Page 12: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 12

Not All About Bugs

• Performance (later)• Usability (some other time)• Accessibility (read the book)

Page 13: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 13

Development Philosophy

• Lots of development philosophies• Waterfall• Iterative• Agile / Extreme• <!-- insert cool new technique here -->

Page 14: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 14

The Many Faces of Testing

• Requirements• Design• Test Planning• Test Development• Test Execution• Test Reporting

Page 15: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 15http://www.flickr.com/photos/49503191155@N01/17975738/

Testing

Development

Requirements

Page 16: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 16

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 17: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 17http://www.flickr.com/photos/thomashawk/85441961/

Page 18: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 18

Testing Tools

• JSUnit• JSMock• HTTPUnit

Page 19: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 19

JSUnit

function setUpPage() {setUpPageStatus = 'complete';

}

function setUp() {//…

}

function testDoSomething() {assertEquals(“a”, ”b”);

}

Page 20: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 20

JSUnit Method Overview

• setUpPage()

• setUp()• tearDown()

• warn(message, [value])• inform(message, [value]) • debug(message, [value])

• addTestSuite(filename)

Page 21: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 21

Page 22: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 22

Page 23: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 23

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 24: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 24

Page 25: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 25

Functional Testing

• Test the software according to the specification• Functional testing steps:

• Identification of functions that the software is expected to perform

• Creation of test data that will exercise those functions

Page 26: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 26

Functional Testing Tools

• Selenium• Core• IDE• Remote Control

• Watir• jWebUnit

Page 27: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 27

Page 28: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 28

Page 29: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 29

This is a Test

<table>

<tr><td>setVariable</td><td>username</td><td>'user'+(new Date()).getTime()</td></tr>

<tr><td>open</td><td>./selenium_ft_tool/setup?user=${username}</td><td></td></tr>

<tr><td>setVariable</td><td>base_url</td><td>'http://www.example.com:8080/'</td></tr>

<tr><td>open</td><td>${base_url}</td><td></td></tr>

<tr><td>type</td><td>__ac_name</td><td>${username}</td></tr>

<tr><td>type</td><td>__ac_password</td><td>${username}</td></tr>

<tr><td>click</td><td>submit</td><td></td></tr>

<tr><td>verifyTextPresent</td><td>Welcome! You are now logged in</td><td></td></tr>

<tr><td>setVariable</td><td>myfolder_url</td><td>'${base_url}/${username}/folder'</td></tr>

<tr><td>click</td><td>//a[@href='${myfolder_url}']</td><td></td></tr>

<tr><td>verifyTextPresent</td><td>${username}</td><td></td></tr>

<tr><td>setVariable</td><td>homepage_url</td><td>'${base_url}/${username}/index/view'</td></tr>

<tr><td>click</td><td>//a[@href='${homepage_url}']</td><td></td></tr>

<table>

Page 30: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 30

DEMOSelenium IDE

Page 31: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 31

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 32: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 32http://www.flickr.com/photos/rbos/91474426/

Page 33: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 33

Why Regression Test?

• With many people working the same code regression bugs are inevitable

• This can be painful

Page 34: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 34

Manual Testing

• Don’t underestimate its value• Introduces randomness• We are good at noticing oddities• We are bad at checking precise results

Page 35: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 35

Automated Testing

• This can be expensive• The sooner you automate the greater value• Automated test value is in untargeted bugs

it finds—completely unrelated to the specific purpose for which it was written

Page 36: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 36

Good Bad

Easy X

Misses important bugs X

Find regression bugs X

Fragile X

Run with the build X

Ignore functionality X

The Good and the BadComparing aspects of automated testing

Page 37: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 37

The Value Proposition

1. The more bugs you have the higher cost of automation

2. The more automation you have the fewer bugs you find

3. The more testing infrastructure you already have the better

Page 38: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 38

Golden Rule

TI

ATNBTC

Page 39: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 39

Questions to Ask

• Do they test the right things?• How many bugs do you know about?• How long will tests be relevant?• What is the value of the tests?

Page 40: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 40

First Steps

• Have few automated tests that coarsely cover the software—Smoke Test

• If the smoke test succeeds, the product is testable and a “good build”

• Pluses• Configuration changes are easily observed• Gross regressions are caught preemptively• Keeps emphasis on manual testing

Page 41: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 41

Test Automation Tools

• Selenium server• JSUnit server

• CruiseControl• AntHill• Ant / NAnt

Page 42: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 42

JSUnit Server

Page 43: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 43

Selenium Server

Page 44: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 44

Page 45: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 45

Page 46: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 46

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 47: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 47

Finally

• Once the tests are written (and hopefully all fail) we can actually write some code

• There’s only one problem… creating bugs

Page 48: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 48

Page 49: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 49

http://www.flickr.com/photos/esther17/86558129/

Page 50: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 50

Page 51: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 51

Ground Rules

• Namespaces• nitobi.$

• Asynchronous programming• Watch out for IE

• Inheritance, interfaces etc• Global variables

• _this = this• Closures• Frameworks• Documentation

Page 52: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 52

Page 53: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 53

Debugging Tools

• Firebug (Mozilla)• Developer Toolbar (IE)• Fiddler (IE)• WebKit (Safari)

Page 54: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 54

Firebug

• console.log('width: %d height: %d', w, h);• Write arrays, elements, whatever• Debug, info, warn, error• console.trace• console.time / timeEnd

Page 55: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 55

DEMOFirebug

Page 56: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 56

Most Important JavaScript Programming Language Keyword

debugger

Page 57: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 57

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 58: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 58

http://www.flickr.com/photos/pulpolux/192587812/

Page 59: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 59

Deployment Guidelines

• Be aware of your audience• Ask questions first, optimize later

Page 60: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 60

Optimization

• Situation dependent• How much data?• What type of data?• How many server hits?• What are the common workflows?• What browsers are you targeting?• What is the existing infrastructure?

Page 61: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 61

HTML

XML

JSON

Page 62: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 62

Firefox

Page 63: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 63

Internet Explorer

Page 64: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 64

http://www.jamesward.org/census/

Firefox

Internet Explorer

Page 65: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 65

XSLT Cross Browser Performance

0

5

10

15

20

25

30

35

40

45

0 100 200 300 400 500

Table Cells

Tim

e (m

s)

Firefox 1.5

IE 6

Page 66: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 66

Big Picture

0

100

200

300

400

500

600

700

800

getO

ffset

Top

getB

ox

setC

lassN

ame

getS

tyles

heet

setB

ackg

roun

dCol

or

getS

tyle

getC

lassN

ame

Tim

e (

ms

)

Page 67: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 67

Most Wanted

• innerHTML• offsetTop / Left• getBoundingClientRect / getBoxObjectFor• Stylesheets

• Consider HTML tags, styles, events

Page 68: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 68

Documentation

• JSDoc

@constructor Class constructor

@class Class name

@param Method or function arguments

@extends Parent class

@type The function return type of field / property type (number, string etc)

@see Linking across classes and methods

Page 69: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 69

JSDoc Markup

/** * Creates a new SimpleDataTable instance * @class Simple class for storing record based data from the server. * @constructor * @extends entAjax.DataModel */entAjax.SimpleDataTable = function(data) { /** * Contains the data rendered in the DataTable * @private * @type {Array} * @see entAjax.DataModel#get */ this.m_data = data;}/** * @return Returns data from the server. * @param {String} url The URL of the location of the data on the server * @type */entAjax.SimpleDataTable.prototype.get = function(url) {}

Page 70: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 70

JavaScript Compression

• Removing comments• Removing whitespace• Removing new-line characters• Replacing variables with shorter names

Page 71: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 71

Simple Example

var _a = function(a){var b=0;var c=a.length;for(var d=0;d<c;d++){b+=a[d];}return b/c;}var calcAverage=_a;

/** * @private */var _calcAverage = function(aNumber) { var nTotal = 0; var iLength = aNumber.length; for (var iIndex = 0; i<iLength; i++) { nTotal += aNumber[iIndex]; } return nTotal/iLength;}/** * Calculates the average of an array of numbers. * @param {Array} Array of numbers to average. */var calcAverage = _calcAverage;

Page 72: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 72

Ant Based Minification / Obfuscation

<target name="obfuscateJS" description="compress and obfuscate code">

<java classname="org.mozilla.javascript.tools.shell.Main“dir="${basedir}\build\rhino\bin\“fork="true“output="${basedir}\output\src_obfuscated.js">

<arg line="-c ${basedir}\output\src.js" /> <classpath> <pathelement path="${basedir}\build\rhino\bin\js.jar"/> </classpath> </java></target>

Page 73: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 73

Compression

• Request• Accept-Encoding: gzip,deflate

• Response• Content-Encoding: gzip || deflate

• IIS and Apache dynamically GZip / Deflate content and cache it

• All modern browsers support compressed content• Internet Explorer 6 SP1 had some problems

• IE, Firefox, Opera accept compressed content _without_ Content-Encoding header

Page 74: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 74

Apache Config

# Compress everything unless excluded below.

SetOutputFilter DEFLATE

SetInputFilter DEFLATE

SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI \.(?:exe|t?gz|zip|bz2|rar)$ no-gzip dont-vary

SetEnvIfNoCase Request_URI \.(?:pdf|avi|mov|mp3|rm)$ no-gzip dont-vary

# Explicity compress certain file types

AddOutputFilterByType DEFLATE text/html text/plain text/xml

Page 75: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 75

Size (Kb)

Original 9.3

Minify 3.9

GZip / Deflate 2.8

Minify + GZip / Deflate 1.3

Size Reduction 86%

How Small is It?Expected Results for JavaScript Compression

Page 76: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 76

Content Merging

• Reduce download overhead by merging resources• JavaScript• Cascading Stylesheets• Images

• Careful with caching though!

Page 77: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 77

Image Merging

Page 78: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 78

Image Merging

<html> <head> <style type="text/css" media="screen"> .colour {clip: rect(0px 135px 125px 0px);} .grayscale { left:-135px; clip: rect(0px 270px 125px 135px); } .grayscale, .colour { position:absolute; width: 270px;height: 125px; background: url(images/nitobi.jpg); } .container { height:125px;width:135px; position:relative; } </style> </head> <body> <div class="container"><div class="colour"></div></div> <div class="container"><div class="grayscale"></div></div> </body></html>

clip: rect(0px 135px 125px 0px);

clip: rect(0px 270px 125px 135px);

background: url(images/nitobi.jpg);

Page 79: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 79

Agenda

Who I Am

JavaScript Programming Language TestingUnit TestingFunctional TestingRegression Testing

JavaScript Programming Language Debugging

JavaScript Programming Language Deployment

Summary

Page 80: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 80

Summary

• Assuring quality is an expensive task• There is value in testing• Don’t test too much• Use all the tools at your disposal• Optimize with care

Page 81: Pragmatic Parallels: Java and JavaScript

2007 JavaOneSM Conference | Session TS-9624 | 81

Q&A