ブラウザテストをサクサク自動化するためのGeb実践入門 #jjug_ccc

ブラウザテストをサクサク自動化するためのGeb実践入門 #jjug_ccc

JJUG CCC Spring 2016 での発表資料です。

B765faf519f19520e1495bd870f4d7f0?s=128

PoohSunny

May 21, 2016
Tweet

Transcript

  1. 4.

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

    "TDDBC", "Agile Samurai Base Camp", "೔ຊSeleniumϢʔβʔίϛϡχςΟ" ] }
  2. 5.
  3. 8.
  4. 12.
  5. 13.
  6. 14.

    ؆ܿͳهड़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/
  7. 22.
  8. 27.
  9. 28.
  10. 31.

    How

  11. 32.
  12. 36.
  13. 39.

    νϡʔτϦΞϧ͋Δͷʁ I don't know about any tutorials but the documentation

    is quite comprehensive. It also lists a couple of example projects hosted on github which you might want to have a look at. -- ஌Βͳ͍͚ͲυΩϡϝϯτΈͨΒ͍͍Αɻ͋ͱ githubʹ͍͔ͭ͘αϯϓϧ͕͋ΔΑ5 5 https://groups.google.com/d/msg/geb-user/tgnM4gVMRFs/j3Bdo3D6NgAJ
  14. 41.

    @RunWith(JUnit4) class GebishOrgTest extends GebReportingTest { @Test void canGetToTheCurrentBookOfGeb() {

    to GebishOrgHomePage //hover over to expand the menu interact { moveToElement(manualsMenu.toggle) } //first link is for the current manual assert manualsMenu.links[0].text().endsWith("- CURRENT") manualsMenu.links[0].click() at TheBookOfGebPage } }
  15. 42.

    ૸ΒͤͯΈΑ͏ ./gradlew chromeTest # chromeͰ࣮ߦ ./gradlew firefoxTest # firefoxͰ࣮ߦ ./gradlew

    phantomJsTest # phantomjsͰ࣮ߦ ./gradlew test # ্ه3ͭͷϒϥ΢βͰͦΕͧΕ࣮ߦ
  16. 43.

    σΟϨΫτϦߏ੒ root |-- src | `-- test | |-- groovy

    | | `- xxxSpecs.groovy | `-- resources | `-- GebConfig.groovy `-- build.gradle
  17. 44.

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

    testCompile "org.seleniumhq.selenium:selenium-firefox-driver:$seleniumVersion" testCompile("com.codeborne:phantomjsdriver:1.2.1") { // phantomjs driver pulls in a different selenium version transitive = false } } 6 ͳͥɺWebDriverΛͦΕͧΕґଘؔ܎ʹ௥Ճ͢Δͷ͔ʹ͍ͭͯ͸ɺhttps://groups.google.com/d/ msg/geb-user/Og40o5mXK-4/tga5H2zuRRYJ
  18. 51.

    GebͷϖʔδΦϒδΣΫτ PageΫϥεΛܧঝ class GebishOrgHomePage extends Page { static at =

    { title == "Sample page" } static content = { header { $("#header") } manualsMenu { module MenuModule, $("#header", 0) } } }
  19. 52.

    ࠶ར༻Մೳͳύʔπ͸Moduleʹ import geb.Module class MenuModule extends Module { static content

    = { toggle { children("span") } links { $('.link-list li a') } } }
  20. 54.

    url8 to()ϝιουͰݺΜͩ࣌ʹભҠ͢ΔઌΛઃఆ class PageWithUrl extends Page { static url =

    "example" } ૬ରύεͰهड़͢Δͱ͖͸ɺbaseUrlͷઃఆ͕ඞཁ9 9 http://www.gebish.org/manual/current/#base-url 8 http://www.gebish.org/manual/current/#page-urls
  21. 57.

    jQueryͬΆ͍API11 $("div") // ࠷ॳʹݟ͔ͭͬͨ div ཁૉ $("div", name: "main") //

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

    Page, Module͸ઐ༻ͷσΟϨΫτϦ ʹ഑ஔ͠·͠ΐ͏ root |-- src | `-- test |

    |-- groovy | | |-- pages | | |-- modules | | `- xxxSpecs.groovy | `-- resources | `-- GebConfig.groovy `-- build.gradle
  23. 61.

    click manualsMenu.links[0].click() ϖʔδΦϒδΣΫτͷఆٛͷ࢓ํ͍Ζ͍Ζ content { // to: ͜ͷΤϨϝϯτ͕ΫϦοΫ͞Εͨͱ͖ʹConfirmPageΛ౉͢ // toWait:

    ࣍ͷϖʔδͷatνΣοΫ͕trueʹͳΔ·Ͱ଴ͭ button(to: ConfirmPage, toWait: true) { $('button#save') } }
  24. 62.

    GebConfig Α͘࢖͍ͦ͏ͳઃఆ14 baseUrl = "http://gebish.org" environments { // systemProperty "geb.env",

    "chrome" chrome { driver = { new ChromeDriver() } } } 14 http://www.gebish.org/manual/current/#configuration
  25. 64.

    GebConfig wait·ΘΓ15 waiting { includeCauseInMessage = true timeout = 10

    retryInterval = 0.5 } atCheckWaiting = true // at ͷ࣌ʹwait͕૸Δ // ݺͼํ waitFor {} waitFor(20) // 20ඵͰtimeout 15 http://www.gebish.org/manual/current/#waiting-configuration
  26. 65.

    GebConfig ͜Μͳࢦఆ΋ΞϦ waiting { presets { slow { timeout =

    20 retryInterval = 1 } quick { timeout = 1 } } } // ݺͼํ waitFor("quick")
  27. 66.
  28. 67.
  29. 72.

    methodMissing browserΦϒδΣΫτʹॲཧΛҠৡ16 class GebSpec extends Specification { Browser _browser Browser

    getBrowser() { if (_browser == null) { _browser = createBrowser() } _browser } def methodMissing(String name, args) { getBrowser()."$name"(*args) } } 16 https://github.com/geb/geb/blob/master/module/geb-spock/src/main/groovy/geb/spock/ GebSpec.groovy ͔ΒҰ෦ൈਮ
  30. 74.

    লུͤͣʹॻ͘͜ͱ΋Մೳ17 HomePage homePage = browser.to HomePage homePage.loginPageLink.click() LoginPage loginPage =

    browser.at LoginPage SecurePage securePage = loginPage.login("user1", "password1") ͜͏ॻ͘ͱIDEͷิ׬͕ޮ͖·͢ ิ׬ΛऔΔ͔ɺ؆ܿ͞ΛऔΔ͔͸Α͘ߟ͑ͯʂ 17 http://www.gebish.org/manual/current/#ide-support
  31. 76.
  32. 87.

    Θ͔Γ΍͍͢هड़Λ 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
  33. 89.

    ॻ͖ํόϥόϥ໰୊ // 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)
  34. 96.

    ରࡦ εΫϦϓτͷվળ ςΩετϚονϯά͸࠷ऴखஈ19 $('div', text: 'text matcher') ՄೳͳݶΓCSS selectorsΛར༻͢Δ(id, class,

    attributes) 19 http://markmail.org/message/3fl2bqjmnzsanjyu#query:+page:1+mid: 2oh4r2ycbdluq4l7+state:results
  35. 97.

    ରࡦ ฒྻ࣮ߦ20 // GebConfig cacheDriverPerThread = true quitCachedDriverOnShutdown = false

    // build.gradle test { maxParallelForks = 4 } 20 http://www.gebish.org/manual/current/#driver-caching-configuration
  36. 98.
  37. 104.