more details in speaker notes
open popup with [s].
package tk.hildebrandt.geb
class Example {
}
class Example {
def foo;
String bar;
def doFoo(def foo){
def bar
}
boolean doBar(){
bar
}
}
@Test
void callWithList(){
withList([1, 1, 2, 3, 5])
}
void withList(def list) {
list.each {
entry ->
println "${entry}"
}
list.eachWithIndex {
entry, index ->
println "${index}: ${entry}"
}
}
void callWithList(){
withList([1, 1, 2, 3, 5])
}
void withList(def list) {
println list*.toString()
}
void callWithMap() {
withMap(test: '123', name: '324')
}
void withMap(def map) {
map.each {
key, value ->
println "${key}: ${value}"
}
}
void callWithClosure() {
assert withClosure {
final boolean result = 1 == 1
println "within closure ${result}"
result
}
}
def withClosure(Closure<Boolean> closure) {
closure.call()
}
final String test = "test"
assert "test" == test && "test2" == test
Ausgabe
assert "test" == test && "test2" == test
| | | | |
| test false | test
true false
Browser.drive {
go "http://google.com/"
}
$("input", name: "q")
$("li.g", 0).find("a.l")
$("p", text: "Hans")
$("p", endsWith: "aus")
$("input", name: "q").value("wikipedia")
$("select").value("wikipedia", "google")
assert $("h1").text() == "Wikipedia"
assert $("h1").parent().@src.contains("wikipedia")
$("button").click()
$(By.id("some-id"))
$(By.className("some-class"))
$(By.xpath('//p[@class="xpath"]'))
@Test
public void testSearchWithPageAndModule(){
go 'https://www.bing.com'
assert title == 'Bing'
$('input', name: 'q').value("wikipedia")
$(By.id('sb_form_go')).click()
waitFor { title.endsWith(' - Bing') }
assert $('li.b_algo a', 0).text() == "wikipedia.de - Wikipedia, die freie Enzyklopädie";
}
GoogleResultsPage doSomething(Closure closure) { .
closure.call(this)
this
}
List<GoogeResultModule> results;
googleResultsPage
.doSomething (
{ GoogleResultsPage page ->
results = page.listResults()
})
.goToNextPage()
class BingStartPage extends Page {
static url = 'https://www.bing.com'
static at = { title == 'Bing' }
static content = {
searchField { $('input', name: 'q') }
searchButton(to: BingResultsPage) {
$(By.id('sb_form_go'))
}
}
}
@Test
public void testPage(){
to BingStartPage
assert at(BingStartPage)
searchField.value("wikipedia")
searchButton.click()
waitFor { at BingResultsPage }
assert searchFirstResultLink.text() == "wikipedia.de - Wikipedia, die freie Enzyklopädie";
}
static at = { title == 'Bing' }
static url = 'https://www.bing.com'
static content = {
searchField { $('input', name: 'q') }
searchFirstResultLink { $('li.b_algo a', 0) }
searchButton(to: BingResultsPage) { $(By.id('sb_form_go')) }
}
public BingResultsPage search(String query){
searchField.value(query)
searchButton.click()
waitFor { browser.at BingResultsPage }
browser.page(BingResultsPage)
}
BingResultsPage assertFirstResultLinkText(String expected) {
assert searchFirstResultLink.text() == expected
this
}
T doOnPage(Closure closure) {
Closure cloned = closure.clone()
cloned.delegate = browser
cloned.resolveStrategy = Closure.DELEGATE_FIRST
cloned.call()
this
}
public void testDoOnPage() {
String link = null
to(BingStartPage)
...
.doOnPage ({
link = searchFirstResultLink.@href
})
}
static content = {
dynamicallyAdded(wait: true) { $("p.dynamic") }
dynamicallyAddedOrNot(wait: true, required: false)
{ $("p.dynamicButOptional") }
dynamicallyAddedOrNot(wait: "ajax", required: false)
{ $("p.dynamicButOptional") }
}
//Exception nach dem Wait-Timeout
assert dynamicallyAdded.text() == "I'm here now"
assert dynamicallyAddedOrNot.text() == "I'm here now"
//nach dem Wait-Timeout ist das Value null
|| dynamicallyAddedOrNot.value() == null
waitFor { title.endsWith("Google Search") }
waitFor { at GoogleResultsPage }
waitFor 30 { at GoogleResultsPage }
waitFor ("searchResult", { at GoogleResultsPage })
static content = {
searchField { $('input', name: 'q').module(TextInput) }
}
class BingResultModule extends Module {
static content = {
header { $('h2') }
link { $('h2>a') }
}
}
static content = {
searchField { nameParameter -> $('input', name: nameParameter) }
}
static content = {
results { $('li.b_algo').moduleList(BingResultModule)}
searchFirstResultLink { $('li.b_algo a', 0) }
}
BingResultsPage assertFirstResultLinkText(String expected) {
assert results.get(0).link.text() == expected
this
}
BingResultsPage search(String query){
searchField.setQuery(query)
searchButton.jquery.click()
waitFor { browser.at BingResultsPage }
browser.page(BingResultsPage)
}
driver = "ie"
driver = "chome"
driver = {
def ffDriver = new FirefoxDriver()
ffDriver.manage().window().maximize()
return ffDriver
}
waiting {
timeout = 10
retryInterval = 0.5
presets {
ajax {
timeout = 3
retryInterval = 0.5
}
}
}
environments {
'phantomjs' {
driver = {
final capabilities = new DesiredCapabilities()
capabilities.setCapability("javascriptEnabled", true)
final phantomJSDriver = new PhantomJSDriver(capabilities)
phantomJSDriver
.manage()
.window()
.setSize(new Dimension(1028, 768))
return phantomJSDriver
}
}
}
mvn -Dgeb.env=phantomjs test
interact {
keyDown(Keys.SHIFT)
doubleClick($('li.clicky'))
keyUp(Keys.SHIFT)
}
interact {
clickAndHold($('#element'))
moveByOffset(400, -150)
release()
}
interact {
dragAndDropBy($('#element'), 400, -150)
}
withWindow('MyNewTab') {
$('#MyNewTabCloseButton').click()
}
class ExamplePageAndModuleSpec extends geb.spock.GebSpec {
def "test search with page and module"() {
when: {
to GoogleStartPage
search.searchField.value("wikipedia")
waitFor { at GoogleResultsPage }
}
then: {
searchFirstResultLink.text() == "Wikipedia"
}
}
}