LoginSignup
6
5

More than 5 years have passed since last update.

Gebでマルチプラットフォーム環境のテスト実装案

Last updated at Posted at 2015-06-16

※少し冗長だけど、あくまで一案ということで。

環境

  • Geb
  • Spock
  • Gradle

やりたい事

  • PCサイト(Chrome)スマホサイト(Chrome Emulator) の自動テストを作成
  • PageObjectはPCとスマホで独立して作成するが、テストケース(Spec)はPCとスマホで共通のコードを使い回したい

前提

スクリプト構成は下記の通り

src
 └ test
   └ groovy
     ├ testcase
     │ ・ExampleSpec.groovy
     └ page
       ├ pc
       │ ・LoginPage.groovy
       └ smartphone
         ・LoginPage.groovy
  • ExampleSpec.groovy はPCとスマホで共通
  • LoginPage.groovy はPCとスマホで別ファイル

実装

PageObjectクラス

パッケージで環境ごとに名前空間を分ける

page/pc/LoginPage.groovy
package page.pc
import geb.Page

class LoginPage extends Page {

    static at = { title.startsWith("ログイン") }
    static content = { }

    void login(String username, String password) { }
}
page/smartphone/LoginPage.groovy
package page.smartphone
import geb.Page

class LoginPage extends Page {

    static at = { title.startsWith("ログイン") }
    static content = { }

    void login(String username, String password) { }
}

Factoryクラス

  • 環境による分岐をFactoryクラスで吸収
    geb.envプロパティに環境の値が設定されていると過程
  • LoginPageFactory.get()で環境ごとのクラスが取得できる
page/factory/LoginPageFactory.groovy
package page.factory

class LoginPageFactory {

    public static def get() {
        switch(System.getProperty("geb.env")) {
            case "pc":
                return page.pc.LoginPage
                break
            case "smartphone":
                return page.smartphone.LoginPage
                break
        }
    }
}

TestCaseクラス

  • Pageクラス自体はインポートせずにFactoryクラスをインポート
  • Pageクラスを取得したい場合はFactoryクラスに定義されたget()メソッドを使う
testcase/ExampleSpec.groovy
package testcase

import framework.GebConfigReportingSpec
import page.factory.TopPageFactory

class ExampleSpec extends GebReportingSpec {

    def "ログイン後にトップページに遷移すること"() {
        when: "ログインする"
        LoginPageFactory.get().login(browser, user.username, user.password)

        then: "タイトルが正しいことを確認する"
        assert title == "マイページ - TOP"
    }
}

テストの実行方法

gradle clean test --tests testcase.ExampleSpec -Dgeb.env=pc
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5