目前已經與友人,將相關資料整理編譯成一本電子書,因為在鐵人賽時時間比較趕,有些錯誤修正的部分,直接參考電子書,就不同步修正了http://learngeb.readbook.tw/。
30天的鐵人競賽,終於抵達終點了,覺得這個比賽很有趣,其實寫到第15天後,就會開始詞窮了,真的要很認真的找很多資料,並著手寫一些合適的範例,可以強迫自己再把技術玩得更detail一點。<br />
<br />
<br />
<br />
* 使用變數<br />
<br />
<pre name="code" class="c">
<script type="text/javascript">
var aVariable = 1;
</script>
</pre><br />
<br />
<pre name="code" class="c">
assert js.aVariable == 1</pre><br />
<br />
* 呼叫方法(method)<br />
<br />
<pre name="code" class="c">
<script type="text/javascript">
function add(a,b) {
return a + b;
}
</script>
</pre><br />
<br />
<pre name="code" class="c">
assert js.add(1, 1) == 2</pre><br />
<br />
* 使用原生JS<br />
<br />
<pre name="code" class="c">
assert js."document.title" == "Geb"</pre><br />
<br />
一般常見的對話視窗有alert、confirm、prompt, <a href="http://www.gebish.org/manual/current/api/geb/js/AlertAndConfirmSupport.html">AlertAndConfirmSupport </a>這個類別涵蓋了前兩項,由於Geb不鼓勵使用prompt對話視窗,所以不支持這個功能。<br />
<br />
* String withAlert(Closure actions)<br />
* void withNoAlert(Closure actions)<br />
* void withNoConfirm(Closure actions)<br />
* String withConfirm(boolean ok, Closure actions)<br />
* ok參數預設為true,可省略<br />
<br />
* JavascriptSpec<br />
<br />
<pre name="code" class="c">
package example
import geb.Page
import geb.spock.GebReportingSpec
/**
* @see geb.js.AlertAndConfirmSupport
*/
class JavascriptSpec extends GebReportingSpec{
def setup() {
to JavascriptPage
}
def "withAlert"(){
expect:
withAlert(wait: true) { showAlert.click() } == "Hello World!"
}
def "withNoAlert"(){
expect:
withNoAlert { showAlert.click() }
// java.lang.AssertionError: an unexpected browser alert() was raised (message: Hello World!)
}
def "withConfirm"(){
expect:
withConfirm(true) { showConfirm.click() } == "Do you like Geb?"
}
}
class JavascriptPage extends Page{
static url = "javascript.html"
static content = {
showAlert {$("input", name: "showAlert")}
showConfirm {$("input", name: "showConfirm")}
}
}</pre><br />
<br />
* javascript.html<br />
<br />
<pre name="code" class="c">
<head lang="en">
<meta charset="UTF-8">
<title></title>
<input type="button" name="showAlert" onclick="alert('Hello World!');" value="showAlert"/>
<input type="button" name="showConfirm" onclick="confirm('Do you like Geb?');" value="showConfirm"/>
</pre>