functionally testing web apps with geb
TRANSCRIPT
SPRINGONE2GXWASHINGTON, DC
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a Creat ive Commons Attr ibut ion-NonCommercial l icense: ht tp: / /creat ivecommons.org/ l icenses/by-nc/3.0/
Functionally Testing Web Apps with Geb
By Allison Figus@ErinWith2Ls
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Presentation Scope• What is Geb?• Survey of the testing ecosystem
• Where does Geb fit in?• How does Geb do it better?• Selenium vs Geb
• Design Patterns— Page Object, Data Driven
• How to write Functional tests with Geb
2
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Geb is…• A standalone framework for Automated
Functional Testing• A Page Object framework to be used with
another test runner (jUnit, Spock, etc) for Automated Functional Testing
• Spock• jUnit• Cucumber-JVM• TestNg
3
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
What is Spock?• Test framework based on jUnit• Unit, Integration, and Functional tests• Can be used with Geb
4
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Important Groovy Points• Groovy derives from Java, but it is actually an (improper) superset of Java• (Most) Java can be compiled by the Groovy compiler• Groovy can be compiled by the Java compiler⇒ Java and Groovy can be used interchangeably
⇒ A Groovy test suite can be a fully integrated part of a Java project
• When used as a dynamic, duck-typing language, Groovy can be faster to develop in than Java
5
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Geb translates to Selenium WebDriver code
6
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
The Testing Ecosystem1. Unit2. Integration3. External
7
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
The Testing Ecosystem
Internal• Unit• Integration
8
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
The Testing Ecosystem
Internal• Unit• Integration
External• Functional
9
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Functionally Testing an App
10
ApplicationBack-end
AutomatedFunctional
Tests
Manual QA
ApplicationUser Interface
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Functionally Testing a Web App
11
AutomatedFunctional
Tests
Manual QA
Server
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Functionally Testing a Web App
12
AutomatedFunctional
Tests
Manual QA
Server
Geb(Groovy)
PHP?.Net?JVM?
Implementation Agnostic
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
History of Geb
13
WebDriver
2006
Selenium
2004 2010
Geb
2009
SeleniumWebDriver
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
History of Geb
14
WebDriver
2006
Selenium
2004 2010
Geb
2009
SeleniumWebDriver
SeleniumIDE
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Application Under Test— CLUE• Select a Suspect, Room,
and Weapon• Click “Accuse!”• Reset Solution to play
again• View and Create new
Suspects/Rooms/Weapons
15
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Smoke TestGo to the Suspects pageVerify presence of “Suspect List” header
16
<h1>Suspect List</h1>
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 17
Recording with Selenium IDE
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 18
Running Tests with Selenium IDE
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 19
Maintaining Tests with Selenium IDEUI Changes…
⇒
<h1>Suspect List</h1> <h3>Suspect List</h3>
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 20
Maintaining Test with Selenium IDE
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Maintaining Tests with Selenium IDE• No encapsulation• Violation of open-closed principle• No debugger• Doesn’t scale
• Not Data Driven
21
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Next Option: Selenium WebDriver• Encapsulation• Open-Closed• Debug with an IDE• Data Driven testing
22
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
public void testSuspectHeader() {
WebDriver driver = new FirefoxDriver() String baseUrl = "http://localhost:8080" driver.get(baseUrl + "/")
driver.findElement(By.linkText("Suspects")).click()
assert "Suspect List" == driver.findElement(By.cssSelector("h1")).getText()
driver.quit() }
23
Selenium WebDriver Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
public void testSuspectHeader() {
WebDriver driver = new FirefoxDriver() String baseUrl = "http://localhost:8080" driver.get(baseUrl + "/")
driver.findElement(By.linkText("Suspects")).click()
assert "Suspect List" == driver.findElement(By.cssSelector("h1")).getText()
driver.quit() }
24
Selenium WebDriver Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
The WebDriver Interface
25
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
public void testSuspectHeader() {
WebDriver driver = new FirefoxDriver() String baseUrl = "http://localhost:8080" driver.get(baseUrl + "/")
driver.findElement(By.linkText("Suspects")).click()
assert "Suspect List" == driver.findElement(By.cssSelector("h1")).getText()
driver.quit() }
26
Selenium WebDriver Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
public void testSuspectHeader() {
WebDriver driver = new FirefoxDriver() String baseUrl = "http://localhost:8080" driver.get(baseUrl + "/")
driver.findElement(By.linkText("Suspects")).click()
assert "Suspect List" == driver.findElement(By.cssSelector("h1")).getText()
driver.quit() }
27
Selenium WebDriver Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
driver.findElement(By.linkText(“Suspects")).click()
assert "Suspect List" == driver.findElement(By.cssSelector("h1")).getText()
28
Selenium WebDriver
Selenium IDE
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
public void testAllHeaders() { WebDriver driver = new FirefoxDriver() String baseUrl = "http://localhost:8080"
['Suspect', 'Room', 'Weapon'].each { page -> driver.get(baseUrl + '/') driver.findElement(By.linkText("${page}s")).click()
assert "${page} List" == driver.findElement(By.cssSelector("h1")).getText() }
driver.quit() }
29
Data-Driven Selenium WebDriver
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Functional Testing Architecture
How do we design scaleable, maintainable test suites?
The problem:
30
Page Objects are the solution
“if you write tests that manipulate the HTML elements directly your tests will be brittle to changes in the UI”
- Martin Fowler
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 31
Functional Tests Without Page Objects
driver.findElement(By.linkText(“Suspects")).click()
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 32
With Page Objects
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
The Page Object Design Pattern• A layer of abstraction between the test and the HTML
• Provides an easy-to-use interface• Meaningful names for the HTML elements used in test
• Hides “structure” of the HTML page• Allows test to do anything & see anything a human can
33
If the UI changes, the tests themselves don’t need to change
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Page Objects: Good OO Design• Encapsulation
• Interact with web page solely through the Page Object
• DRY• Use the same Page Object from many tests
34
“There is a single ‘repository’ for the services or operationsoffered by the page rather than having these services scattered
throughout the tests” — SeleniumHQ.org
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Selenium WebDriver with Page Objects
35
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
LandingPage landingPage = new LandingPage(driver) SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText()
36
Selenium WebDriver with Page Objects
driver.findElement(By.linkText(“Suspects")).click()
assert "Suspect List" == driver.findElement(By.cssSelector("h1")).getText()
Selenium WebDriver without Page Objects
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
driver.findElement(By.linkText(“Suspects")).click()
⇒ landingPage.toSuspectsPage()
37
Page Objects: Improved Code Clarity
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
38
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
39
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
40
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
41
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
42
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Interacting with Page Object elements• Complexity of interaction
• Finish functions before verification step• Code Clarity/Readability
toSuspectPage
43
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
44
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage { WebDriver driver WebElement suspectsPageLink
LandingPage(WebDriver driver) { this.driver = driver // Sanity Check to see that we're on the right page if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') } suspectsPageLink = driver.findElement(By.linkText("Suspects")) }
SuspectsPage toSuspectsPage() { suspectsPageLink.click() return new SuspectsPage(driver) } }
45
Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Geb Support for Page Objects• Selenium WebDriver Page Objects are just a design pattern• Geb Page Objects are a feature of the “language”
• Reduces boilerplate
46
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
void testSuspectHeaderWithGeb() { Browser browser = new Browser(driver: new FirefoxDriver())
browser.drive { LandingPage landingPage = to LandingPage SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText() }.quit() }
47
Geb Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText()
48
Using Geb Page Object
SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText()
Using Selenium WebDriver Page Object
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
void testSuspectHeaderWithGeb() { Browser browser = new Browser(driver: new FirefoxDriver())
browser.drive { LandingPage landingPage = to LandingPage SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText() }.quit() }
49
Geb Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Browser = WebDriver + “current page”
50
• driver• page
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
void testSuspectHeaderWithGeb() { Browser browser = new Browser(driver: new FirefoxDriver())
browser.drive { LandingPage landingPage = to LandingPage SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText() }.quit() }
51
Geb Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
void testSuspectHeaderWithGeb() { Browser browser = new Browser(driver: new FirefoxDriver())
browser.drive { LandingPage landingPage = to LandingPage SuspectsPage suspectsPage = landingPage.toSuspectsPage() assert "Suspect List" == suspectsPage.getHeaderText() }.quit() }
52
Geb Example
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
browser.drive { go "signup" assert $("h1").text() == "Signup Page" }
53
Browser.drive
Browser browser = new Browser(driver: new FirefoxDriver()) browser.go "signup" assert browser.$("h1").text() == "Signup Page"
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage extends geb.Page {
static url = "http://localhost:8080/" static at = { title == 'Clue Demo'}
static content = { suspectsPageLink(to: SuspectsPage) {$("a", text: "Suspects")} }
SuspectsPage toSuspectsPage() { suspectsPageLink.click()
return browser.page } }
54
Geb Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage extends geb.Page {
static url = "http://localhost:8080/" static at = { title == 'Clue Demo'}
static content = { suspectsPageLink(to: SuspectsPage) {$("a", text: "Suspects")} }
SuspectsPage toSuspectsPage() { suspectsPageLink.click()
return browser.page } }
55
Geb Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
static content = { suspectsPageLink(to: SuspectsPage) { $("a", text: “Suspects") } }
56
Geb Page Object Elements
WebElement suspectsPageLink … suspectsPageLink = driver.findElement(By.linkText("Suspects"))
Selenium WebDriver Page Object Elements
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage extends geb.Page {
static url = "http://localhost:8080/" static at = { title == 'Clue Demo'}
static content = { suspectsPageLink(to: SuspectsPage) {$("a", text: "Suspects")} }
SuspectsPage toSuspectsPage() { suspectsPageLink.click()
return browser.page } }
57
Geb Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
return browser.page
58
Geb Page Object Return Values
return new SuspectsPage(driver)
Selenium WebDriver Page Object Return Values
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage extends geb.Page {
static url = "http://localhost:8080/" static at = { title == 'Clue Demo'}
static content = { suspectsPageLink(to: SuspectsPage) {$("a", text: "Suspects")} }
SuspectsPage toSuspectsPage() { suspectsPageLink.click()
return browser.page } }
59
Geb Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
static at = { title == 'Clue Demo'}
60
Geb Page Object at-checker
if ('Clue Demo' != driver.getTitle()) { throw new IllegalStateException(‘Not the Landing Page') }
Selenium WebDriver at-checker
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
class LandingPage extends geb.Page {
static url = "http://localhost:8080/" static at = { title == 'Clue Demo'}
static content = { suspectsPageLink(to: SuspectsPage) {$("a", text: "Suspects")} }
SuspectsPage toSuspectsPage() { suspectsPageLink.click()
return browser.page } }
61
Geb Page Object Example: LandingPage
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
to LandingPage
62
Geb Page Object URL navigation
driver.get(“http://localhost:8080“)
Selenium WebDriver URL navigation
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 63
Other Ways to Use Geb Page Objectsvoid testSuspectHeaderWithGebAlternative() { Browser browser = new Browser(driver: new FirefoxDriver()) browser.drive { to LandingPage suspectsPageLink.click() assert "Suspect List" == header.text() }.quit() }
Where are the references to the Page Objects?
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
suspectsPageLink.click()vs
landingPage.toSuspectsPage()
64
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/ 65
Why Geb?
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Interacting with Content
• css selector• tag name, id, class
• index• If there are multiple matches
• tag attributes
66
static content = { suspectsPageLink(to: SuspectsPage) { $("a", text: “Suspects") } }
—gebish.org
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Interacting with Content: Text Selectors
• startsWith• contains• endsWith• containsWord
• notStartsWith• notContains• notEndsWith• notContainsWord
67
—gebish.org
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Interacting with Content: Finding and Filtering$("div").find(".b") $("div").$(".b")
68
—gebish.org
$("div").filter(".b") $(".b").not("p")
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Verifying CSS Properties
Select the style
69
<input ... style="background-color: red"/>
assert $("input").css("background-color") == "red"
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
Questions/Comments/Discussion
70
Allison Figus @ErinWith2Ls
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
More Groovy & Spock!
71
Unless otherwise indicated, these sl ides are © 2013-2015 Pivotal Software, Inc. and l icensed under a
Creat ive Commons Attr ibut ion-NonCommercial l icense: http://creativecommons.org/licenses/by-nc/3.0/
The End, Thank You
72