Geb Script

出典: フリー百科事典『ウィキペディア(Wikipedia)』

2020年10月2日 (金) 16:40; Semi-Brace (会話 | 投稿記録) による版 (bulk replace (ns=0, <source> → <syntaxhighlight>) via script)(日時は個人設定で未設定ならUTC

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)

Geb Scriptは、Groovy言語で書かれたブラウザ自動化フレームワークである[1]

Selenium WebDriverで挙がっている問題(例えば、ログインボタンを押すだけでも多くのプログラミングが必要など)を解決するために誕生した。

Gebのメリット[編集]

出典[1]

  • Selenium WebDriverと比較して、より短く、よりわかりやすいテストを記述することができる。Gebの提供するナビゲーターAPI(Navigator API)というjQueryのようなAPIがそれを可能にしている。
  • Page Object patternをサポートしているので画面変更に強いテストを簡単に作成することができる。
  • Mavenで提供されるのでテスト作成の環境構築が簡単にできる。
  • GroovyなのでJavaと互換性があり、SeleniumWebDriverの既存資産をそのまま利用することができる。
  • Moduleを使うことで共通部分を部品化することができる。

Navigator APIとは[編集]

出典[2]

  • jQueryのような文法でコンテンツ内容を取得するAPIをGebでは、"Navigator API"と呼んでいる。
  • "$"関数を使うことでCSSセレクタを基にしてコンテンツ内容を取得することができる。
// CSS 3 selectors
$("div.some-class p:first[title='something']")
 
// index and/or attributeをマッチングすることで検索する
$("h1", 2, class: "heading")
$("p", name: "description")
$("ul.things li", 2)
 
//"text"は、テキスト内容要素のための特別な属性である
$("h1", text: "All about Geb")
 
// Gebが提供するmatcherや正規表現でも取得が可能
$("p", text: contains("Geb"))
$("input", value: ~/\d{3,}-\d{3,}-\d{3,}/)
 
// Chaining
$("div").find(".b")
$("div").filter(".c").parents()
$("p.c").siblings()

サンプルコード[編集]

出典[3]

以下、コードを実行することで「Google検索⇒検索結果⇒Wikipedia」という流れのWebテストが実施できる。

import geb.Browser
 
Browser.drive {
    go "http://google.com/ncr"
 
    // make sure we actually got to the page
    assert title == "Google"
 
    // enter wikipedia into the search field
    $("input", name: "q").value("wikipedia")
 
    // wait for the change to results page to happen
    // (google updates the page dynamically without a new request)
    waitFor { title.endsWith("Google Search") }
 
    // is the first link to wikipedia?
    def firstLink = $("li.g", 0).find("a")
    assert firstLink.text() == "Wikipedia"
 
    // click the link 
    firstLink.click()
 
    // wait for Google's javascript to redirect to Wikipedia
    waitFor { title == "Wikipedia" }
}

脚注[編集]