Skip to content

Instantly share code, notes, and snippets.

@kodaitakahashi
Last active June 30, 2017 10:25
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 kodaitakahashi/ebffe3654f4ff5e7d8c7d2823ed408ff to your computer and use it in GitHub Desktop.
Save kodaitakahashi/ebffe3654f4ff5e7d8c7d2823ed408ff to your computer and use it in GitHub Desktop.
Spock+Gebでダイアログ操作
import geb.report.ScreenshotReporter
import org.openqa.selenium.ie.InternetExplorerDriver;
environments {
ie {
driver = { new InternetExplorerDriver() }
}
}
reporter = new ScreenshotReporter(){
@Override
protected escapeFileName(String name) {
name.replaceAll('[\\\\/:\\*?\\"<>\\|]', '_')
}
}
reportsDir = "./report"
import geb.Page
class NextPage extends Page{
static at = { title == 'NextPage' }
}
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>SamplePage</title>
<script>
function btn(){
if(confirm('ダイアログ操作')){
location.href = 'next.html'
}
}
</script>
</head>
<body>
<button id="confirm" onclick="btn()" >ダイアログ</button>
</body>
</html>
import geb.Page
class SamplePage extends Page{
static at = { title == 'SamplePage'}
static url = 'http://path.to.sample.html'
static content = {
btnConfirm {$('#confirm')}
}
}
import geb.spock.GebReportingSpec
class SampleSpec extends GebReportingSPec{
def 'ダイヤログ出現時にOKを選ぶとネクストページに遷移する'(){
when: 'サンプルページに移動'
to SamplePage
then: 'サンプルページに遷移していること'
waitFor{ at SamplePage }
when: 'ダイアログボタンをクリック'
btnConfirm.click()
//ダイアログを操作に切り替え
def confirm = driver.switchTo.alert()
// confirmでtrueの結果を返せる
confirm.accept()
then:'ネクストページに遷移していること'
waitFor{ at NextPage }
}
def 'ダイヤログ出現時にキャンセルを選ぶとネクストページに遷移する'(){
when: 'サンプルページに移動'
to SamplePage
then: 'サンプルページに遷移していること'
waitFor{ at SamplePage }
when: 'ダイアログボタンをクリック'
btnConfirm.click()
//ダイアログを操作に切り替え
def confirm = driver.switchTo.alert()
// confirmでfalseの結果を返せる
confirm.dismiss()
then:'サンプルページにいること'
at SamplePage
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment