Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Better Selenium Tests with Geb - Selenium Conf 2014

4,326 views

Published on

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

Published in: Software

Better Selenium Tests with Geb - Selenium Conf 2014

  1. 1. Better Selenium Tests with Geb Naresha K Enteleki Solutions naresha.k@gmail.com @naresha_k
  2. 2. http://martinfowler.com/bliki/PageObject.html WebDriver ChromeDriver FirefoxDriver InternetExplorerDriver
  3. 3. WebDriver WebDriverJS Selenium server ChromeDriver FirefoxDriver InternetExplorerDriver
  4. 4. Level of Abstraction https://www.flickr.com/photos/pagedooley/3028798210
  5. 5. WebDriver WebDriverJS Selenium server ChromeDriver FirefoxDriver InternetExplorerDriver
  6. 6. Any problem in computer science can be solved with another layer of indirection David Wheeler https://www.flickr.com/photos/pc_plod/14187378533
  7. 7. Web Driver
  8. 8. Geb
  9. 9. Browser import geb.Browser! import org.openqa.selenium.firefox.FirefoxDriver! ! Browser browser = new Browser(driver: new FirefoxDriver())!
  10. 10. 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/'!
  11. 11. 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()!
  12. 12. Accessing Elements // driver.findElement(By.name("j_username")) ! def username = browser.$(name: 'j_username')! // username.sendKeys("user1")! username << 'user1'! println username.value()!
  13. 13. Geb Browser
  14. 14. 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()!
  15. 15. Hello Geb - Improved Browser.drive{! ! go “http://localhost:8000/app/login.html"! ! $(name: 'j_username') << 'user1'! ! $(name: 'j_password') << 'secret'! ! $('#submit').click()! }.quit()!
  16. 16. 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()!
  17. 17. Assertion assert $('h1').text() == 'Dashboard'!
  18. 18. Navigator API
  19. 19. Navigator Syntax $(<css selector>, <index or range>, <attribute / text matchers>)
  20. 20. <h2>Introduction</h2>! <h2>Navigator</h2>! <h2>Page Objects</h2>! <h2>Summary</h2>! $('h2').text() == 'Introduction'! $('h2', 1).text() == 'Navigator'! $('h2').size() == 4!
  21. 21. <h2>Introduction</h2>! <h2>Navigator</h2>! <h2>Page Objects</h2>! <h2>Summary</h2>! $('h2', 0..2)*.text() == ! ! ! ['Introduction', 'Navigator', 'Page Objects']!
  22. 22. <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!
  23. 23. <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!
  24. 24. <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
  25. 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> $('.language').filter('.jvm').each{ element ->! ! ! println element.text()! } Java Groovy $('.language').not('.clr').each{ element ->! ! ! println element.text()! } Java Groovy Elixir
  26. 26. Page Objects
  27. 27. Page Objects
  28. 28. Modules
  29. 29. Modules
  30. 30. 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')}! ! }! }
  31. 31. Modules Browser.drive() {! ! to ProductPage! ! products.each{ product ->! ! ! println "${product.productCode} -> ${product.price}"! ! }! }.quit()
  32. 32. Modules List
  33. 33. Waiting
  34. 34. 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! }! }! }
  35. 35. Integration https://www.flickr.com/photos/lumaxart/2137737248
  36. 36. Supported Frameworks
  37. 37. @Stepwise! class SampleGebSpec extends GebReportingSpec{! ! def "User can login"(){! !! when:! !! ! to LoginPage! ! ! ! login('user1', 'secret')! ! ! then:! ! ! ! at DashboardPage! ! ! ! and:! ! ! ! header.pageTitle == 'Dashboard'! }! ! }! Spock Example
  38. 38. Integration
  39. 39. Summary • Power of WebDriver • Elegance of jQuery selection • Robustness of Page Object modeling • Expressiveness of Groovy Welcome Geb
  40. 40. 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

×