• No results found

JavaScript Testing. Beginner's Guide. Liang Yuxian Eugene. Test and debug JavaScript the easy way PUBLISHING MUMBAI BIRMINGHAM. k I I.

N/A
N/A
Protected

Academic year: 2021

Share "JavaScript Testing. Beginner's Guide. Liang Yuxian Eugene. Test and debug JavaScript the easy way PUBLISHING MUMBAI BIRMINGHAM. k I I."

Copied!
8
0
0

Loading.... (view fulltext now)

Full text

(1)

JavaScript Testing

Beginner

's

Guide

Test

and

debug

JavaScript

the

easy way

Liang

Yuxian

Eugene

[

rwtmm® I

k

I

I

PUBLISHING

(2)

Table

of

Contents

Preface

1

Chapter

1:

What

is

JavaScript Testing?

7

Where does

JavaScript

fit intotheweb

page?

8

HTML Content 8

Time for action-

building

a HTMLdocument 9

StylingHTML elementsusingits attributes 11

Specifyingid and classnameforanHTML element 12

Cascading Style

Sheets 12

Timefor action -

styling

your HTML document

using

CSS 14

ReferringtoanHTML elementbyits idorclassnameandstylingit 18

Differences betweenaclassselectorandanid selector 19

Otherusesfor dass selectors and id selectors 20

Completelist of CSS attributes 20

JavaScript providing

behavior toa webpage 20

Time for action-

giving

behaviorto yourHTML document 20

JavaScript Syntax 24

JavaScriptevents 26

Findingelementsinadocument 26

Puttingitall together 28

The difference between

JavaScript

and server-side

languages

29

Why

pages needtowork without

JavaScript

30

Whatis

testing?

31

Why

doyou needto test? 31

Types

oferrors 32

Loading

errors 33

Time for action-

loading

errorsin action 33

PartiallycorrectJavaScript 34

Timefor action

-loading

errorsin action 35

Runtimeerrors 36

Time for action-runtime errorsin action 36

(3)

Table ofContents

Timeforaction-

logic

errors in action 38

Someadvice for

writing

error-free

JavaScript

40

Always

check forpropernames of

objects, variables,

and functions 40

Check for proper

syntax

40

Plan before you code 40

Checkfor correctnessasyou code 40

Preventing

errors

by choosing

a suitabletexteditor 41

Summary

41

Chapter

2: Ad Hoc

Testing

and

Debugging

in

JavaScript

43

The purposeof ad hoc

testing-getting

the

script

torun 44

What

happens

when the browser encountersan errorin

JavaScript

44

Browserdifferencesand theneedtotest in

multiple

browsers 45

Time for action-

checking

for features and

sniffing

browsers 46

Testing

browser differences via

capability testing

47

Timefor action-

capability

testing

for different browsers 48

Are you

getting

thecorrect

output

and

putting

values in thecorrect

places?

50

Accessing

the values on aform 50

Time for action-

accessing

values

from

aform 51

Anothertechniqueforaccessingform values 54

Accessing

other partsof

the

web page 55

Time for action

-getting

thecorrectvalues in thecorrect

places

55

Doesthe

script

give

the

expected

result 65

What todo if the

script

doesn't run? 65

Visually inspecting

thecode 66

Using

alert()

toseewhat code is

running

66

Using alert()

toseewhat values are

being

used 67

Time for action-

using

alertto

inspect

yourcode 67

Aless obtrusiveway tocheck what code is

running

and the values used 71

Time for action-

unobtrusively checking

what valuesareused 72

Commenting

out

parts

of the

script

to

simplify testing

75

Timefor action-

simplifying

the

checking

process 76

Timing

differences-making

surethat theHTML is there before

interacting

with it 77

Why

ad hoc

testing

isnever

enough

78

Summary

79

Chapter

3:

Syntax

Validation

81

The difference between

validating

and

testing

82 Code that is valid but

wrong-validation

doesn't find all theerrors 83

Code thatisinvalid but

right

83

Code thatisinvalid and

wrong-validation

finds

someerrorsthat

might

(4)

of

Code

quality

83

HTML and CSS needs tobe valid before you start on

JavaScript

84

What

happens

if you don'tvalidate yourcode 85

Color-coding

editors-how your editorcan

help

youto

spot

validationerrors 87 Commonerrorsin

JavaScript

that will be

picked

up

by

validation 89

JSLint-anonline validator 90

Time for action-

using

JSLint to

spot validation errors 91

Valid codeconstructs that

produce

validation

warnings

92

Shouldyou

fix

valid code constructsthat

produce

validation

warnings?

92 What

happens

if you don't fix them 93

How tofix validationerrors 93

Error—missing

"use strict" statement 94

Time for action

-fixing

"use strict"errors 94

Error—unexpected

use of++ 94

Time for action-

fixing

the errorof

"Unexpected

useof++" 95

Error—functionsnotdefined 96

Time for action-

fixing

theerrorof "Functionsnotdefined" 96

Too many varstatements 97

Time for action-

fixing

theerrorof

using

too manyvarstatements 98

Expecting

<V

instead of

<\

100

Time foraction-

fixing

the

expectation

of

'<V

instead of

'</'

101

Expected

'===' but found '==' 102

Time for action

-changing

==to=== 102

Alert isnotdefined 102

Time foraction-

fixing

"Alertis not defined" 103

Avoiding

HTML event handlers 103

Time for action-

avoiding

HTMLeventhandlers 104

Summary

of the correctionswe have done 106

JavaScript

Lint-a tool you can download 112

Challenge yourself-fix

the

remaining

errors

spotted by

JSLint 113

Summary

113

Chapter

4:

Planning

to Test 115

A very briefintroductiontothe

software lifecycle

116

The

agile

method 116

Theagilemethod and the softwarecyclein action 117

Analysisanddesign 117

Implementation

and testing 117

Deployment 117

Maintenance 117

(5)

TableofContents

When to

develop

the test

plan

118 Howmuch

testing

is

required?

118

What is the code intended to do? 119

Testing

whether the code satisfiesour needs 119

Testing

for invalid actions

by

users 119

A shortsummaryof the

above

issues 120

Major

testing concepts

and

strategies

120

Functional

requirement testing

120

Non-functional

requirement testing

121

Acceptance testing

121

Black box

testing

122

Usabilitytests 123

Boundary

testing 123

Equivalence partitioning 123

Betatesting 124

White box

testing

124

Branchtesting 124

Paretotesting 125

Unit tests 125

Web pagetests 126

Performancetests 127

Integration testing

127

Regression testing-repeating prior testing

after

making

changes

128

Testing

order 128

Documenting

your test

plan

129

Thetest

plan

129

Versioning 130

Teststrategy 130

Bug form 137

Summary

ofourtest

plan

137

Summary

137

Chapter

5:

Putting

the Test Plan IntoAction 139

Applying

the test

plan: running

your tests in order 140

Test Case 1:

Testing expected and acceptable

values 140

Time for action-Test Case la:

testing expected

and

acceptable

values

by

using

white box

testing

141

TestCase lb:

Testing

expected

but

unacceptable

values

using

blackbox

testing

142

Time for action-Testcase lbi:

testing expected

but

unacceptable

values

using boundary

value

testing

142

Time foraction-Test

case lbii:

testing expected

but

unacceptable

values

(6)

of

Test Case 2:

Testing

the program

logic

146

Time for action-

testing

the program

logic

146

Test Case 3:

Integration testing and

testing unexpected

values 147 Time for action -Test Case 3a:

testing

the entire program with

expected

values 147 Time foraction-Test Case 3b:

testing

robustness of the second form 150

Whattodo whenatest returns an

unexpected

result 151

Regression testing

in action 151

Time for action-

fixing

the

bugs

and

performing

regression testing

151

Performance

issues—compressing

your codetomake it load faster 160 Does

using Ajax

make a difference? 161

Difference from server-side

testing

162

What

happens

if youvisitor turnsoff

JavaScript

162

Summary

164

Chapter

6:

Testing

More

Complex

Code

165

Issues with

combining scripts

166

Combining

eventhandlers 166

Naming

clashes 168

Using JavaScript

libraries 169

Doyou needto test a

library

that someone elsehas written? 170

Whatsortoftests to run

against library

code 170

Performancetesting 170

Profilingtesting 171

GUI and

widget

add-onstolibraries and considerationson how to testthem 171

Deliberately throwing

yourown

JavaScript

errors 172

The throwstatement 172

The try,

catch,

and

finally

statements 172

Trapping

errors

by using

built-in

objects

176

The Error

object

176

The

RangeError

object

178

The ReferenceError

object

178

The

TypeError object

180

The

SyntaxError object

181

The URIError

object

181

The EvalError

object

181

Using

the errorconsole

log

181

Errormessages 181

Writing

yourown messages 182

Modifying scripts

and

testing

184 Time for action-

coding, modifying, throwing,

and

catching

errors 184

(7)

TableofContents

Chapter

7:

Debugging

Tools

201

IE 8

Developer

Tools

(and

the

developer

toolbar

plugin

forIE6and

7)

202

Using

IE

developer

tools 202

Open

202

A briefintroduction tothe userinterface 203

Debugging

basics of the IE

debugging

tool 203

Time foraction

-debugging

HTML

by using

theIE8

developer

tool 204

Time for action-

debugging

CSS

by using

the IE8

developer

tool 205

Debugging

JavaScript

206

Timeforaction-more

Debugging JavaScript by using

the IE8

developer

tool 206

Safari or

Google

Chrome Web

Inspector

and

JavaScript Debugger

211

Differences between Safari and

Google

Chrome 211

Debugging using

Chrome 212

A brief introductiontothe userinterface 213

Timeforaction-

debugging

with Chrome 213

Opera JavaScript Debugger

(Dragonfly)

218

Using Dragonfly

218

Starting Dragonfly 218

Time

for

action

-debugging

with

Opera Dragonfly

219

Inspection

and Call Stack 220

Thread

Log

220

Continue,

Step

Into,

Step Over, Step Out,

and

Stop

at Error 220

Settings

222

Firefox and theVenkmanextension 222

Using Firefox's

Venkman extension 222 Obtainingthe VenkmanJavaScript Debuggerextension 222

OpeningVenkman 222

Abrief introductiontotheuserinterface 223

Time for action

-debugging using

Firefox's Venkman extension 224

Breakpoints

orCall Stack 225

Local Variables

and

Watches 226

Time foraction-more

debugging

with the Venkmanextension 227

Firefox andthe

Firebug

extension 229

(8)

of

Chapter

8:

Testing

Tools

231

Sahi 232

Timeforaction- userInterface

testing using

Sahi 232

More

complex testing

with Sahi 235

QUnit

236

Time for action

-testing

JavaScript

withQUnit 236

Applying

QUnit

in real-life situations 240

More assertion tests forvarioussituations 240

JSLitmus 241

Time foraction-

creating

ad hoc

JavaScript

benchmark tests 241

More

complex testing

with JSLitmus 244

More

testing

tools thatyou should

check

out 244

Summary

246

References

Related documents

We also exhibit a tight kernel for the Projective Point Hyperplane Cover problem, where the hyperplanes that are allowed to cover the points must all contain a fixed point, and

Cardiff Council held a public consultation in spring 2013 on a proposal to establish a stand alone new 1.5 form entry (FE) English-medium primary school with a 32 full time

Uncertainty is present in this analysis through failure rate inputs (some were based on field data from a particular model of wind turbines, whereas as a formal methodology was

The project sponsor will generally be expected to act, at a senior management level, as an advocate for the project; to ensure that the project delivers the desired business

Finally, just as CECA was to spearhead the link of Spanish savings banks with international wholesale financial markets, the industry association was key in the creation

With the litter team in the four-man carry position, the Team Leader gives the command, “Two-man carry, move.” The right front and left rear bearers grasp both handles and

ICO, Hospital Universitario Germans Trias i Pujol, Badalona (Barcelona) 08:45 -09:55 SESSION 7: MOLECULAR SCREENING. FOR SYNTHETIC

To recap from Chapter 4, the community sport policy making process has five stages: (1) national agencies (the DCMS and Sport England) debate and agree the policy