Better Selenium Tests with Geb - Selenium Conf 2014

Naresha K
Naresha KDeveloper | Technical Excellence Coach | Consultant | Trainer at Independent
Better Selenium Tests with 
Geb 
Naresha K 
Enteleki Solutions 
naresha.k@gmail.com 
@naresha_k
http://martinfowler.com/bliki/PageObject.html 
WebDriver 
ChromeDriver FirefoxDriver InternetExplorerDriver
WebDriver 
WebDriverJS 
Selenium server 
ChromeDriver FirefoxDriver InternetExplorerDriver
Level of Abstraction 
https://www.flickr.com/photos/pagedooley/3028798210
WebDriver 
WebDriverJS 
Selenium server 
ChromeDriver FirefoxDriver InternetExplorerDriver
Any problem in 
computer science can 
be solved with another 
layer of indirection 
David Wheeler 
https://www.flickr.com/photos/pc_plod/14187378533
Better Selenium Tests with Geb - Selenium Conf 2014
Web Driver
Geb
Browser 
import geb.Browser! 
import org.openqa.selenium.firefox.FirefoxDriver! 
! 
Browser browser = new Browser(driver: new FirefoxDriver())!
Browser 
import geb.Browser! 
import org.openqa.selenium.firefox.FirefoxDriver! 
! 
Browser browser = new Browser(driver: new FirefoxDriver())! 
// driver.get("http://seleniumconf.org/")! 
browser.go 'http://seleniumconf.org/'!
External Config 
// GebConfig.groovy! 
import org.openqa.selenium.firefox.FirefoxDriver! 
! 
driver = { ! 
! def driverInstance = new FirefoxDriver() ! 
! driverInstance.manage().window().maximize() ! 
! driverInstance ! 
} ! 
Browser browser = new Browser()! 
! 
// driver.get("http://seleniumconf.org/")! 
browser.go 'http://seleniumconf.org/'! 
browser.quit()!
Accessing Elements 
// driver.findElement(By.name("j_username")) ! 
def username = browser.$(name: 'j_username')! 
// username.sendKeys("user1")! 
username << 'user1'! 
println username.value()!
Geb Browser
Hello Geb 
Browser browser = new Browser()! 
browser.go “http://localhost:8000/app/login.html"! 
browser.$(name: 'j_username') << 'user1'! 
browser.$(name: 'j_password') << 'secret'! 
browser.$('#submit').click()! 
browser.quit()!
Hello Geb - Improved 
Browser.drive{! 
! go “http://localhost:8000/app/login.html"! 
! $(name: 'j_username') << 'user1'! 
! $(name: 'j_password') << 'secret'! 
! $('#submit').click()! 
}.quit()!
Configurable URL 
// GebConfig.groovy! 
baseUrl = "http://localhost:8000/app/" ! 
Browser.drive{! 
! go “login.html”! 
! $(name: 'j_username') << 'user1'! 
! $(name: 'j_password') << 'secret'! 
! $('#submit').click()! 
}.quit()!
Assertion 
assert $('h1').text() == 'Dashboard'!
Navigator API
Navigator Syntax 
$(<css selector>, <index or range>, <attribute / text matchers>)
<h2>Introduction</h2>! 
<h2>Navigator</h2>! 
<h2>Page Objects</h2>! 
<h2>Summary</h2>! 
$('h2').text() == 'Introduction'! 
$('h2', 1).text() == 'Navigator'! 
$('h2').size() == 4!
<h2>Introduction</h2>! 
<h2>Navigator</h2>! 
<h2>Page Objects</h2>! 
<h2>Summary</h2>! 
$('h2', 0..2)*.text() == ! 
! ! ['Introduction', 'Navigator', 'Page Objects']!
<h2 duration="5">Introduction</h2>! 
<h2 duration="15">Navigator</h2>! 
<h2>Page Objects</h2>! 
<h2 duration="5">Summary</h2>! 
$('h2', duration: '5').size() == 2! 
$('h2', text: 'Summary').size() == 1!
<h2 duration="5">Introduction</h2>! 
<h2 duration="15">Navigator</h2>! 
<h2>Page Objects</h2>! 
<h2 duration="5">Summary</h2>! 
$('h2', text: contains('o')).size() == 2! 
$('h2', text: iContains('o')).size() == 3! 
$('h2', duration: contains('5')).size() == 3!
<div class="languages">! 
! ! <div class="language jvm">Java</div>! 
! ! <div class="language clr">C#</div>! 
! ! <div class="language jvm">Groovy</div>! 
! ! <div class="language clr">F#</div>! 
! ! <div class="language erlang">Elixir</div>! 
</div> 
$('div.languages').find('.jvm').each{ element ->! 
! ! println element.text()! 
} 
Java 
Groovy
<div class="languages">! 
! ! <div class="language jvm">Java</div>! 
! ! <div class="language clr">C#</div>! 
! ! <div class="language jvm">Groovy</div>! 
! ! <div class="language clr">F#</div>! 
! ! <div class="language erlang">Elixir</div>! 
</div> 
$('.language').filter('.jvm').each{ element ->! 
! ! println element.text()! 
} 
Java 
Groovy 
$('.language').not('.clr').each{ element ->! 
! ! println element.text()! 
} 
Java 
Groovy 
Elixir
Page Objects
Page Objects
Modules
Modules
Better Selenium Tests with Geb - Selenium Conf 2014
Modules 
class Record extends Module{! 
! static content = {! 
! ! column {index -> $('td', index)}! 
! ! productCode {column(1).text()}! 
! ! price { column(2).text().toInteger()}! 
! }! 
} 
class ProductPage extends Page{! 
! static url = 'table.html'! 
! static content = {! 
! ! products {moduleList Record, $('table tbody tr')}! 
! }! 
}
Modules 
Browser.drive() {! 
! to ProductPage! 
! products.each{ product ->! 
! ! println "${product.productCode} -> ${product.price}"! 
! }! 
}.quit()
Modules List
Waiting
Wait 
<div id="dynamic"></div> 
waitFor { $('#dynamic').text()}! 
waitFor(8) { $('#dynamic').text()}! 
waitFor(8, 0.5) { $('#dynamic').text()}! 
waitFor('slow') { $('#dynamic').text()} 
// GebConfig.groovy! 
waiting {! 
presets {! 
slow {! 
timeout = 12! 
retryInterval = 1! 
}! 
}! 
}
Integration 
https://www.flickr.com/photos/lumaxart/2137737248
Supported Frameworks
@Stepwise! 
class SampleGebSpec extends GebReportingSpec{! 
! 
def "User can login"(){! 
!! when:! 
!! ! to LoginPage! 
! ! ! login('user1', 'secret')! 
! 
! then:! 
! ! ! at DashboardPage! 
! ! ! and:! 
! ! ! header.pageTitle == 'Dashboard'! 
}! 
! 
}! 
Spock Example
Integration
Summary 
• Power of WebDriver 
• Elegance of jQuery selection 
• Robustness of Page Object 
modeling 
• Expressiveness of Groovy 
Welcome Geb
References 
Official Geb Page - http://www.gebish.org/ 
! 
Example - https://github.com/geb/geb-example-gradle 
! 
Spock Documentation - http://spock-framework. 
readthedocs.org/en/latest/ 
! 
Code samples - https://github.com/naresha/seconf2014
1 of 42

Recommended

Pragmatic Browser Automation with Geb - GIDS 2015 by
Pragmatic Browser Automation with Geb - GIDS 2015Pragmatic Browser Automation with Geb - GIDS 2015
Pragmatic Browser Automation with Geb - GIDS 2015Naresha K
1.8K views49 slides
jQuery in 15 minutes by
jQuery in 15 minutesjQuery in 15 minutes
jQuery in 15 minutesSimon Willison
41.5K views14 slides
Introduction to jQuery by
Introduction to jQueryIntroduction to jQuery
Introduction to jQuerymanugoel2003
2.2K views36 slides
Introduction to jQuery by
Introduction to jQueryIntroduction to jQuery
Introduction to jQueryGunjan Kumar
2.8K views45 slides
JQuery introduction by
JQuery introductionJQuery introduction
JQuery introductionNexThoughts Technologies
1.6K views26 slides
Drupal Best Practices by
Drupal Best PracticesDrupal Best Practices
Drupal Best Practicesmanugoel2003
5.4K views47 slides

More Related Content

What's hot

Learning jQuery in 30 minutes by
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutesSimon Willison
53.4K views31 slides
jQuery by
jQueryjQuery
jQueryMostafa Bayomi
9.5K views77 slides
Prototype & jQuery by
Prototype & jQueryPrototype & jQuery
Prototype & jQueryRemy Sharp
37K views25 slides
jQuery Essentials by
jQuery EssentialsjQuery Essentials
jQuery EssentialsBedis ElAchèche
2.8K views83 slides
Javascript first-class citizenery by
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenerytoddbr
1.2K views32 slides
jQuery Essentials by
jQuery EssentialsjQuery Essentials
jQuery EssentialsMarc Grabanski
173.8K views115 slides

What's hot(20)

Learning jQuery in 30 minutes by Simon Willison
Learning jQuery in 30 minutesLearning jQuery in 30 minutes
Learning jQuery in 30 minutes
Simon Willison53.4K views
Prototype & jQuery by Remy Sharp
Prototype & jQueryPrototype & jQuery
Prototype & jQuery
Remy Sharp37K views
Javascript first-class citizenery by toddbr
Javascript first-class citizeneryJavascript first-class citizenery
Javascript first-class citizenery
toddbr1.2K views
Sprout core and performance by Yehuda Katz
Sprout core and performanceSprout core and performance
Sprout core and performance
Yehuda Katz1.4K views
jQuery Fundamentals by Gil Fink
jQuery FundamentalsjQuery Fundamentals
jQuery Fundamentals
Gil Fink5.4K views
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K by Thomas Fuchs
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2KZepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Zepto.js, a jQuery-compatible mobile JavaScript framework in 2K
Thomas Fuchs13.6K views
jQuery Presentation by Rod Johnson
jQuery PresentationjQuery Presentation
jQuery Presentation
Rod Johnson4.7K views
A Short Introduction To jQuery by Sudar Muthu
A Short Introduction To jQueryA Short Introduction To jQuery
A Short Introduction To jQuery
Sudar Muthu2.5K views
JavaScript and jQuery Basics by Kaloyan Kosev
JavaScript and jQuery BasicsJavaScript and jQuery Basics
JavaScript and jQuery Basics
Kaloyan Kosev3.5K views

Viewers also liked

Cloud browser testing with Gradle and Geb by
Cloud browser testing with Gradle and GebCloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and GebDavid Carr
2.1K views7 slides
Design Patterns from 10K feet by
Design Patterns from 10K feetDesign Patterns from 10K feet
Design Patterns from 10K feetNaresha K
424 views17 slides
Java beyond Java - from the language to platform by
Java beyond Java - from the language to platformJava beyond Java - from the language to platform
Java beyond Java - from the language to platformNaresha K
284 views35 slides
Geb presentation by
Geb presentationGeb presentation
Geb presentationIvar Østhus
1.6K views19 slides
What makes Geb groovy? by
What makes Geb groovy?What makes Geb groovy?
What makes Geb groovy?Youtarou TAKAHASHI
8.7K views157 slides
Geb with spock by
Geb with spockGeb with spock
Geb with spockMonika Gurram
38.9K views14 slides

Viewers also liked(6)

Cloud browser testing with Gradle and Geb by David Carr
Cloud browser testing with Gradle and GebCloud browser testing with Gradle and Geb
Cloud browser testing with Gradle and Geb
David Carr2.1K views
Design Patterns from 10K feet by Naresha K
Design Patterns from 10K feetDesign Patterns from 10K feet
Design Patterns from 10K feet
Naresha K424 views
Java beyond Java - from the language to platform by Naresha K
Java beyond Java - from the language to platformJava beyond Java - from the language to platform
Java beyond Java - from the language to platform
Naresha K284 views

Similar to Better Selenium Tests with Geb - Selenium Conf 2014

jQuery basics by
jQuery basicsjQuery basics
jQuery basicsStijn Van Minnebruggen
651 views61 slides
What you need to know bout html5 by
What you need to know bout html5What you need to know bout html5
What you need to know bout html5Kevin DeRudder
973 views113 slides
Creating GUI container components in Angular and Web Components by
Creating GUI container components in Angular and Web ComponentsCreating GUI container components in Angular and Web Components
Creating GUI container components in Angular and Web ComponentsRachael L Moore
493 views144 slides
J query b_dotnet_ug_meet_12_may_2012 by
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012ghnash
1.1K views40 slides
HTML5 New and Improved by
HTML5   New and ImprovedHTML5   New and Improved
HTML5 New and ImprovedTimothy Fisher
6.8K views46 slides
HTML5 Essentials by
HTML5 EssentialsHTML5 Essentials
HTML5 EssentialsMarc Grabanski
43.6K views62 slides

Similar to Better Selenium Tests with Geb - Selenium Conf 2014(20)

What you need to know bout html5 by Kevin DeRudder
What you need to know bout html5What you need to know bout html5
What you need to know bout html5
Kevin DeRudder973 views
Creating GUI container components in Angular and Web Components by Rachael L Moore
Creating GUI container components in Angular and Web ComponentsCreating GUI container components in Angular and Web Components
Creating GUI container components in Angular and Web Components
Rachael L Moore493 views
J query b_dotnet_ug_meet_12_may_2012 by ghnash
J query b_dotnet_ug_meet_12_may_2012J query b_dotnet_ug_meet_12_may_2012
J query b_dotnet_ug_meet_12_may_2012
ghnash1.1K views
Building iPhone Web Apps using "classic" Domino by Rob Bontekoe
Building iPhone Web Apps using "classic" DominoBuilding iPhone Web Apps using "classic" Domino
Building iPhone Web Apps using "classic" Domino
Rob Bontekoe1.6K views
Resource Registries: Plone Conference 2014 by Rob Gietema
Resource Registries: Plone Conference 2014Resource Registries: Plone Conference 2014
Resource Registries: Plone Conference 2014
Rob Gietema571 views
Practical HTML5: Using It Today by Doris Chen
Practical HTML5: Using It TodayPractical HTML5: Using It Today
Practical HTML5: Using It Today
Doris Chen972 views
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group) by Doris Chen
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
jQuery Makes Writing JavaScript Fun Again (for HTML5 User Group)
Doris Chen9.5K views
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012 by crokitta
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
Oracle Application Express & jQuery Mobile - OGh Apex Dag 2012
crokitta1.7K views
Enjoy the vue.js by TechExeter
Enjoy the vue.jsEnjoy the vue.js
Enjoy the vue.js
TechExeter1.4K views

More from Naresha K

The Groovy Way of Testing with Spock by
The Groovy Way of Testing with SpockThe Groovy Way of Testing with Spock
The Groovy Way of Testing with SpockNaresha K
240 views40 slides
Evolving with Java - How to Remain Effective by
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain EffectiveNaresha K
243 views74 slides
Take Control of your Integration Testing with TestContainers by
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersNaresha K
268 views50 slides
Implementing Resilience with Micronaut by
Implementing Resilience with MicronautImplementing Resilience with Micronaut
Implementing Resilience with MicronautNaresha K
344 views20 slides
Take Control of your Integration Testing with TestContainers by
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainersNaresha K
281 views21 slides
Favouring Composition - The Groovy Way by
Favouring Composition - The Groovy WayFavouring Composition - The Groovy Way
Favouring Composition - The Groovy WayNaresha K
229 views48 slides

More from Naresha K(20)

The Groovy Way of Testing with Spock by Naresha K
The Groovy Way of Testing with SpockThe Groovy Way of Testing with Spock
The Groovy Way of Testing with Spock
Naresha K240 views
Evolving with Java - How to Remain Effective by Naresha K
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain Effective
Naresha K243 views
Take Control of your Integration Testing with TestContainers by Naresha K
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainers
Naresha K268 views
Implementing Resilience with Micronaut by Naresha K
Implementing Resilience with MicronautImplementing Resilience with Micronaut
Implementing Resilience with Micronaut
Naresha K344 views
Take Control of your Integration Testing with TestContainers by Naresha K
Take Control of your Integration Testing with TestContainersTake Control of your Integration Testing with TestContainers
Take Control of your Integration Testing with TestContainers
Naresha K281 views
Favouring Composition - The Groovy Way by Naresha K
Favouring Composition - The Groovy WayFavouring Composition - The Groovy Way
Favouring Composition - The Groovy Way
Naresha K229 views
Effective Java with Groovy - How Language Influences Adoption of Good Practices by Naresha K
Effective Java with Groovy - How Language Influences Adoption of Good PracticesEffective Java with Groovy - How Language Influences Adoption of Good Practices
Effective Java with Groovy - How Language Influences Adoption of Good Practices
Naresha K294 views
What's in Groovy for Functional Programming by Naresha K
What's in Groovy for Functional ProgrammingWhat's in Groovy for Functional Programming
What's in Groovy for Functional Programming
Naresha K275 views
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo... by Naresha K
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Effective Java with Groovy & Kotlin - How Languages Influence Adoption of Goo...
Naresha K208 views
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ... by Naresha K
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Effective Java with Groovy & Kotlin How Languages Influence Adoption of Good ...
Naresha K162 views
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro... by Naresha K
Eclipse Collections, Java Streams & Vavr - What's in them for  Functional Pro...Eclipse Collections, Java Streams & Vavr - What's in them for  Functional Pro...
Eclipse Collections, Java Streams & Vavr - What's in them for Functional Pro...
Naresha K499 views
Implementing Cloud-Native Architectural Patterns with Micronaut by Naresha K
Implementing Cloud-Native Architectural Patterns with MicronautImplementing Cloud-Native Architectural Patterns with Micronaut
Implementing Cloud-Native Architectural Patterns with Micronaut
Naresha K402 views
Groovy - Why and Where? by Naresha K
Groovy  - Why and Where?Groovy  - Why and Where?
Groovy - Why and Where?
Naresha K79 views
Leveraging Micronaut on AWS Lambda by Naresha K
Leveraging Micronaut on AWS LambdaLeveraging Micronaut on AWS Lambda
Leveraging Micronaut on AWS Lambda
Naresha K310 views
Groovy Refactoring Patterns by Naresha K
Groovy Refactoring PatternsGroovy Refactoring Patterns
Groovy Refactoring Patterns
Naresha K514 views
Implementing Cloud-native Architectural Patterns with Micronaut by Naresha K
Implementing Cloud-native Architectural Patterns with MicronautImplementing Cloud-native Architectural Patterns with Micronaut
Implementing Cloud-native Architectural Patterns with Micronaut
Naresha K667 views
Effective Java with Groovy by Naresha K
Effective Java with GroovyEffective Java with Groovy
Effective Java with Groovy
Naresha K501 views
Evolving with Java - How to remain Relevant and Effective by Naresha K
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
Naresha K294 views
Effective Java with Groovy - How Language can Influence Good Practices by Naresha K
Effective Java with Groovy - How Language can Influence Good PracticesEffective Java with Groovy - How Language can Influence Good Practices
Effective Java with Groovy - How Language can Influence Good Practices
Naresha K311 views
Beyond Lambdas & Streams - Functional Fluency in Java by Naresha K
Beyond Lambdas & Streams - Functional Fluency in JavaBeyond Lambdas & Streams - Functional Fluency in Java
Beyond Lambdas & Streams - Functional Fluency in Java
Naresha K186 views

Recently uploaded

Introduction to Gradle by
Introduction to GradleIntroduction to Gradle
Introduction to GradleJohn Valentino
7 views7 slides
Transport Management System - Shipment & Container Tracking by
Transport Management System - Shipment & Container TrackingTransport Management System - Shipment & Container Tracking
Transport Management System - Shipment & Container TrackingFreightoscope
6 views3 slides
Automated Testing of Microsoft Power BI Reports by
Automated Testing of Microsoft Power BI ReportsAutomated Testing of Microsoft Power BI Reports
Automated Testing of Microsoft Power BI ReportsRTTS
11 views20 slides
Playwright Retries by
Playwright RetriesPlaywright Retries
Playwright Retriesartembondar5
7 views1 slide
Google Solutions Challenge 2024 Talk pdf by
Google Solutions Challenge 2024 Talk pdfGoogle Solutions Challenge 2024 Talk pdf
Google Solutions Challenge 2024 Talk pdfMohdAbdulAleem4
34 views17 slides
Supercharging your Python Development Environment with VS Code and Dev Contai... by
Supercharging your Python Development Environment with VS Code and Dev Contai...Supercharging your Python Development Environment with VS Code and Dev Contai...
Supercharging your Python Development Environment with VS Code and Dev Contai...Dawn Wages
5 views51 slides

Recently uploaded(20)

Transport Management System - Shipment & Container Tracking by Freightoscope
Transport Management System - Shipment & Container TrackingTransport Management System - Shipment & Container Tracking
Transport Management System - Shipment & Container Tracking
Freightoscope 6 views
Automated Testing of Microsoft Power BI Reports by RTTS
Automated Testing of Microsoft Power BI ReportsAutomated Testing of Microsoft Power BI Reports
Automated Testing of Microsoft Power BI Reports
RTTS11 views
Google Solutions Challenge 2024 Talk pdf by MohdAbdulAleem4
Google Solutions Challenge 2024 Talk pdfGoogle Solutions Challenge 2024 Talk pdf
Google Solutions Challenge 2024 Talk pdf
MohdAbdulAleem434 views
Supercharging your Python Development Environment with VS Code and Dev Contai... by Dawn Wages
Supercharging your Python Development Environment with VS Code and Dev Contai...Supercharging your Python Development Environment with VS Code and Dev Contai...
Supercharging your Python Development Environment with VS Code and Dev Contai...
Dawn Wages5 views
tecnologia18.docx by nosi6702
tecnologia18.docxtecnologia18.docx
tecnologia18.docx
nosi67026 views
predicting-m3-devopsconMunich-2023-v2.pptx by Tier1 app
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptx
Tier1 app14 views
University of Borås-full talk-2023-12-09.pptx by Mahdi_Fahmideh
University of Borås-full talk-2023-12-09.pptxUniversity of Borås-full talk-2023-12-09.pptx
University of Borås-full talk-2023-12-09.pptx
Mahdi_Fahmideh12 views
Top-5-production-devconMunich-2023-v2.pptx by Tier1 app
Top-5-production-devconMunich-2023-v2.pptxTop-5-production-devconMunich-2023-v2.pptx
Top-5-production-devconMunich-2023-v2.pptx
Tier1 app9 views
Introduction to Git Source Control by John Valentino
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source Control
John Valentino8 views
predicting-m3-devopsconMunich-2023.pptx by Tier1 app
predicting-m3-devopsconMunich-2023.pptxpredicting-m3-devopsconMunich-2023.pptx
predicting-m3-devopsconMunich-2023.pptx
Tier1 app10 views
aATP - New Correlation Confirmation Feature.pptx by EsatEsenek1
aATP - New Correlation Confirmation Feature.pptxaATP - New Correlation Confirmation Feature.pptx
aATP - New Correlation Confirmation Feature.pptx
EsatEsenek1222 views

Better Selenium Tests with Geb - Selenium Conf 2014

  • 1. Better Selenium Tests with Geb Naresha K Enteleki Solutions naresha.k@gmail.com @naresha_k
  • 3. WebDriver WebDriverJS Selenium server ChromeDriver FirefoxDriver InternetExplorerDriver
  • 4. Level of Abstraction https://www.flickr.com/photos/pagedooley/3028798210
  • 5. WebDriver WebDriverJS Selenium server ChromeDriver FirefoxDriver InternetExplorerDriver
  • 6. Any problem in computer science can be solved with another layer of indirection David Wheeler https://www.flickr.com/photos/pc_plod/14187378533
  • 9. Geb
  • 10. Browser import geb.Browser! import org.openqa.selenium.firefox.FirefoxDriver! ! Browser browser = new Browser(driver: new FirefoxDriver())!
  • 11. Browser import geb.Browser! import org.openqa.selenium.firefox.FirefoxDriver! ! Browser browser = new Browser(driver: new FirefoxDriver())! // driver.get("http://seleniumconf.org/")! browser.go 'http://seleniumconf.org/'!
  • 12. External Config // GebConfig.groovy! import org.openqa.selenium.firefox.FirefoxDriver! ! driver = { ! ! def driverInstance = new FirefoxDriver() ! ! driverInstance.manage().window().maximize() ! ! driverInstance ! } ! Browser browser = new Browser()! ! // driver.get("http://seleniumconf.org/")! browser.go 'http://seleniumconf.org/'! browser.quit()!
  • 13. Accessing Elements // driver.findElement(By.name("j_username")) ! def username = browser.$(name: 'j_username')! // username.sendKeys("user1")! username << 'user1'! println username.value()!
  • 15. Hello Geb Browser browser = new Browser()! browser.go “http://localhost:8000/app/login.html"! browser.$(name: 'j_username') << 'user1'! browser.$(name: 'j_password') << 'secret'! browser.$('#submit').click()! browser.quit()!
  • 16. Hello Geb - Improved Browser.drive{! ! go “http://localhost:8000/app/login.html"! ! $(name: 'j_username') << 'user1'! ! $(name: 'j_password') << 'secret'! ! $('#submit').click()! }.quit()!
  • 17. Configurable URL // GebConfig.groovy! baseUrl = "http://localhost:8000/app/" ! Browser.drive{! ! go “login.html”! ! $(name: 'j_username') << 'user1'! ! $(name: 'j_password') << 'secret'! ! $('#submit').click()! }.quit()!
  • 20. Navigator Syntax $(<css selector>, <index or range>, <attribute / text matchers>)
  • 21. <h2>Introduction</h2>! <h2>Navigator</h2>! <h2>Page Objects</h2>! <h2>Summary</h2>! $('h2').text() == 'Introduction'! $('h2', 1).text() == 'Navigator'! $('h2').size() == 4!
  • 22. <h2>Introduction</h2>! <h2>Navigator</h2>! <h2>Page Objects</h2>! <h2>Summary</h2>! $('h2', 0..2)*.text() == ! ! ! ['Introduction', 'Navigator', 'Page Objects']!
  • 23. <h2 duration="5">Introduction</h2>! <h2 duration="15">Navigator</h2>! <h2>Page Objects</h2>! <h2 duration="5">Summary</h2>! $('h2', duration: '5').size() == 2! $('h2', text: 'Summary').size() == 1!
  • 24. <h2 duration="5">Introduction</h2>! <h2 duration="15">Navigator</h2>! <h2>Page Objects</h2>! <h2 duration="5">Summary</h2>! $('h2', text: contains('o')).size() == 2! $('h2', text: iContains('o')).size() == 3! $('h2', duration: contains('5')).size() == 3!
  • 25. <div class="languages">! ! ! <div class="language jvm">Java</div>! ! ! <div class="language clr">C#</div>! ! ! <div class="language jvm">Groovy</div>! ! ! <div class="language clr">F#</div>! ! ! <div class="language erlang">Elixir</div>! </div> $('div.languages').find('.jvm').each{ element ->! ! ! println element.text()! } Java Groovy
  • 26. <div class="languages">! ! ! <div class="language jvm">Java</div>! ! ! <div class="language clr">C#</div>! ! ! <div class="language jvm">Groovy</div>! ! ! <div class="language clr">F#</div>! ! ! <div class="language erlang">Elixir</div>! </div> $('.language').filter('.jvm').each{ element ->! ! ! println element.text()! } Java Groovy $('.language').not('.clr').each{ element ->! ! ! println element.text()! } Java Groovy Elixir
  • 32. Modules class Record extends Module{! ! static content = {! ! ! column {index -> $('td', index)}! ! ! productCode {column(1).text()}! ! ! price { column(2).text().toInteger()}! ! }! } class ProductPage extends Page{! ! static url = 'table.html'! ! static content = {! ! ! products {moduleList Record, $('table tbody tr')}! ! }! }
  • 33. Modules Browser.drive() {! ! to ProductPage! ! products.each{ product ->! ! ! println "${product.productCode} -> ${product.price}"! ! }! }.quit()
  • 36. Wait <div id="dynamic"></div> waitFor { $('#dynamic').text()}! waitFor(8) { $('#dynamic').text()}! waitFor(8, 0.5) { $('#dynamic').text()}! waitFor('slow') { $('#dynamic').text()} // GebConfig.groovy! waiting {! presets {! slow {! timeout = 12! retryInterval = 1! }! }! }
  • 39. @Stepwise! class SampleGebSpec extends GebReportingSpec{! ! def "User can login"(){! !! when:! !! ! to LoginPage! ! ! ! login('user1', 'secret')! ! ! then:! ! ! ! at DashboardPage! ! ! ! and:! ! ! ! header.pageTitle == 'Dashboard'! }! ! }! Spock Example
  • 41. Summary • Power of WebDriver • Elegance of jQuery selection • Robustness of Page Object modeling • Expressiveness of Groovy Welcome Geb
  • 42. References Official Geb Page - http://www.gebish.org/ ! Example - https://github.com/geb/geb-example-gradle ! Spock Documentation - http://spock-framework. readthedocs.org/en/latest/ ! Code samples - https://github.com/naresha/seconf2014