liguofeng29’s blog

個人勉強用ブログだっす。

groovy,geb(selenium),spockによる自動化テスト その8 - Page Object Pattern - Pageの属性

Pageオブジェクトの属性

url属性 : PageのURLを表す

・シナリオ

  1. 相対パス指定のpageに移動 (to ExamplePage1)
  2. 絶対パス指定のpageに移動 (to ExamplePage2)
  3. 引数指定のpageに移動 (to ExamplePageWithArguments)
  4. インスタンス引数指定のpageに移動 (to ExamplePageWithInstance, new Person(id : “12345”))
/**
 * Page Object
 *
 * URL
 * urlは baseUrl + パスで構成
 * Page Objectのurlは、相対、絶対両方OK
  */
import geb.Browser
import geb.Page

Browser.drive {
    // baseUrl
    config.baseUrl = "http://www.gebish.org/"

    to ExamplePage1

    sleep 3 * 1000 // for debug

    to ExamplePage2

    sleep 3 * 1000 // for debug

    to ExamplePageWithArguments, "args1", "args2"
    // will go to「http://www.gebish.org/manual/args1/args2」

    sleep 3 * 1000 // for debug

    to ExamplePageWithInstance, new Person(id : "12345")
    // will go to [http://www.gebish.org/manual/12345]
    sleep 3 * 1000 // for debug
}.quit()

/** 相対url **/
class ExamplePage1 extends Page {
    static url = "manual/current/all.html"
}

/** 絶対url **/
class ExamplePage2 extends Page {
    static url = "http://www.gebish.org/manual/current/#at-checking"
}

/** 引数ありurl **/
class ExamplePageWithArguments extends Page {
    static url = "http://www.gebish.org/manual"
}

/** インスタンスを引数とするurl **/
class ExamplePageWithInstance extends Page{
    static url = "http://www.gebish.org/manual/"

    String convertToPath(Person person) {
        person.id.toString()
    }
}

class Person {
    String id
}

f:id:liguofeng29:20170302221619g:plain

at属性 : 呼ばれたpageが予期したpageであるのかを検証する

・シナリオ

  1. Yahooに移動する (to YahooTopWithRightAt)
  2. 検索ボタンをクリックする
  3. Yahooページに移動する (YahooTopWithWrongAt )
  4. at処理でgeb.waiting.WaitTimeoutExceptionが発生する
/**
 * Page Object
 *
 * at
 * 呼ばれたpageが予期したpageであるのかを検証する
 */
import geb.Browser
import geb.Page

def keywords = 'javait.hatenablog.com'

Browser.drive {

    to YahooTopWithRightAt
    println 'ログイン画面 : ' + title
    検索(keywords)
    println 'ホーム画面 : ' + title


    // geb.waiting.WaitTimeoutException発生
    to YahooTopWithWrongAt
    println 'to YahooTopWithWrongAtのat条件を満たさないので、処理はここまでがこないはず'

    sleep 10 * 1000 // for debug
}.quit()


class YahooTopWithRightAt extends Page {

    static url = 'http://www.yahoo.co.jp/'

    static at = {
        title.endsWith("Yahoo! JAPAN")
    }

    static content = {
        検索欄 { $('#srchtxt') }
        検索ボタン { $('#srchbtn') }
    }

    // pageが提供するサービス
    void 検索 (String keywords) {
        検索欄 = keywords
        検索ボタン.click()
    }
}

class YahooTopWithWrongAt extends Page {

    static url = 'http://www.yahoo.co.jp/'

    static at = {
        // GebConfig.groovyに設定したtimeoutでエラー発生
        title.endsWith("xxxxxxxxxxxxxxx wrong")
    }

    static content = {
        検索欄 { $('#srchtxt') }
        検索ボタン { $('#srchbtn') }
    }

    // pageが提供するサービス
    void 検索 (String keywords) {
        検索欄 = keywords
        検索ボタン.click()
    }
}

Content DSL : pageの要素を定義する

/**
 * Page Object
 *
 * Content DSL
 * 引数:
 * required : default true
 *            要素が存在しない場合、RequiredPageContentNotPresent例外を投げるかどうか
 *
 * cache :  defalut false
 *          ページをリロードしないと値はそのままだ。
 *
 * to    :  指定pageのURLを開く
 * wait  :  defalut false 値、true, a string, a number, a 2 element list of numbers
 * toWait : default false 、toと組み合わせで使う
 *          主に非同期遷移とかの場合、ページ遷移を待つのか?未検証。
 *
 * page  :  defalut null
 */
import geb.Browser
import geb.Page

def testPage = new File('src/main/java/html/script24.html')

Browser.drive {
    to YahooTopTestRequired

    /** required 検証 **/
    /** http://www.gebish.org/manual/current/#code-required-code **/
    try {
        println 存在しない要素1
    } catch (MissingPropertyException e) {
        println "required: true の場合は、MissingPropertyException発生 : " + 存在しない要素2
    }

    println "required: false の場合は、null : " + 存在しない要素2

    /** cache 検証 **/
    /** http://www.gebish.org/manual/current/#code-cache-code **/

    to YahooTopTestCache

    assert キャッシュ利用する == "value1"
    assert キャッシュ利用しない == "value1"

    value1 = "value99"

    assert キャッシュ利用する == "value1"        // 値が変わらない
    // assert キャッシュ利用しない == "value99"   // 変わるはずだが・・・・ 公式ページのコードでもやったが。

    /** page 検証 **/
    // 通常IFrame操作で利用する。

    /** to 検証 **/
    /* The to option allows the definition of which page the browser will be sent to if the content is clicked. */
    /** http://www.gebish.org/manual/current/#content-dsl-to **/
    config.baseUrl = new File(testPage.getAbsolutePath()).toURI()
    to PageTestTo
    リンク.click()

    /** NOTE
   *
   * toを使う際に、指定pageにはatを実装すべきである
   * toを指定することで実際に遷移が行われるわけではなく、指定pageのatが実行される。
   *
   * テキスト.click()をクリックしてもページ遷移は発生しない。
   */

     sleep 10 * 1000 // for debug
}.quit()

class YahooTopTestRequired extends Page {

    static url = 'http://www.yahoo.co.jp/'

    static at = { title.endsWith("Yahoo! JAPAN") }

    static content = {
        検索欄 (required: true) { $('#srchtxt') }
        検索ボタン (required: true) { $('#srchbtn') }

        存在しない要素1 (required: true) { $('#abcde1').text() }
        存在しない要素2 (required: false) { $('#abcde2').text() }
    }
}

class YahooTopTestCache extends Page {

    def value1 = "value1"

    static url = 'http://www.yahoo.co.jp/'

    static at = { title.endsWith("Yahoo! JAPAN") }

    static content = {
        検索欄 (required: true) { $('#srchtxt') }
        検索ボタン (required: true) { $('#srchbtn') }

        キャッシュ利用する   (cache: true) { value1 }
        // キャッシュ利用しない (cache: false) { value1 }
        キャッシュ利用しない  { value1 }
    }
}

class PageTestTo extends Page {
    static url = "script24.html"
    static content = {
        テキスト(to : GoogleTopTestTo) {$('#div1')}
        リンク (to : GoogleTopTestTo) {$('#googleLink')}
    }
}

class GoogleTopTestTo extends Page {
    static url = 'http://www.google.co.jp/'
    static at = { waitFor { title.startsWith("Google")} }
}