LoginSignup
0
0

More than 5 years have passed since last update.

EarlGreyのMatcherを自作してみよう!

Posted at

普段テストを行っていると、UITableViewUICollectionViewなどで特定条件のセルを探したい時などがあるかと思います。

そんな時は、matcherを自作しましょう。
ActionやAssertの作り方とは少し違いますので、気をつけましょう。

以下が例です。

自作Matcher
  private func grey_customCellMatcher() -> GREYMatcher {
    return GREYElementMatcherBlock.matcher(matchesBlock: { (ele) -> Bool in
      guard let cell = ele as? HogeCell else { return }

      ///return値がtrueの要素を探しています
      return cell.isHoge
    }, descriptionBlock: { (description) in
      _ = description?.appendText("grey customCellMatcher")
    })
  }

trueだったものを返すので、複数要素がある場合multipleエラーになってしまいます。そんな時はatIndex()などを使いましょう。

全体コード

import XCTest
import EarlGrey

@testable import QiitaPresents

class QiitaPresentsTests: XCTestCase {

    override func setUp() {
        super.setUp()
        // Put setup code here. This method is called before the invocation of each test method in the class.

      let success = GREYCondition(name: "setup") { () -> Bool in
        var error: NSError? = nil

        EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIViewController.self))
          .atIndex(0)
          .assert(grey_sufficientlyVisible(), error: &error)

        return error != nil
      }.wait(withTimeout: 15)
      GREYAssertTrue(success, reason: "failed setup")
    }

    override func tearDown() {
        // Put teardown code here. This method is called after the invocation of each test method in the class.
        super.tearDown()
    }

    func testExample() {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct results.

      var targetCell: HogeCell!

      let matcher = grey_allOf([grey_sufficientlyVisible(),
                                grey_kindOfClass(HogeCell.self),
                                self.grey_customCellMatcher()])

      EarlGrey.select(elementWithMatcher: matcher)
        .inRoot(grey_kindOfClass(UITableView.self))
        .atIndex(0)
        .perform(grey_tap())

    }

  private func grey_customCellMatcher() -> GREYMatcher {
    return GREYElementMatcherBlock.matcher(matchesBlock: { (ele) -> Bool in
      guard let cell = ele as? HogeCell else { return }

      return cell.isHoge
    }, descriptionBlock: { (description) in
      _ = description?.appendText("grey customCellMatcher")
    })
  }
}

以上です。

お願い

自作MatcherのdescriptionBlock:の部分が何をしているか把握していません。
よろしければ誰か教えていただければ幸いです。

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