SlideShare a Scribd company logo
Cucumber on the JVM with
              Groovy
          cucumber, cuke4duke, groovy, geb




Richard Paul                             2011-03-24
What is Groovy

•   Dynamic language for the JVM
•   Inspired by Python, Ruby, Smalltalk
•   Integrates closely with Java
•   Superset of Java syntax




http://groovy.codehaus.org/
Why Groovy

•   Leverage existing Java API knowledge
•   Integrate closely with production Java code
•   Expressive language & libraries
•   Just a 5MB jar on the classpath
Groovy Support in Cucumber

• Cuke4Duke
• Uses JRuby behind the scenes
  o Pure Java coming later this year
• Groovy DSL to automate scenarios




https://github.com/aslakhellesoy/cuke4duke
Feature

Scenario: Regular numbers
  Given I have entered 3
  And I have entered 2
  When I press divide
  Then the result should be 1.5




https://github.com/aslakhellesoy/cuke4duke/tree/master/examples/groovy
Given


Given(~'I have entered (.*)') { number ->
    calculator.push number
}



// Implicit coercion to integer
Given(~'I have entered (.*)') { int number ->
    assert number.class == Integer.class
    calculator.push number
}
When


When(~'I press (w+)') { operatorName ->
    result = calculator."$operatorName"()
}



// Can use slashy strings to avoid escaping
When(/I press (w+)/) { operatorName ->
    result = calculator."$operatorName"()
}
Then


Then(~'the result should be (.*)') {
        double result ->
    assert expected == result
}


// == compares equality
// not same instance as in Java
Power Assert

def now = new Date()
def old = Date.parse('yyyyMMdd', '20100101')
assert now.date == old.date



Assertion failed:

assert now.date == old.date
       |   |    | |    |
       |   19   | |    1
       |        | Fri Jan 01 00:00:00 GMT 2010
       |        false
       Sat Mar 19 13:18:42 GMT 2011
Multiline Strings

Given some text
"""
Line 1
Line 2
"""

Given(~'some text') { body ->
    body.eachLine {
        println it
    }
}

=> Line 1
=> Line 2
Tables

Given I have the following foods
  |name |healthy|
  |Orange|Yes    |
  |Chips |No     |
When I count the number of healthy items
Then I have 1 healthy item
Tables

Given(~'I have the following foods') { table ->
    basket = new Basket()
    table.hashes().each {
        def item = new Food(
            name:    it.name, // it.get('name')
            healthy: it.healthy == 'Yes')
        basket.add(item)
    }
}
When(~'I count the healthy items') {
    numberOfHealthy = basket.numberOfHealthy
}
Then(~'I have (.+) healthy items') { int count ->
    assert numberOfHealthy == count
}
Tables

class Basket {
    private items = []
    void add(item) {
        items << item
    }
    int getNumberOfHealthy() {
        items.findAll {
            it.healthy
        }.size()
    }
}

class Food {
    def name
    def healthy
}
Organising Step Definitions

Cucumber will read in any .groovy files within step_definitions

All steps are then available to any scenarios

State can be shared between steps by setting to script binding

When(~'I set a variable to the binding') {
    x = 1
}
Then(~'the other step can access variable') {
    assert x == 1
}
Before/After

Before {
  // Initialise something
}

Before('@tagname') {
  // Initialise only for features/scenarios
  // tagged with @tagname
}
Before('~@tagname') {} // not tagged

After {
  // Clean up something
}
World

Allow simple access to methods from within step definitions


World {
    def world = new Object()
    world.metaClass.mixin Math
    world
}


When(~'we take the square root') {
    sqrt(4)    // calls Math.sqrt(4)
}
Browser Automation with Geb

Given(~'I am on the Wikipedia homepage') {
    go()
}

When(~'I search for "(.+)"') { query ->
    $('#searchInput').value(query)
    $('.searchButton').click()
}

Then(~'I am shown the "(.+)" article') { article ->
    assert $('h1').text() == article
}
env.groovy for Geb

import geb.Browser
this.metaClass.mixin(cuke4duke.GroovyDsl)

World {
    new Browser('http://wikipedia.org')
}

After {
    clearCookies()
}
Build Tools

Integration with
 • Maven
 • Ant

Cucumber can write reports in JUnit format for CI reports
Discussion/Questions




                   Thanks!

         http://rapaul.com      @rapaul

More Related Content

What's hot (19)

PDF
Python my SQL - create table
Learnbay Datascience
 
PDF
Python my sql database connection
Learnbay Datascience
 
PDF
GPars For Beginners
Matt Passell
 
PPTX
TDD in the wild
Brainhub
 
PDF
Productive Programming in Groovy
Ganesh Samarthyam
 
PDF
Selenium cheat sheet
Sri Priya P Kulkarni
 
PDF
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
PDF
C++ course start
Net3lem
 
KEY
Gwt and Xtend
Sven Efftinge
 
PPSX
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
Rohit malav
 
PPTX
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
PDF
Python in the database
pybcn
 
ODP
FRP and Bacon.js
Starbuildr
 
DOCX
Service Functions
pineda2
 
ODP
Meetup slides
suraj_atreya
 
PDF
FrontendLab: Programming UI with FRP and Bacon js - Вячеслав Ворончук
GeeksLab Odessa
 
PPTX
Voldemort collections library
Jason Ko
 
PPTX
Grails queries
Husain Dalal
 
PPTX
Scala
suraj_atreya
 
Python my SQL - create table
Learnbay Datascience
 
Python my sql database connection
Learnbay Datascience
 
GPars For Beginners
Matt Passell
 
TDD in the wild
Brainhub
 
Productive Programming in Groovy
Ganesh Samarthyam
 
Selenium cheat sheet
Sri Priya P Kulkarni
 
Auto-GWT : Better GWT Programming with Xtend
Sven Efftinge
 
C++ course start
Net3lem
 
Gwt and Xtend
Sven Efftinge
 
NOTEPAD MAKING IN PAYTHON 2ND PART BY ROHIT MALAV
Rohit malav
 
Psycopg2 - Connect to PostgreSQL using Python Script
Survey Department
 
Python in the database
pybcn
 
FRP and Bacon.js
Starbuildr
 
Service Functions
pineda2
 
Meetup slides
suraj_atreya
 
FrontendLab: Programming UI with FRP and Bacon js - Вячеслав Ворончук
GeeksLab Odessa
 
Voldemort collections library
Jason Ko
 
Grails queries
Husain Dalal
 

Viewers also liked (8)

ODP
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
PPTX
Is Groovy better for testing than Java?
Trisha Gee
 
PDF
Cucumber in Practice(en)
Kyosuke MOROHASHI
 
PDF
RSpec & TDD Tutorial
Wen-Tien Chang
 
PDF
Cucumber ppt
Qwinix Technologies
 
PPTX
Test automation with Cucumber-JVM
Alan Parkinson
 
PDF
Introduction to BDD with Cucumber for Java
Seb Rose
 
PPTX
Cucumber_Training_ForQA
Meenakshi Singhal
 
Test Automation Framework using Cucumber BDD overview (part 1)
Mindfire Solutions
 
Is Groovy better for testing than Java?
Trisha Gee
 
Cucumber in Practice(en)
Kyosuke MOROHASHI
 
RSpec & TDD Tutorial
Wen-Tien Chang
 
Cucumber ppt
Qwinix Technologies
 
Test automation with Cucumber-JVM
Alan Parkinson
 
Introduction to BDD with Cucumber for Java
Seb Rose
 
Cucumber_Training_ForQA
Meenakshi Singhal
 
Ad

Similar to Cucumber on the JVM with Groovy (20)

PPTX
Cucumber presenation
Oussama BEN WAFI
 
PPTX
Test Driven In Groovy
Christopher Bartling
 
PDF
Oscon Java Testing on the Fast Lane
Andres Almiray
 
PDF
Groovy On Trading Desk (2010)
Jonathan Felch
 
PDF
Groovy.pptx
Giancarlo Frison
 
PDF
Groovy Fly Through
niklal
 
KEY
Introduction to Groovy
André Faria Gomes
 
PDF
Acceptance testing with Geb
Richard Paul
 
PDF
Introduction to Groovy (Serbian Developer Conference 2013)
Joachim Baumann
 
PDF
淺談 Groovy 與 AWS 雲端應用開發整合
Kyle Lin
 
ODP
BDD with Cucumber
Knoldus Inc.
 
KEY
Cucumber, Cuke4Duke, and Groovy
Christopher Bartling
 
PDF
Behaviour driven infrastructure
Lindsay Holmwood
 
PDF
Groovy and Grails talk
desistartups
 
PPT
Svcc Groovy Testing
Andres Almiray
 
PPT
GTAC Boosting your Testing Productivity with Groovy
Andres Almiray
 
PPTX
Cucumber jvm best practices v3
Ahmed Misbah
 
PDF
Javantura v2 - Making Java web-apps Groovy - Franjo Žilić
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PPT
Cucumber presentation
Akhila B
 
PPT
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
Cucumber presenation
Oussama BEN WAFI
 
Test Driven In Groovy
Christopher Bartling
 
Oscon Java Testing on the Fast Lane
Andres Almiray
 
Groovy On Trading Desk (2010)
Jonathan Felch
 
Groovy.pptx
Giancarlo Frison
 
Groovy Fly Through
niklal
 
Introduction to Groovy
André Faria Gomes
 
Acceptance testing with Geb
Richard Paul
 
Introduction to Groovy (Serbian Developer Conference 2013)
Joachim Baumann
 
淺談 Groovy 與 AWS 雲端應用開發整合
Kyle Lin
 
BDD with Cucumber
Knoldus Inc.
 
Cucumber, Cuke4Duke, and Groovy
Christopher Bartling
 
Behaviour driven infrastructure
Lindsay Holmwood
 
Groovy and Grails talk
desistartups
 
Svcc Groovy Testing
Andres Almiray
 
GTAC Boosting your Testing Productivity with Groovy
Andres Almiray
 
Cucumber jvm best practices v3
Ahmed Misbah
 
Javantura v2 - Making Java web-apps Groovy - Franjo Žilić
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Cucumber presentation
Akhila B
 
2007 09 10 Fzi Training Groovy Grails V Ws
loffenauer
 
Ad

More from Richard Paul (8)

PDF
Acceptance tests
Richard Paul
 
PDF
jQuery Behaviours
Richard Paul
 
PDF
Introduction to Spring's Dependency Injection
Richard Paul
 
PDF
Introduction to Spring MVC
Richard Paul
 
PDF
Unit Testing Fundamentals
Richard Paul
 
PDF
Using Dojo
Richard Paul
 
PDF
Javascript & Ajax Basics
Richard Paul
 
PDF
Mocking in Java with Mockito
Richard Paul
 
Acceptance tests
Richard Paul
 
jQuery Behaviours
Richard Paul
 
Introduction to Spring's Dependency Injection
Richard Paul
 
Introduction to Spring MVC
Richard Paul
 
Unit Testing Fundamentals
Richard Paul
 
Using Dojo
Richard Paul
 
Javascript & Ajax Basics
Richard Paul
 
Mocking in Java with Mockito
Richard Paul
 

Recently uploaded (20)

PPTX
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
PDF
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
PDF
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PPTX
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
PDF
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
PPTX
Digital Circuits, important subject in CS
contactparinay1
 
PPTX
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 
Future Tech Innovations 2025 – A TechLists Insight
TechLists
 
“Computer Vision at Sea: Automated Fish Tracking for Sustainable Fishing,” a ...
Edge AI and Vision Alliance
 
The 2025 InfraRed Report - Redpoint Ventures
Razin Mustafiz
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
MuleSoft MCP Support (Model Context Protocol) and Use Case Demo
shyamraj55
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Seamless Tech Experiences Showcasing Cross-Platform App Design.pptx
presentifyai
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Go Concurrency Real-World Patterns, Pitfalls, and Playground Battles.pdf
Emily Achieng
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
COMPARISON OF RASTER ANALYSIS TOOLS OF QGIS AND ARCGIS
Sharanya Sarkar
 
LOOPS in C Programming Language - Technology
RishabhDwivedi43
 
Digital Circuits, important subject in CS
contactparinay1
 
The Project Compass - GDG on Campus MSIT
dscmsitkol
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
UiPath DevConnect 2025: Agentic Automation Community User Group Meeting
DianaGray10
 

Cucumber on the JVM with Groovy

  • 1. Cucumber on the JVM with Groovy cucumber, cuke4duke, groovy, geb Richard Paul 2011-03-24
  • 2. What is Groovy • Dynamic language for the JVM • Inspired by Python, Ruby, Smalltalk • Integrates closely with Java • Superset of Java syntax http://groovy.codehaus.org/
  • 3. Why Groovy • Leverage existing Java API knowledge • Integrate closely with production Java code • Expressive language & libraries • Just a 5MB jar on the classpath
  • 4. Groovy Support in Cucumber • Cuke4Duke • Uses JRuby behind the scenes o Pure Java coming later this year • Groovy DSL to automate scenarios https://github.com/aslakhellesoy/cuke4duke
  • 5. Feature Scenario: Regular numbers Given I have entered 3 And I have entered 2 When I press divide Then the result should be 1.5 https://github.com/aslakhellesoy/cuke4duke/tree/master/examples/groovy
  • 6. Given Given(~'I have entered (.*)') { number -> calculator.push number } // Implicit coercion to integer Given(~'I have entered (.*)') { int number -> assert number.class == Integer.class calculator.push number }
  • 7. When When(~'I press (w+)') { operatorName -> result = calculator."$operatorName"() } // Can use slashy strings to avoid escaping When(/I press (w+)/) { operatorName -> result = calculator."$operatorName"() }
  • 8. Then Then(~'the result should be (.*)') { double result -> assert expected == result } // == compares equality // not same instance as in Java
  • 9. Power Assert def now = new Date() def old = Date.parse('yyyyMMdd', '20100101') assert now.date == old.date Assertion failed: assert now.date == old.date | | | | | | 19 | | 1 | | Fri Jan 01 00:00:00 GMT 2010 | false Sat Mar 19 13:18:42 GMT 2011
  • 10. Multiline Strings Given some text """ Line 1 Line 2 """ Given(~'some text') { body -> body.eachLine { println it } } => Line 1 => Line 2
  • 11. Tables Given I have the following foods |name |healthy| |Orange|Yes | |Chips |No | When I count the number of healthy items Then I have 1 healthy item
  • 12. Tables Given(~'I have the following foods') { table -> basket = new Basket() table.hashes().each { def item = new Food( name: it.name, // it.get('name') healthy: it.healthy == 'Yes') basket.add(item) } } When(~'I count the healthy items') { numberOfHealthy = basket.numberOfHealthy } Then(~'I have (.+) healthy items') { int count -> assert numberOfHealthy == count }
  • 13. Tables class Basket { private items = [] void add(item) { items << item } int getNumberOfHealthy() { items.findAll { it.healthy }.size() } } class Food { def name def healthy }
  • 14. Organising Step Definitions Cucumber will read in any .groovy files within step_definitions All steps are then available to any scenarios State can be shared between steps by setting to script binding When(~'I set a variable to the binding') { x = 1 } Then(~'the other step can access variable') { assert x == 1 }
  • 15. Before/After Before { // Initialise something } Before('@tagname') { // Initialise only for features/scenarios // tagged with @tagname } Before('~@tagname') {} // not tagged After { // Clean up something }
  • 16. World Allow simple access to methods from within step definitions World { def world = new Object() world.metaClass.mixin Math world } When(~'we take the square root') { sqrt(4) // calls Math.sqrt(4) }
  • 17. Browser Automation with Geb Given(~'I am on the Wikipedia homepage') { go() } When(~'I search for "(.+)"') { query -> $('#searchInput').value(query) $('.searchButton').click() } Then(~'I am shown the "(.+)" article') { article -> assert $('h1').text() == article }
  • 18. env.groovy for Geb import geb.Browser this.metaClass.mixin(cuke4duke.GroovyDsl) World { new Browser('http://wikipedia.org') } After { clearCookies() }
  • 19. Build Tools Integration with • Maven • Ant Cucumber can write reports in JUnit format for CI reports
  • 20. Discussion/Questions Thanks! http://rapaul.com      @rapaul