Skip to content

Instantly share code, notes, and snippets.

@h-mochizuki
Last active April 26, 2018 06:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save h-mochizuki/7606864 to your computer and use it in GitHub Desktop.
Save h-mochizuki/7606864 to your computer and use it in GitHub Desktop.
Gebのサンプル
@Grab(group='org.gebish', module='geb-core', version='2.1')
@Grab(group='org.seleniumhq.selenium', module='selenium-api', version='3.11.0')
@Grab(group='org.seleniumhq.selenium', module='selenium-firefox-driver', version='3.11.0')
import geb.*
System.properties['webdriver.gecko.driver'] = './geckodriver.exe'
class ThisGistPage extends Page {
// 何故か this.url が空になるのでブラウザから現在の url を取得
String getCurrentUrl() {
browser.driver.currentUrl
}
static at = {
currentUrl =~ /gist.github.com/ && title =~ /Gebのサンプル/
}
static content = {
viewRaw { $('#file-gebsample-groovy > div.file-header > div.file-actions > a') }
}
}
Browser.drive {
go 'https://gist.github.com/h-mochizuki/7606864'
assert at(ThisGistPage)
viewRaw.click()
// raw ファイルをダウンロードして比較
assert new File('./gebSample.groovy').text == page.downloadText()
// これを入れないとテストが終わらない
quit()
}
@Grab(group='org.codehaus.geb', module='geb-core', version='0.7.2')
@Grab(group='org.seleniumhq.selenium', module='selenium-api', version='2.37.1')
@Grab(group='org.seleniumhq.selenium', module='selenium-firefox-driver', version='2.37.1')
import geb.*
/**
* Google の検索処理を行う部分を共通化
* これができるのも Google がちゃんと部品化しているおかげですね
*/
class GoogleSearchModule extends Module {
def buttonValue
static content = {
field { $('input', name: 'q') }
button(to: GoogleResultPage) {
$('input', value: buttonValue)
}
}
}
/**
* Google のホームページ
*/
class GoogleHomePage extends Page {
static url = 'https://www.google.co.jp/'
static at = { title == 'Google' }
static content = {
// 部品化された検索部分
// ボタンの値が違うのでそこだけ変更している
search { module GoogleSearchModule, buttonValue: 'Google 検索' }
}
}
class GoogleResultPage extends Page {
static at = { title.endsWith 'Google 検索' }
static content = {
// 部品化された検索部分
search { module GoogleSearchModule, buttonValue: '検索' }
// 検索結果のリスト
results { $("li.g") }
// 引数に数字をとって、i番目の検索結果を取得する
result { i -> results[i] }
// 検索結果のうちのリンク部分
resultLink { i -> result(i).find("a em") }
// 最初の検索結果のリンク部分
firstResultLink { resultLink(0) }
}
}
/**
* Wikipedia のホームページ
*/
class WikipediaPage extends Page {
static at = { title == "Wikipedia" }
}
// テスト開始
Browser.drive {
// Googleホームページで検索
to GoogleHomePage
assert at(GoogleHomePage)
search.field.value 'wikipedia'
search.button.click()
// 検索結果ページが出てくるので、最初のリンクを押す
waitFor { at GoogleResultPage }
assert firstResultLink.text() == 'ウィキペディア'
firstResultLink.click()
// Wikipediaのページ表示が遅いので10秒くらい待ってみる
waitFor(10) { at WikipediaPage }
// ブラウザを閉じる
quit()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment