LoginSignup
3
0

More than 5 years have passed since last update.

EarlGreyのコード解説

Posted at

メソッドについて

EarlGreyには要素に対して働きかける三種類のメソッドがあります。

action

要素に何かしらの動作を施します。

assert

要素が思った通りになっているかの確認に用います

matcher

操作したい要素を探すときに使います

以上の三つのメソッドを使うことによって、簡単にUITestコードを実装することができます。

では、実際に書いて見ましょう。

アプリの動作テスト

例えば、ボタンを押すとラベルが表示される簡単なコードを書いたとします。

ViewController.swift
import UIKit

class ViewController: UIViewController {

  @IBOutlet weak var resultLabel: UILabel!
  @IBOutlet weak var button: UIButton!

  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    resultLabel.isHidden = true
    resultLabel.accessibilityIdentifier = "resultLabel"

    button.isSelected = false
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }
  @IBAction func didPushButtoon(_ sender: Any) {

    resultLabel.isHidden = false
    button.isSelected = true
  }
}

では次に、ボタンを押してラベルが表示されているかのテストコードを書きます。

TestViewController.swift
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.

      EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIButton.self)).perform(grey_tap())

      EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIButton.self)).assert(grey_selected())

      EarlGrey.select(elementWithMatcher: grey_accessibilityID("resultLabel")).assert(grey_sufficientlyVisible())
    }
}

一体何をやっているのか

主にtestExamplesでテストを行っています。
その内容を解説していきます


解説1

EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIButton.self)).perform(grey_tap())

このコード処理内容は画面上にあるUIButtonを選択し、タップさせる処理です。
では各メソッドについて細かく補足します。

EarlGrey.select(elementWithMatcher: Matcher)は操作したい要素を選択するときに使います。

そのselectの引数に入っているgrey_kindOfClass()は引数に入っているクラスを元に対象を割り出す際に使います。

selectの中にはmatcherが入り、.perform("Action")は要素にタップ操作やダブルタップ等の操作処理をしたいときに使うものだと覚えておけば良いと思います。


解説2

EarlGrey.select(elementWithMatcher: grey_kindOfClass(UIButton.self)).assert(grey_selected())

これは、画面上にあるボタンのisSelectedプロパティがtrueになっているか確認しています。

.assert(Matcher or Asserts)は要素に対して何か確認したいときに使ったりします。


解説3

EarlGrey.select(elementWithMatcher: grey_accessibilityID("resultLabel")).assert(grey_sufficientlyVisible())

このコードは、accessibilityIDが設定されているラベルを選択し、ちゃんと表示されているかをgrey_sufficientlyVisible()で確認しています。

画面上にUILabelの要素が複数ある場合にmultipleエラー(Matcherの該当要素が複数ある場合のエラー)を吐かれますので、個別に要素を選択出来るようにUILabelにaccessibilityIdentifierを割り当てています。

resultLabel.accessibilityIdentifier = "好きな名前"

以上で超簡単なテストコードが書けると思います。

もし間違いなどがあれば、ご指摘いただけたら幸いです。

おまけ

Matcherのgrey_sufficientlyVisible()について

画面内に収まっていて、表示されているかを確認するassertメソッドです。
ただ、画面内に収まりきらない要素はたとえ表示されていても、「確認ができない」とUITest実行時にエラーを吐かれますので、使う際には十分に注意してください。

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