public
Last active

Geb + Spock + Eclipse

  • Download Gist
GooglePage.groovy
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
import geb.Browser
import geb.Module
import geb.Page
 
// modules are reusable fragments that can be used across pages that can be paramaterised
// here we are using a module to model the search function on the home and results pages
class GoogleSearchModule extends Module {
 
// a paramaterised value set when the module is included
def buttonValue
 
// the content DSL
static content = {
 
// name the search input control “field”, defining it with the jQuery like navigator
field { $("input", name: "q") }
 
// the search button declares that it takes us to the results page, and uses the
// parameterised buttonValue to define itself
button(to: GoogleResultsPage) {
$("input", value: buttonValue)
}
}
}
 
class GoogleHomePage extends Page {
 
// pages can define their location, either absolutely or relative to a base
static url = "http://google.com/ncr"
 
// “at checkers” allow verifying that the browser is at the expected page
static at = { title == "Google" }
 
static content = {
// include the previously defined module
search { module GoogleSearchModule, buttonValue: "Google Search" }
}
}
 
class GoogleResultsPage extends Page {
static at = { title.endsWith "Google Search" }
static content = {
// reuse our previously defined module
search { module GoogleSearchModule, buttonValue: "Search" }
 
// content definitions can compose and build from other definitions
results { $("li.g") }
result { i -> results[i] }
resultLink { i -> result(i).find("a.l") }
firstResultLink { resultLink(0) }
}
}
 
class WikipediaPage extends Page {
static at = { title == "Wikipedia" }
}
GooglePageTest.groovy
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
import geb.spock.GebSpec
 
class GoogleWikipediaSpec extends GebSpec {
 
def "first result for wikipedia search should be wikipedia"() {
given:
to GoogleHomePage
 
expect:
at GoogleHomePage
 
when:
search.field.value("wikipedia")
 
then:
waitFor { at GoogleResultsPage }
 
and:
firstResultLink.text() == "Wikipedia"
 
when:
firstResultLink.click()
 
then:
waitFor { at WikipediaPage }
}
}
build.gradle
Groovy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
apply plugin: "groovy"
apply plugin: "idea"
apply plugin: "eclipse"
 
repositories {
mavenCentral()
}
 
// The drivers we want to use
ext.drivers = ["firefox", "chrome"]
 
dependencies {
groovy "org.codehaus.groovy:groovy-all:2.0.4"
 
def gebVersion = "0.9.0-RC-1"
def seleniumVersion = "2.26.0"
 
// If using Spock, need to depend on geb-spock
testCompile "org.gebish:geb-spock:$gebVersion"
testCompile "org.spockframework:spock-core:0.7-groovy-2.0"
 
// If using JUnit, need to depend on geb-junit (3 or 4)
testCompile "org.gebish:geb-junit4:$gebVersion"
testCompile "junit:junit-dep:4.11"
 
// Drivers
drivers.each { driver ->
testCompile "org.seleniumhq.selenium:selenium-$driver-driver:$seleniumVersion"
}
}
 
drivers.each { driver ->
task "${driver}Test"(type: Test) {
testReportDir = reporting.file("$name/tests")
testResultsDir = file("$buildDir/test-results/$name")
 
systemProperty "geb.build.reportsDir", reporting.file("$name/geb")
systemProperty "geb.env", driver
systemProperty "webdriver.chrome.driver", "C:\\workspace\\test-automation\\chromedriver.exe"
 
// If you wanted to set the baseUrl in your build…
// systemProperty "geb.build.baseUrl", "http://myapp.com"
}
}
 
task test(overwrite: true, dependsOn: drivers.collect { tasks["${it}Test"] })
 
task wrapper(type: Wrapper) {
gradleVersion = '1.3'
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.