42
Better Selenium Tests with Geb Naresha K Enteleki Solutions [email protected] @naresha_k

Better Selenium Tests with Geb - Selenium Conf 2014

Embed Size (px)

DESCRIPTION

Slide of my Selenium Conference 2014 2014 - Sept 6, 2014.

Citation preview

Page 1: Better Selenium Tests with Geb - Selenium Conf 2014

Better Selenium Tests with GebNaresha K

Enteleki Solutions [email protected]

@naresha_k

Page 2: Better Selenium Tests with Geb - Selenium Conf 2014

WebDriver

ChromeDriver FirefoxDriver InternetExplorerDriver

http://martinfowler.com/bliki/PageObject.html

Page 3: Better Selenium Tests with Geb - Selenium Conf 2014

WebDriver

ChromeDriver FirefoxDriver InternetExplorerDriver

Selenium server

WebDriverJS

Page 4: Better Selenium Tests with Geb - Selenium Conf 2014

https://www.flickr.com/photos/pagedooley/3028798210

Level of Abstraction

Page 5: Better Selenium Tests with Geb - Selenium Conf 2014

WebDriver

ChromeDriver FirefoxDriver InternetExplorerDriver

Selenium server

WebDriverJS

Page 6: Better Selenium Tests with Geb - Selenium Conf 2014

Any problem in computer science can be solved with another

layer of indirection

David Wheeler

https://www.flickr.com/photos/pc_plod/14187378533

Page 7: Better Selenium Tests with Geb - Selenium Conf 2014
Page 8: Better Selenium Tests with Geb - Selenium Conf 2014

Web Driver

Page 9: Better Selenium Tests with Geb - Selenium Conf 2014

Geb

Page 10: Better Selenium Tests with Geb - Selenium Conf 2014

Browser

import geb.Browser!import org.openqa.selenium.firefox.FirefoxDriver!!Browser browser = new Browser(driver: new FirefoxDriver())!

Page 11: Better Selenium Tests with Geb - Selenium Conf 2014

Browserimport 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/'!

Page 12: Better Selenium Tests with Geb - Selenium Conf 2014

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()!

Page 13: Better Selenium Tests with Geb - Selenium Conf 2014

Accessing Elements// driver.findElement(By.name("j_username")) !def username = browser.$(name: 'j_username')!

// username.sendKeys("user1")!username << 'user1'!

println username.value()!

Page 14: Better Selenium Tests with Geb - Selenium Conf 2014

Geb Browser

Page 15: Better Selenium Tests with Geb - Selenium Conf 2014

Hello GebBrowser 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()!

Page 16: Better Selenium Tests with Geb - Selenium Conf 2014

Hello Geb - Improved

Browser.drive{!! go “http://localhost:8000/app/login.html"!! $(name: 'j_username') << 'user1'!! $(name: 'j_password') << 'secret'!! $('#submit').click()!}.quit()!

Page 17: Better Selenium Tests with Geb - Selenium Conf 2014

Configurable URL

Browser.drive{!! go “login.html”!! $(name: 'j_username') << 'user1'!! $(name: 'j_password') << 'secret'!! $('#submit').click()!}.quit()!

// GebConfig.groovy!baseUrl = "http://localhost:8000/app/" !

Page 18: Better Selenium Tests with Geb - Selenium Conf 2014

Assertion

assert $('h1').text() == 'Dashboard'!

Page 19: Better Selenium Tests with Geb - Selenium Conf 2014

Navigator API

Page 20: Better Selenium Tests with Geb - Selenium Conf 2014

Navigator Syntax

$(<css selector>, <index or range>, <attribute / text matchers>)

Page 21: Better Selenium Tests with Geb - Selenium Conf 2014

<h2>Introduction</h2>!<h2>Navigator</h2>!<h2>Page Objects</h2>!<h2>Summary</h2>!

$('h2').text() == 'Introduction'!

$('h2', 1).text() == 'Navigator'!

$('h2').size() == 4!

Page 22: Better Selenium Tests with Geb - Selenium Conf 2014

<h2>Introduction</h2>!<h2>Navigator</h2>!<h2>Page Objects</h2>!<h2>Summary</h2>!

$('h2', 0..2)*.text() == !! ! ['Introduction', 'Navigator', 'Page Objects']!

Page 23: Better Selenium Tests with Geb - Selenium Conf 2014

<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!

Page 24: Better Selenium Tests with Geb - Selenium Conf 2014

<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!

Page 25: Better Selenium Tests with Geb - Selenium Conf 2014

<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

Page 26: Better Selenium Tests with Geb - Selenium Conf 2014

<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 27: Better Selenium Tests with Geb - Selenium Conf 2014

Page Objects

Page 28: Better Selenium Tests with Geb - Selenium Conf 2014

Page Objects

Page 29: Better Selenium Tests with Geb - Selenium Conf 2014

Modules

Page 30: Better Selenium Tests with Geb - Selenium Conf 2014

Modules

Page 31: Better Selenium Tests with Geb - Selenium Conf 2014
Page 32: Better Selenium Tests with Geb - Selenium Conf 2014

Modulesclass 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')}!! }!}

Page 33: Better Selenium Tests with Geb - Selenium Conf 2014

Modules

Browser.drive() {!! to ProductPage!! products.each{ product ->!! ! println "${product.productCode} -> ${product.price}"!! }!}.quit()

Page 34: Better Selenium Tests with Geb - Selenium Conf 2014

Modules List

Page 35: Better Selenium Tests with Geb - Selenium Conf 2014

Waiting

Page 36: Better Selenium Tests with Geb - Selenium Conf 2014

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! }! }!}

Page 37: Better Selenium Tests with Geb - Selenium Conf 2014

Integration

https://www.flickr.com/photos/lumaxart/2137737248

Page 38: Better Selenium Tests with Geb - Selenium Conf 2014

Supported Frameworks

Page 39: Better Selenium Tests with Geb - Selenium Conf 2014

@Stepwise!class SampleGebSpec extends GebReportingSpec{!! def "User can login"(){! !! when:! !! ! to LoginPage!! ! ! login('user1', 'secret')! ! ! then:!! ! ! at DashboardPage!! ! ! and:!! ! ! header.pageTitle == 'Dashboard'! }!!}!

Spock Example

Page 40: Better Selenium Tests with Geb - Selenium Conf 2014

Integration

Page 41: Better Selenium Tests with Geb - Selenium Conf 2014

• Power of WebDriver • Elegance of jQuery selection • Robustness of Page Object

modeling • Expressiveness of Groovy

Summary

Welcome Geb

Page 42: Better Selenium Tests with Geb - Selenium Conf 2014

ReferencesOfficial 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