Upgrade to Pro — share decks privately, control downloads, hide ads and more …

jQueryのようにWebテストが書ける Geb Navigator APIの紹介 第一回Geb勉強会

jQueryのようにWebテストが書ける Geb Navigator APIの紹介 第一回Geb勉強会

第1回Geb勉強会 What makes Geb groovy ~なぜGebはイケてるのか~
の登壇した時の資料です。
https://geb.connpass.com/event/11030/

YukiFujisawa

January 31, 2015
Tweet

More Decks by YukiFujisawa

Other Decks in Technology

Transcript

  1. 4 自己紹介 名前 ふじさわゆうき 現在の仕事 部署全体を技術的に底上げするチームに所属 テスト自動化、FindBugsルール化、標準開発環境 の提供、ミドルウェア検証、OutOfMemory調査&解 決、社内WIKI導入&運用など幅広く担当 経歴

    高校~大学~社会人とずっとプログラム書いてます 株式投資アルゴリズム(Java)のコンテストで優秀ア ルゴリズム賞を受賞したことがあります なぜかMBAホルダーです
  2. 6 プレゼン概要 対象 これからGebを始める人 Navigator APIについて改めて学びたい人 方針 公式サイトマニュアルに従った内容とする GitHubでNavigator APIの例を提供し、参加者にそ

    のコードを理解してもらうことで、明日から仕事 ですぐに使える知識を提供する ゴール 「便利そうだし、サンプルもあるからGebをやっ てみよう!」と参加した人がなってくれること 「Gebってこんな使い方もできるんだ」と発見が あること
  3. 10 サンプルコード解説の楽しみ方 公式マニュアル The Book Of Gebの「4章 Interacting with content

    」を開きます http://www.gebish.org/manual/current/navigator.html #interacting_with_content この章の順番通りに説明していきます 原文と突き合わせながらプレゼンを聞いて ください! GebStudyのテスト対象は以下のサイト http://www.gebish.org/ https://account.edit.yahoo.co.jp/registration
  4. 14 4. The $ Function $関数は、jQueryのような記法でNavigatorオ ブジェクトを返す関数 Navigatorオブジェクトは、seleniumの WebElementインタフェースを実装している $(

    css selector , ≪ ≫ ≪ index / range , ≫ ≪ attribute / text matchers ) ≫ $("h1", 2, class: "heading") $("div p", 0) $("div p", title: "something") $(title: "something")
  5. 15 4.1.2 Indexes and Ranges コンテンツを単一で取得できる 配列で複数取得することもできる <p>a</p> <p>b</p> <p>c</p>

    $("p", 0).text() == "a" $("p", 2).text() == "c" $("p", 0..1)*.text() = ["a", "b"] $("p", 1..2)*.text() = ["b", "c"]
  6. 17 4.1.3 Attribute and Text Matching HTMLのAttributeとTextにマッチさせること ができる <p attr1="a"

    attr2="b">p1</p> <p attr1="a" attr2="c">p2</p> $("p", attr1: "a").size() == 2 $("p", attr2: "c").size() == 1 $("p", attr1: "a", attr2: "b").size() == 1 $("p", text: "p1").size() == 1 $("p", text: "p1", attr1: "a").size() == 1
  7. 19 4.1.3.1 Using Patterns パターンマッチも可能 <p attr1="a" attr2="b">p1</p> <p attr1="a"

    attr2="c">p2</p> $("p", text: ~/p./).size() == 2 $("p", text: startsWith("p")).size() == 2 $("p", text: endsWith("2")).size() == 1
  8. 20 4.1.3.1 Using Patterns Gebで指定可能なパターンリスト Case Sensitive Description startsWith 指定した文字列で開始する値とマッチする

    contains 指定した文字列を含む値とマッチする endsWith 指定した文字列で終了する値とマッチする containsWord 指定した単語(前後に半角スペースを含む文字 列)を含む値とマッチする notStartsWith 指定した文字列で開始しない値とマッチする notContains 指定した文字列を含まない値とマッチする notEndsWith 指定した文字列で終了しない値とマッチする notContainsWord 指定した単語(前後に半角スペースを含む文字 列)を含まない値とマッチする
  9. 24 4.2 Finding & Filtering “find”, ”$”は子要素を検索するための関数で ある。“filter”, ”not”は、要素を減らすための 関数である

    <div class="a"> <p class="b">geb</p> </div> <div class="b"> <input type="text"/> </div> $("div").find(".b").text() == “geb” $("div").$(".b").text() == “geb” $("div").filter(".b").text() == “” $(".b").not("p").text() == “” $("div").has("p").text() == “geb” $("div").has("input", type: "text").text() == “”
  10. 25 4.2 Finding & Filtering Finding & Filteringのメソッドリスト Method Description

    find 指定したContentのListに対して、指定した条件に合致した それぞれのContentの子ContentのListを取得する $ findと同様の機能 filter 指定したContentのListから、指定した条件に合致しない Contentを除外したListを取得する not 指定したContentのListにから、指定した条件に合致した Contentを除外したListを取得する has 指定したContentのListに対して、指定した条件に合致した 子を持つcontetを取得する closest 指定したContentから一番近くの条件に一致したcontentを 取得する nextUntil nextUntilは、指定したContentから条件に合致するまで List取得する。指定したContentと条件は含まない
  11. 27 4.3 Traversing 検索したコンテンツ前後のコンテンツと マッチさせることができる <div class="a"> <div class="b"> <p

    class="c"></p> <p class="d"></p> <p class="e"></p> </div> <div class="f"></div> </div> $("p.d").previous() // 'p.c' $("p.e").prevAll() // ['p.c' , 'p.d] $("p.d").next() // 'p.e' $("p.c").nextAll() // ['p.d' , 'p.e'] $("p.d").parent() // 'div.b' $("p.d").siblings() // ['p.c' , 'p.e'] $("div.a").children() // ['div.b' , 'div.f]
  12. 28 4.3 Traversing Traversingのメソッドリスト Method Description previous 指定したcontentの一つ前を取得する prevAll 指定したcontentの前をListで全て取得する

    next 指定したcontentの一つ後を取得する nextAll 指定したcontentのをListで全て取得する parent 指定したcontentの親contentを取得する siblings 指定したcontent以外をListで全て取得する children 指定したcontentの子contentを配列で取得する
  13. 36 4.7 Size and Location PageのSizeとLocationを取得できる LocationはPageの左上からのx , yプロパティ でpixel指定する

    $("div").height == 20 $("div").width == 40 $("div").x == 60 $("div").y == 80 $("div")*.height == [20, 30] $("div")*.width == [40, 50] $("div")*.x == [60, 70] $("div")*.y == [80, 90]
  14. 38 4.8 Accessing tag name, attributes, text and classes Navigator

    objectsのtag(), text(), @attribute and classes()を使って値を取得できる classes()は、class属性を java.util.List形式 で返す <p title="a" class="a para">a</p> <p title="b" class="b para">b</p> <p title="c" class="c para">c</p> $("p").text() == "a" $("p").tag() == "p" $("p").@title == "a" $("p").classes() == ["a", "para"]
  15. 44 4.11 Accessing input values Input, select, textarea など入力値は、value メソッドでセットすることができる

    checkbox は、booleanをセットすることも できる multiple select は配列をセットすることもで きる
  16. 46 4.12 Form Control Shortcuts Input, selectなどform要素への入力に対して 簡単に入力できるようにしている <form> <input

    type="text" name="geb" value="testing" /> </form> $("form").geb == "testing" $("form").geb = "goodness" $("form").geb == "goodness"
  17. 47 4.12 Form Control Shortcuts <select name="artist"> <option value="1">Ima Robot</option>

    <option value="2">Edward Sharpe</option> <option value="3">Alexander</option> </select> $("form").artist = "1" //Ima Robot $("form").artist = 2 //Edward Sharpe $("form").artist = "Alexander" //Alexander selectタグは、value, textまたは順番の値を 渡すことで選択できる
  18. 48 4.12 Form Control Shortcuts <select name="genres" multiple> <option value="1">Alt

    folk</option> <option value="2">Chiptunes</option> <option value="3">Electroclash</option> <option value="4">G-Funk</option> <option value="5">Hair metal</option> </select> $("form").genres = ["2", "3"] $("form").genres = [1, 4, 5] $("form").genres = ["Alt folk", "Hair metal"] $("form").genres = [] multiple Selectタグも同様に、value, textま たは順番の配列を渡すことで選択できる
  19. 49 4.12 Form Control Shortcuts <input type="checkbox" name="pet" value="dogs" />

    <input type="checkbox" name="pet" value="cats" /> $("input", type: "checkbox", name: "pet").value("dogs") $("input", type: "checkbox", name: "pet").value() == "dogs" $("input", type: "checkbox", name: "pet").value() == false if ($("input", type: "checkbox", name: "pet").value()) { //petが選択されていれば実行される } checkboxは、valueで選択することができる 未選択の場合は、falseが返ってくる
  20. 50 4.12 Form Control Shortcuts <label for="site-current">Search this site</label> <input

    type="radio" id="site-current" name="site" value="current"> <label>Search Google <input type="radio" name="site" value="google"> </label> $("form").site = "current" //Search this site $("form").site = "Search this site" //Search this site $("form").site = "Search Google" //Search Google RadioButtonは、valueまたはlabelのtextで選択す ることができる
  21. 51 4.12 Form Control Shortcuts <input name="postcode" /> ("form").postcode =

    "12345" $("form").postcode() << Keys.BACK_SPACE assert $("form").postcode == "1234" Text Inputは、文字列を渡すことで入力できる キーストロークもKeysクラスを使うことで入力 できる
  22. 52 4.12 Form Control Shortcuts <input type="file" name="csvFile"> //絶対パスでファイルを指定する $("form").csvFile

    = "/path/to/my/file.csv" 絶対パスを渡すことでファイルのアップロード もできる
  23. 54 4.13.2 Using Actions WebDriver driverのActionクラスを使うこと で複雑な動作も実行することができる def actions =

    new Actions(driver) WebElement someItem = $('li.clicky').firstElement() def shiftDoubleClickAction = actions.keyDown(Keys.SHIFT).doubleClick(someItem). keyUp(Keys.SHIFT).build() shiftDoubleClickAction.perform()
  24. 55 4.13.3 Using Interact Closures Interact Closuresを使うと、Actionよりも簡 単に実装できる interact {

    keyDown(Keys.SHIFT) doubleClick($('li.clicky')) keyUp(Keys.SHIFT) } interact { keyDown(Keys.CONTROL) click($('ul.multiselect li', text: 'Order 1')) click($('ul.multiselect li', text: 'Order 2')) click($('ul.multiselect li', text: 'Order 3')) keyUp(Keys.CONTROL) }