Pragmatic Parallels: Java and JavaScript

Post on 17-May-2015

4,091 views 0 download

description

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

transcript

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

2007 JavaOneSM Conference | Session TS-9624 | 2

Goal

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

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

2007 JavaOneSM Conference | Session TS-9624 | 4

Who I Am

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

• blogs.nitobi.com/dave

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

2007 JavaOneSM Conference | Session TS-9624 | 6

2007 JavaOneSM Conference | Session TS-9624 | 7

Clients

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

2007 JavaOneSM Conference | Session TS-9624 | 9

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

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”

2007 JavaOneSM Conference | Session TS-9624 | 11

It’s About the Bugs

• Testing is about finding bugs• Added bonus!

Now withcode

coverage!

2007 JavaOneSM Conference | Session TS-9624 | 12

Not All About Bugs

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

2007 JavaOneSM Conference | Session TS-9624 | 13

Development Philosophy

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

2007 JavaOneSM Conference | Session TS-9624 | 14

The Many Faces of Testing

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

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

Testing

Development

Requirements

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

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

2007 JavaOneSM Conference | Session TS-9624 | 18

Testing Tools

• JSUnit• JSMock• HTTPUnit

2007 JavaOneSM Conference | Session TS-9624 | 19

JSUnit

function setUpPage() {setUpPageStatus = 'complete';

}

function setUp() {//…

}

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

}

2007 JavaOneSM Conference | Session TS-9624 | 20

JSUnit Method Overview

• setUpPage()

• setUp()• tearDown()

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

• addTestSuite(filename)

2007 JavaOneSM Conference | Session TS-9624 | 21

2007 JavaOneSM Conference | Session TS-9624 | 22

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

2007 JavaOneSM Conference | Session TS-9624 | 24

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

2007 JavaOneSM Conference | Session TS-9624 | 26

Functional Testing Tools

• Selenium• Core• IDE• Remote Control

• Watir• jWebUnit

2007 JavaOneSM Conference | Session TS-9624 | 27

2007 JavaOneSM Conference | Session TS-9624 | 28

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>

2007 JavaOneSM Conference | Session TS-9624 | 30

DEMOSelenium IDE

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

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

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

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

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

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

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

2007 JavaOneSM Conference | Session TS-9624 | 38

Golden Rule

TI

ATNBTC

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?

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

2007 JavaOneSM Conference | Session TS-9624 | 41

Test Automation Tools

• Selenium server• JSUnit server

• CruiseControl• AntHill• Ant / NAnt

2007 JavaOneSM Conference | Session TS-9624 | 42

JSUnit Server

2007 JavaOneSM Conference | Session TS-9624 | 43

Selenium Server

2007 JavaOneSM Conference | Session TS-9624 | 44

2007 JavaOneSM Conference | Session TS-9624 | 45

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

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

2007 JavaOneSM Conference | Session TS-9624 | 48

2007 JavaOneSM Conference | Session TS-9624 | 49

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

2007 JavaOneSM Conference | Session TS-9624 | 50

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

2007 JavaOneSM Conference | Session TS-9624 | 52

2007 JavaOneSM Conference | Session TS-9624 | 53

Debugging Tools

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

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

2007 JavaOneSM Conference | Session TS-9624 | 55

DEMOFirebug

2007 JavaOneSM Conference | Session TS-9624 | 56

Most Important JavaScript Programming Language Keyword

debugger

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

2007 JavaOneSM Conference | Session TS-9624 | 58

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

2007 JavaOneSM Conference | Session TS-9624 | 59

Deployment Guidelines

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

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?

2007 JavaOneSM Conference | Session TS-9624 | 61

HTML

XML

JSON

2007 JavaOneSM Conference | Session TS-9624 | 62

Firefox

2007 JavaOneSM Conference | Session TS-9624 | 63

Internet Explorer

2007 JavaOneSM Conference | Session TS-9624 | 64

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

Firefox

Internet Explorer

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

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

)

2007 JavaOneSM Conference | Session TS-9624 | 67

Most Wanted

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

• Consider HTML tags, styles, events

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

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) {}

2007 JavaOneSM Conference | Session TS-9624 | 70

JavaScript Compression

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

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;

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>

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

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

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

2007 JavaOneSM Conference | Session TS-9624 | 76

Content Merging

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

• Careful with caching though!

2007 JavaOneSM Conference | Session TS-9624 | 77

Image Merging

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);

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

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

2007 JavaOneSM Conference | Session TS-9624 | 81

Q&A