0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Gebでページ操作を定義する

Posted at

Gebそのものについてはいろいろ知っている前提。

Pageオブジェクトのcontentを利用すると、ページ内のinput要素とか、エラーメッセージ表示のためのspanへのインタフェースを自分で定義することができる。

class ExamplePage extends Page {
  static content = {
    userId { $("form").find("input", name: "userId") }
    userIdErr { $("span#userIdErr").text() }
  }
}

これのおかげで、仮にinput要素のname属性が変わったり、idやclassの指定が変わっても、基本的には対応するcontentの定義を修正するだけでよくなる。
テストコードへの影響を抑えることができるし、"Page内のcontent"というマッピングが直感的で非常にわかりやすい。

で、ここからは動かしてみてそうだろうと当たりをつけた、という内容なので、正確でない情報もあるかもしれないという前置きをつけた上でのお話。

このcontentは、単にページ内の要素を定義するだけの代物ではなく、ページ内での操作も定義できる。例えば、"検索画面でxxxというクエリで検索する"といった操作を、

class SearchPage extends Page {
  static content = {
    searchUser { key -> 
      $("#searchForm").find("input", name: "keyword").value(key)
      $("#searchButton").click()
    }
  }
}

という感じで書くことができる。contentはブロック呼び出しなので、一連の処理を書いても問題ない。ただ、ブロック呼び出しの結果がnullだとエラーになるっぽいので、最後の行の結果がnullの時は注意が必要。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?