LoginSignup
1
0

More than 5 years have passed since last update.

EarlGreyでよく使っているメソッド

Last updated at Posted at 2018-04-25

EarlGreyよく使うメソッド

grey_allOf([matcher]) Matcher

複数の条件で要素を探したい時に用います

let matcher = grey_allOf([grey_kindOfClass(UIButton.self), 
                          grey_sufficientlyVisible(), 
                          grey_accessibilityID("hogeButton")])

EarlGrey.select(elementWithMatcher: matcher)
.perform(grey_tap())

注意:allOfメソッドは、配列の中にあるMatcherを順番に評価していきます。
評価する順番に気をつけないと処理が遅くなり、Timeoutエラーが起こるので気をつけましょう。
例えばこんな、テストがあるとします。

EarlGrey.selectElement(with: grey_allOf([grey_sufficientlyVisible()
                                         grey_kindOfClass(HogeCell.self),
                                         grey_selected()]))
         .inRoot(grey_kindOfClass(UITableView.self))
         .usingSearch(grey_scrollInDirection(.down, 100),
                      onElementWith: grey_kindOfClass(UITableView.self))
         .atIndex(0)
         .assert(grey_notNil())

この場合は、最初にgrey_sufficientlyVisible()が評価され、画面上に写っているもの全てが対象になり、HogeCellを探し、選択されているものを対象にしています。
画面上に写っているものを対象に入れると、無駄な処理が走るので、順番を変えましょう。

EarlGrey.selectElement(with: grey_allOf([grey_kindOfClass(HogeCell.self)
                                         grey_sufficientlyVisible(),
                                         grey_selected()]))
         .inRoot(grey_kindOfClass(UITableView.self))
         .usingSearch(grey_scrollInDirection(.down, 100),
                      onElementWith: grey_kindOfClass(UITableView.self))
         .atIndex(0)
         .assert(grey_notNil())

これだと、HogeCellで画面に写っているもので、選択されているものを対象にするので、動作が軽くなります

.inRoot(matcher) Method

特定したい要素のwrapperを指定します。

EarlGrey.select(elementWithMatcher: grey_accessibilityID("hogeButton"))
.inRoot(grey_kindOfClass(UICollectionViewCell.self))
.perform(grey_tap())

.atIndex(UInt) Method

複数要素がある場合、このメソッドを用いてN番目の要素を選択することができます。

EarlGrey.select(elementWithMatcher: grey_kindOfClass(UITableViewCell.self))
.inRoot(grey_kindOfClass(UITableView.self))
.assert(grey_notNil())

注意
UITableViewCellとUICollectionViewCellを取ろうとする時取得する順番が違うので注意してください。
UITableViewCell -> 一番最初に表示されているセル
UICollectionViewCell -> 一番最後に表示されているもの

grey_sufficientlyVisible() Matcher

要素が表示されている要素を探すときに使います。

let matcher = grey_allOf(grey_sufficientlyVisible(), 
                         grey_kindOfClass(UITableViewCell.self))

EarlGrey.select(elementWithMatcher: matcher)
.inRoot(grey_kindOfClass(UITableView.self))
.assert(grey_notNil())

注意
このMatcherは要素全体が表示されているかどうかを判定していますので、画面半分に写っているものはカウントしないのでご注意ください。

grey_not(Matcher) Matcher

引数にMatcherを入れると、逆の結果のものを探します。 
画面上にあるUIButtonで、isSelectedfalseのものを探す時に便利だったりします。


let matcher = grey_allOf([grey_kindOfClass(UIButton.self), 
                          grey_not(grey_selected())])

EarlGrey.select(elementWithMatcher: matcher)
.inRoot(grey_kindOfClass(UIView.self))
.perform(grey_tap())

GREYCondition

特定の値が変わるまで待ちたい時に使っています。


let ID: Uint? = nil

getRequest { (resultID) in
  ID = resultID
}

let success = GREYCondition(name: "wait to change id") { () -> Bool in

 return  ID != 0

}.wait(second: 5)


GREYAssertTrue(success, reason: "failed api")

※注意
GREYConditionはTimeoutするまで、クロージャの中身を繰り返し処理するのでクロージャの中身にAPIを呼び出すような処理はしない方が良さそうです。

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