More Related Content Similar to JJUG CCC 2011 Fall / Web test automation with Geb and Spock Similar to JJUG CCC 2011 Fall / Web test automation with Geb and Spock (20) More from Nobuhiro Sue (18) JJUG CCC 2011 Fall / Web test automation with Geb and Spock1. GebとSpockでWebテストを自動化せよ 2011/10/17 @JJUG CCC 2011 Fall JJUG幹事/ JGGUGサポートスタッフ 須江 信洋 http://twitter.com/nobusuehttp://d.hatena.ne.jp/nobusue ※資料の内容は個人としての意見・見解を述べたものであり、 所属する企業・組織が内容を保証するものではありません。 2. 自己紹介 須江 信洋(すえ のぶひろ) Twitter: @nobusue http://www.facebook.com/profile.php?id=732337788 かれこれ10年位、JavaEE関連の仕事をしてます G*(Groovy関連技術)との関わり Groovyコミュニティ(JGGUG)サポートスタッフ 「プログラミングGROOVY」執筆チーム 「Groovy イン・アクション」翻訳チーム Groovyで作ったBot飼ってます(@hatena_groovy) 2 4. テスト自動化できてますか? Vモデル 4 ここは未だに 人海戦術が 主流 ここはJUnitなどで わりかし自動化 できている http://ja.wikipedia.org/wiki/V%E3%83%A2%E3%83%87%E3%83%AB 8. Seleniumの系譜 8 2004~ Selenium1(Selenium RC) Selenium2 Selenium IDE 2009~ Selenium2 +WebDriver Selenium-Grid 2006~ 2011/07 Selenium2.0リリース WebDriver (Google) http://seleniumhq.org/docs/01_introducing_selenium.html#brief-history-of-the-selenium-project 10. WebDriver: Java APIサンプル 10 public class Selenium2Example { public static void main(String[] args) { WebDriver driver = new FirefoxDriver(); driver.get("http://www.google.com"); WebElement element = driver.findElement(By.name("q")); element.sendKeys("Cheese!"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); (new WebDriverWait(driver, 10)) .until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); System.out.println("Page title is: " + driver.getTitle()); driver.quit(); } } 12. jQuery-like Navigator API 12 // CSS 3 selectors $("div.some-class p:first[title='something']") // Find via index and/or attribute matching $("h1", 2, class: "heading") $("p", name: "description") $("ul.thingsli", 2) // 'text' is special attribute for the element text content $("h1", text: "All about Geb") // Use builtin matchers and regular expressions $("p", text: contains("Geb")) $("input", value: ~/{3,}-{3,}-{3,}/) // Chaining $("div").find(".b") $("div").filter(".c").parents() $("p.c").siblings() 13. Page Objectパターン 13 class LoginPage extends Page { static url = "http://myapp.com/login" static at = { heading.text() == "Please Login" } static content = { heading { $("h1") } loginForm { $("form.login") } loginButton(to: AdminPage) { loginForm.login() } } } ログイン画面 Browser.drive { to LoginPage assert at(LoginPage) loginForm.with { username = "admin" password = "password" } loginButton.click() assert at(AdminPage) } テスト class AdminPage extends Page { static at = { heading.text() == "Admin Section" } static content = { heading { $("h1") } } } 管理画面 14. Gebの例: はてなキーワード検索 14 @Grapes([ @Grab("org.codehaus.geb:geb-core:latest.release"), @Grab("org.seleniumhq.selenium:selenium-firefox-driver:latest.release") ]) import geb.Browser Browser.drive { go "http://d.hatena.ne.jp/keyword/" assert title == "はてなキーワード - 話題の言葉がわかる、みんなで編集するキーワード" $("form.header-search").word = "Groovy" $("form.header-search").find("input", name:"submit").click() assert title == "はてな検索: Groovy" } 16. Power Assert def a = 1 def b = 2 def c = 3 assert (a+b)*c == 5 途中結果や、どこでfailしたかまで教えてくれる。assertEquals()とかを組み合わせる必要ナシ。 16 17. 可読性の高いテストケース 17 def "subscribers receive published events at least once"() { when: publisher.send(event) then: (1.._) * subscriber.receive(event) where: event << ["started", "paused", "stopped"] } def "length of Spock's and his friends' names"() { expect: name.size() == length where: name | length "Spock" | 5 "Kirk" | 4 "Scotty" | 6 } 19. GebReportingSpecの例 19 @Grab("org.codehaus.geb:geb-spock:0.6.0") @Grab("org.spockframework:spock-core:0.5-groovy-1.8") @GrabExclude("org.codehaus.groovy:groovy-all") @Grab("org.seleniumhq.selenium:selenium-firefox-driver:latest.release") import geb.spock.GebReportingSpec class FunctionalReportingSpec extends GebReportingSpec { def "Hatena Keyword Search Top"() { when: go "http://d.hatena.ne.jp/keyword/" then: title == "はてなキーワード - 話題の言葉がわかる、みんなで編集するキーワード" } def "Hatena Keyword Search Result"() { when: $("form.header-search").word = "Groovy" $("form.header-search").find("input", name:"submit").click() then: assert title == "はてな検索: Groovy" } }