Title: Marty Kube
1Scripting Approaches For Java Application Test
Automation
- Marty Kube
- Beaver Creek Consulting Corp
- NoVa Test Automation Interest Group
- 2009-04-08
http//beavercreekconsulting.com/
http//www.novataig.net/
2Scripting and Test Automation
- Scripting is the way to go for test automation
- High Level languages - More functionality with
less code - How scripting interacts with the application
- External interfaces
- Win runner
- JMeter
- Expose Internals
- JavaScript in the Browser
- SQL
3Survey a Few DSL Approaches
- Small example application
- Use scripting and Domain Specific Language to
drive application - Groovy
- JavaScript
- JavaCC
- Hands on with code examples
4Contrasting the DSL approaches
- Focus on Strategy Using Java as an example
similar tactics should apply for other
environments. - Three types of resources
- Expertise for set-up, Integration effort,
Expertise for test authoring - Trade offs
- General Trade offs
- Less front end setup
- More complex test authoring
- More front end setup
- Less complex test authoring
5Scripting Languages
- Type-less or easy to use type system
- High Level more productive and less defects
- Interpreted
- Interactive shell for rapid exploration or
development - Text processing - AWK, SED
- General Purpose - Perl, Python, TCL, Emacs LISP
http//en.wikipedia.org/wiki/Scripting
6Domain Specific Languages (DSL)
- Smaller language dedicated to a particular
situation - SQL
- Second Life/MORPHS
- Spotted in the wild
- Less general
- Lack low level functionality
- More expressive in the particular domain
- Really blurry line between scripting language and
DSL
http//en.wikipedia.org/wiki/Domain_specific_langu
age
7Example Application
8The Java Code
- package com.beavercreekconsulting.novataig
- public class TestLedger
- public static void main(String args)
- Ledger subLedger new Ledger()
- // Open the period
- subLedger.setBalance(Account.CHECKING,
1000.00) - subLedger.setBalance(Account.TRAVEL_AND_ENTERTAI
NMENT, 200.00) - subLedger.setBalance(Account.SLUSH_FUND,
-500.00) - // Post activity
- JournalEntry activity
- new JournalEntry(Account.CHECKING,
Ind.CREDIT, 100.00), - new JournalEntry(Account.TRAVEL_AND_ENTERTAINM
ENT, Ind.DEBIT, 100.00), - new JournalEntry(Account.CHECKING,
Ind.CREDIT, 2000.00), - new JournalEntry(Account.SLUSH_FUND,
Ind.DEBIT, 2000.00)
9The Groovy Code
- package com.beavercreekconsulting.novataig
- public class TestLedger
- public static void main(String args)
- Ledger subLedger new Ledger()
- // Open the period
- subLedger.setBalance(Account.CHECKING,
1000.00) - subLedger.setBalance(Account.TRAVEL_AND_ENTERTAI
NMENT, 200.00) - subLedger.setBalance(Account.SLUSH_FUND,
-500.00) - // Post activity
- JournalEntry activity
- new JournalEntry(Account.CHECKING,
Ind.CREDIT, 100.00), - new JournalEntry(Account.TRAVEL_AND_ENTERTAINM
ENT, Ind.DEBIT, 100.00), - new JournalEntry(Account.CHECKING,
Ind.CREDIT, 2000.00), - new JournalEntry(Account.SLUSH_FUND,
Ind.DEBIT, 2000.00)
10Groovy Up Close
- Super set of Java
- Plus
- Dynamic typing
- Enhanced API Java Classes
- String interpolation
- def x Bob print Hello x
- Closures
- square it it
- 1..9.collect(square)
- XML builders and groovy SQL
- Script MS Office with COM Bridge (Scriptom)
http//groovy.codehaus.org/Tutorial2-Codeasda
ta,orclosures
11Groovy Architecture Whats in the JVM
gtgroovy.bat -classpath bin TestLedger.groovy
12Really Groovy Code
- import com.beavercreekconsulting.novataig.
- def subLedger new Ledger()
- Account.CHECKING, 1000.00,
- Account.TRAVEL_AND_ENTERTAINMENT, 200.00,
- Account.SLUSH_FUND, -500.00
-
- .each() subLedger.setBalance(it0, it1)
- new JournalEntry(Account.CHECKING, Ind.CREDIT,
100.00), - new JournalEntry(Account.TRAVEL_AND_ENTERTAINMENT
, Ind.DEBIT, 100.00), - new JournalEntry(Account.CHECKING, Ind.CREDIT,
2000.00), - new JournalEntry(Account.SLUSH_FUND, Ind.DEBIT,
2000.00) -
- .each() je -gt subLedger.post(je)
- Account.CHECKING, -1100.00,
- Account.TRAVEL_AND_ENTERTAINMENT, 300.00,
13JavaScript
- JavaScript is a small language ECMA Script
- Easy to integrate
- Easy to find JavaScript Fan boys
- Rhino is a JavaScript interpreter written in Java
- Other JavaScript Engines, Mozilla, V8
http//en.wikipedia.org/wiki/Javascript
http//www.mozilla.org/rhino/
14Rhino Architecture
gtjava -classpath lib\js.jarbin
org.mozilla.javascript.tools.shell.Main -f
TestLedger.js
15The JavaScript Code
- importPackage(com.beavercreekconsulting.novataig)
- function assertEquals(a, b)
- if(a ! b) throw new TestFailure()
-
- var subLedger new Ledger()
- subLedger.setBalance(Account.CHECKING, 1000.00)
- subLedger.setBalance(Account.TRAVEL_AND_ENTERTAINM
ENT, 200.00) - subLedger.setBalance(Account.SLUSH_FUND, -500.00)
- activity
- new JournalEntry(Account.CHECKING, Ind.CREDIT,
100.00), - new JournalEntry(Account.TRAVEL_AND_ENTERTAINMENT
, Ind.DEBIT, 100.00), - new JournalEntry(Account.CHECKING, Ind.CREDIT,
2000.00), - new JournalEntry(Account.SLUSH_FUND, Ind.DEBIT,
2000.00)
16JavaCC Create a DSL
-
- Test case 16,678
- Author Marty
-
- Checking set 1000.00
- travel_and_entertainment set 200.00
- slush_fund set -500.00
-
- checking post credit 100.00
- travel_and_entertainment post debit 100.00
- checking post credit 2000.00
- slush_fund post debit 2000.00
-
- Checking verify -1100.00
- travel_and_entertainment verify 300.00
- slush_fund verify 1500.00
- print happy day!
https//javacc.dev.java.net/
17JavaCC
18JavaCC Lexer
- SKIP " "
- TOKEN lt CHECKING "checking" gt
- TOKEN lt OP_SET "set" gt
- TOKEN lt NUMBER ("0"-"9") gt
- TOKEN lt EOL "" gt
- ltCHECKINGgt ltOP_SETgt ltNUMBERgt ltEOLgt
- Checking set 1000.00
19JavaCC Parser
- Accepts the token stream
- ltCHECKINGgt ltOP_SETgt ltNUMBERgt ltEOLgt
- ltTRAVELgt ltOP_POSTgt ltNUMBERgt ltEOLgt
- ltEOFgt
- And enforces order the grammar
- (ltCHECKINGgt ltOP_SETgt ltNUMBERgt ltEOLgt )
- ltEOFgt
20Recognize Statements Invoke Application
- (
- ltCHECKINGgt
- ltOP_SETgt
- tltNUMBERgt
- amount new Double(t.image)
- ltEOLgt
- System.out.println("Posting " amount)
- ledger.setBalance(Account.CHECKING, amount)
- )
- ltEOFgt
- System.out.println("Happy Day")
-
21Running
- gtjavacc TestLedger.jj
- gtjavac -d bin -cp "../bin" gen\.java
- gtjava -classpath "bin../bin" LedgerLanguage
TestCase.txt
22Comparison - LOE
23Honorable mentions
- SWIG
- Antlr
- JavaScript Mozilla v8
- Most scripting languages cross language
interfaces, Perl, Python, etc. - Bean Shell
- Jython
24Wrap up
- JVM is a target for many scripting languages
- Using a JVM based language make integration easy
- DSL can make it easy to author test cases
- Setup can be a hurdle