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

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

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

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

PoohSunny

May 21, 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ϢʔβʔίϛϡχςΟ" ] }
  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. How

  4. νϡʔτϦΞϧ͋Δͷʁ 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
  5. @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 } }
  6. ૸ΒͤͯΈΑ͏ ./gradlew chromeTest # chromeͰ࣮ߦ ./gradlew firefoxTest # firefoxͰ࣮ߦ ./gradlew

    phantomJsTest # phantomjsͰ࣮ߦ ./gradlew test # ্ه3ͭͷϒϥ΢βͰͦΕͧΕ࣮ߦ
  7. σΟϨΫτϦߏ੒ root |-- src | `-- test | |-- groovy

    | | `- xxxSpecs.groovy | `-- resources | `-- GebConfig.groovy `-- build.gradle
  8. 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
  9. GebͷϖʔδΦϒδΣΫτ PageΫϥεΛܧঝ class GebishOrgHomePage extends Page { static at =

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

    = { toggle { children("span") } links { $('.link-list li a') } } }
  11. 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
  12. 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
  13. Page, Module͸ઐ༻ͷσΟϨΫτϦ ʹ഑ஔ͠·͠ΐ͏ root |-- src | `-- test |

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

    ࣍ͷϖʔδͷatνΣοΫ͕trueʹͳΔ·Ͱ଴ͭ button(to: ConfirmPage, toWait: true) { $('button#save') } }
  15. GebConfig Α͘࢖͍ͦ͏ͳઃఆ14 baseUrl = "http://gebish.org" environments { // systemProperty "geb.env",

    "chrome" chrome { driver = { new ChromeDriver() } } } 14 http://www.gebish.org/manual/current/#configuration
  16. 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
  17. GebConfig ͜Μͳࢦఆ΋ΞϦ waiting { presets { slow { timeout =

    20 retryInterval = 1 } quick { timeout = 1 } } } // ݺͼํ waitFor("quick")
  18. 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 ͔ΒҰ෦ൈਮ
  19. লུͤͣʹॻ͘͜ͱ΋Մೳ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
  20. Θ͔Γ΍͍͢هड़Λ 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
  21. ॻ͖ํόϥόϥ໰୊ // 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)
  22. ରࡦ εΫϦϓτͷվળ ςΩετϚονϯά͸࠷ऴखஈ19 $('div', text: 'text matcher') ՄೳͳݶΓCSS selectorsΛར༻͢Δ(id, class,

    attributes) 19 http://markmail.org/message/3fl2bqjmnzsanjyu#query:+page:1+mid: 2oh4r2ycbdluq4l7+state:results
  23. ରࡦ ฒྻ࣮ߦ20 // GebConfig cacheDriverPerThread = true quitCachedDriverOnShutdown = false

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