// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();
compile ":geb:0.9.2"
BuildConfig.groovy
dependencies {
test("org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion")
test("org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion")
// You usually only need one of these, but this project uses both
test "org.gebish:geb-spock:$gebVersion"
test "org.gebish:geb-junit4:$gebVersion"
}
GebConfig.groovy
driver = { new ChromeDriver() }
environments {
// run as “grails -Dgeb.env=chrome test-app”
// See: http://code.google.com/p/selenium/wiki/ChromeDriver
chrome {
driver = { new ChromeDriver() }
}
// run as “grails -Dgeb.env=firefox test-app”
// See: http://code.google.com/p/selenium/wiki/FirefoxDriver
firefox {
driver = { new FirefoxDriver() }
}
}
$("a", class: "brand")
$("div.some-class p:first[title='something']")
$("div.footer").find(".copyright")
click()
$("input", name: firstName) << asdf
$("input", name: "firstName") << Keys.chord(Keys.CONTROL, "c")
interact {
clickAndHold($('#element'))
moveByOffset(400, -150)
release()
}
waitFor {} // use default configuration // wait for up to 10 seconds, using the default retry interval waitFor(10) {} // wait for up to 10 seconds, waiting half a second in between retries waitFor(10, 0.5) {} // use the preset “quick” as the wait settings waitFor("quick") {}
Browser.drive {
$("input", value: "Make Request")
waitFor { $("div#result").present }
assert $("div#result").text() == "The Result"
}
js."document.title" == "Book of Geb"
js.gloallyVisibleJavascriptFunction(1,2)
js.exec(1, 2, "return arguments[0] + arguments[1];") == 3
js.exec 'jQuery("div#a").mouseover();'
$("div#a").jquery.mouseover()
class GoogleHomePage extends Page {
static url = "http://google.com/?complete=0"
static at = { title == "Google" }
static content = {
searchField { $("input[name=q]") }
searchButton(to: GoogleResultsPage) { $("input[value='Google Search']") }
}
void search(String searchTerm) {
searchField.value searchTerm
searchButton.click()
}
}
class GoogleResultsPage extends Page { ... }
Browser.drive {
to GoogleHomePage
search "Chuck Norris"
at GoogleResultsPage
resultLink(0).text().contains("Chuck")
}
class GoogleHomePage extends Page { static url = "http://google.com/?complete=0" static at = { title == "Google" } static content = { searchField { $("input[name=q]") } searchButton(to: GoogleResultsPage) {
$("input[value='Google Search']")
} } void search(String searchTerm) { searchField.value searchTerm searchButton.click() } }
page.searchField
class ExampleModule extends Module {
static content = {
button { $("input", type: "submit") }
}
}
class ExamplePage extends Page {
static content = {
theModule { module ExampleModule }
}
}
Browser.drive {
reportGroup "google"
go "http://google.com"
report "home page"
reportGroup "wikipedia"
go "http://wikipedia.org"
report "home page"
}
/target/test-reports/geb/
${grails.project.test.reports.dir}/geb
compile ":remote-control:1.5"