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

横浜タネマキでGebと握手! #yokohamagroovy

横浜タネマキでGebと握手! #yokohamagroovy

Yokohama.groovy #42でのGebのハンズオンの資料です。

PoohSunny

July 30, 2016
Tweet

More Decks by PoohSunny

Other Decks in Technology

Transcript

  1. @PoohSunny { work: "σΟϕϩούʔ" geb: "very minor contributor" community: [

    "TDDBC", "Agile Samurai Base Camp", "೔ຊSeleniumϢʔβʔίϛϡχςΟ" "Yokohama.groovy" <- NEW! ] }
  2. ؆ܿͳهड़1 import geb.Browser Browser.drive { go "http://myapp.com/login" assert $("h1").text() ==

    "Please Login" $("form.login").with { username = "admin" password = "password" login().click() } assert $("h1").text() == "Admin Section" } 1 http://www.gebish.org/
  3. ϓϩδΣΫτͷσΟϨΫτϦ ߏ੒ root |-- src | `-- test | |--

    groovy | | `- xxxSpecs.groovy | `-- resources | `-- GebConfig.groovy `-- build.gradle
  4. build.gradle ґଘؔ܎ͷఆٛ3 dependencies { // ུ // ඞཁͳυϥΠόΛͦΕͧΕґଘؔ܎ʹ௥Ճ testCompile "org.seleniumhq.selenium:selenium-chrome-driver:$seleniumVersion"

    testCompile("com.codeborne:phantomjsdriver:1.2.1") { // phantomjs driver pulls in a different selenium version transitive = false } } 3 ͳͥɺWebDriverΛͦΕͧΕґଘؔ܎ʹ௥Ճ͢Δͷ͔ʹ͍ͭͯ͸ɺhttps://groups.google.com/d/ msg/geb-user/Og40o5mXK-4/tga5H2zuRRYJ
  5. ࿅शͦͷ2 ԼهͷॲཧΛ௥Ճͯ͠Έ·͠ΐ͏ɻ • ϗʔϜϖʔδ͔ΒɺϚχϡΞϧ΁ͷϦϯΫΛΫ ϦοΫ • ϚχϡΞϧϖʔδʢThe book of Gebʣ͕දࣔ

    ͞ΕΔ͔ΛνΣοΫ • step-2ͬͯϒϥϯνʹɺҰ෦ຒ·ͬͯΔςετ ͕͋ΔͷͰɺͦΕΛຒΊͯ΋Β͏ͱ͍͍͔ͱɻ • ώϯτ͕ཉ͍͠ਓ͸࣍ͷϖʔδ΁
  6. jQueryͬΆ͍API4 $("div") // ࠷ॳʹݟ͔ͭͬͨ div ཁૉ $("div", name: "main") //

    <div name="main"></div> $("div", 1, name: "main") // ೋ൪໨ͷ<div name="main"></div> 4 http://www.gebish.org/manual/current/#the-jquery-ish-navigator-api
  7. ͜Μͳॻ͖ํ΋Մ $("#user-name") // user-name ͱ͍͏id͕ࢦఆ͞Ε͍ͯΔཁૉ $(".btn") // btn ͱ͍͏class͕ࢦఆ͞Ε͍ͯΔཁૉ ཁૉΛ୳͢ͷ͸Chrome

    Developer tool + jQueryͰ΍ͬͯɺͦΕΛGebʹίϐ ϖ͢Δͱ͔Α͘΍Γ·͢ νʔϜͰهड़͕όϥόϥͩͱ΍΍͘͜͠ͳΔͷͰɺܾΊ͓ͯ͘ͱྑ͍Ͱ͢
  8. click manualsMenu.links[0].click() ϖʔδΦϒδΣΫτͷఆٛͷ࢓ํ͍Ζ͍Ζ content { // to: ͜ͷΤϨϝϯτ͕ΫϦοΫ͞Εͨͱ͖ʹConfirmPageΛ౉͢ // toWait:

    ࣍ͷϖʔδͷatνΣοΫ͕trueʹͳΔ·Ͱ଴ͭ button(to: ConfirmPage, toWait: true) { $('button#save') } }
  9. Կ͕͍͚ͳ͍ʁ interact { // ఆ͕ٛ௕͍ moveToElement($("#header-content ul li", 0).children("span")) }

    // ఆٛͷॏෳ $("#header-content ul li", 0).$('.link-list li a')[0].click()
  10. GebͷϖʔδΦϒδΣΫτ PageΫϥεΛܧঝ class GebishOrgHomePage extends Page { static at =

    { title == "Sample page" } static content = { header { $("#header") } manualsMenu { module MenuModule, $("#header", 0) } } }
  11. ࠶ར༻Մೳͳύʔπ͸Moduleʹ import geb.Module class MenuModule extends Module { static content

    = { toggle { children("span") } links { $('.link-list li a') } } }
  12. url7 to()ϝιουͰݺΜͩ࣌ʹભҠ͢ΔઌΛઃఆ class PageWithUrl extends Page { static url =

    "example" } ૬ରύεͰهड़͢Δͱ͖͸ɺbaseUrlͷઃఆ͕ඞཁ8 8 http://www.gebish.org/manual/current/#base-url 7 http://www.gebish.org/manual/current/#page-urls
  13. Page, Module͸ઐ༻ͷσΟϨΫτϦ ʹ഑ஔ͠·͠ΐ͏ root |-- src | `-- test |

    |-- groovy | | |-- pages | | |-- modules | | `- xxxSpecs.groovy | `-- resources | `-- GebConfig.groovy `-- build.gradle
  14. Θ͔Γ΍͍͢هड़Λ SpockͷػೳΛ׆༻ given: "user is at Top page" to LoginPage

    login("user", "password") at TopPage when: "user clicks list button" listButton.click() then: "user moves to List page" at ListPage
  15. ॻ͖ํόϥόϥ໰୊ // A͞Μ go "http://myapp.com/login" $("form.login").with { username = "admin"

    password = "password" login().click() } // B͞Μ to LoginPage username = "admin" password = "password" loginButton.click() // C͞Μ to LoginPage login("admin", password)