LoginSignup
8
8

More than 5 years have passed since last update.

xcodeでUI Testingを初めて触る時のメモ

Last updated at Posted at 2017-08-27

UI testを始めた時思ってた作業と違っていて戸惑ったのでメモ。

意外だったのはレコードボタンを押すとエクセルのマクロみたいな感じでコードが吐き出されるです。

新規作成 3ステップ

この記事がわかりやすい。
http://dev.classmethod.jp/smartphone/iphone/xcode-7-ui-testing/

1.新規作成する

スクリーンショット 2017-08-27 14.51.29.png

import XCTest

class postImage: XCTestCase {

    //テストコード起動時に呼び出される
    override func setUp() {
        super.setUp()
        continueAfterFailure = false
        XCUIApplication().launch()
    }
    //テストコード終了時に呼び出される
    override func tearDown() {
        super.tearDown()
    }
    //テストコード自体
    func testExample() {
        ここでレコードボタンを押す!! でシミュレータが動き出すので動かすと ここにコードが吐き出される
    }

}

2.レコードする。

赤い丸を押すとレコード開始。
スクリーンショット 2017-08-27 14.51.58.png

3.検証コードを書く。

aaaという文字が登録されたことを検証。

//コントロールのIdentifierに値を設定しておく必要あり
let staticText = app.staticTexts["HogeIdentifier"]
//検証コード
XCTAssertEqual(staticText.label, "aaa")

ザーッテストが流れるので、他の機能壊れてないかなーってリグレッションテストにもなる。
スクリーンショット 2017-08-27 15.03.32.png

注意点!!

エラーが出て苦戦。下記二個を設定することで回避。

1.accessibilityIdentifierを設定するべし

2.view.isAccessibilityElement = trueにするべし

Timestamped Event Matching Error: Failed to find matching element

スクリーンショット 2017-08-27 16.53.49.png

色々なXCTAssert

XCTAssertEqual意外にも検証用のコードは色々用意されてる。

要素が存在するかどうかのテスト

XCTAssert(app.staticTexts["Welcome"].exists)

要素が表示されるのを待つ

NSPredicateは条件。waitForExpectationsで待つ。

let goLabel = app.staticTexts["Go!"]
XCTAssertFalse(goLabel.exists)

let exists = NSPredicate(format: "exists == true")
expectation(for: exists, evaluatedWithObject: goLabel, handler: nil)

app.buttons["Ready, set..."].tap()
waitForExpectations(timeout: 5, handler: nil)
XCTAssert(goLabel.exists)

要素内の値を検証する

XCTAssertTrueが0であることを検証。

XCTAssertTrue(staticText.label == "0")

テーブルセルの並べ替え

let topButton = app.buttons["Reorder Top Cell"]
let bottomButton = app.buttons["Reorder Bottom Cell"]
bottomButton.press(forDuration: 0.5, thenDragTo: topButton)

XCTAssertLessThanOrEqual(bottomButton.frame.maxY, topButton.frame.minY)

pullしてリフレッシュ

テーブルの最初のセルと6のdyを持つ別のセルからXCUICoordinateを作成します。次に、最初の座標を2番目の座標にドラッグします。

let firstCell = app.staticTexts["Adrienne"]
let start = firstCell.coordinate(withNormalizedOffset: (CGVectorMake(0, 0))
let finish = firstCell.coordinate(withNormalizedOffset: (CGVectorMake(0, 6))
start.press(forDuration: 0, thenDragTo: finish)

ビューコントローラのプッシュとポップ

app.buttons["More Info"].tap()
XCTAssert(app.navigationBars["Volleyball?"].exists)

参照

検証コードを使ってみる
http://qiita.com/yhonda/items/bf759d4694b63b250948
XCTestのUI
http://qiita.com/IsaoTakahashi/items/f9c3ef367d7e0f60e2d6
http://qiita.com/taji-taji/items/c00e5b94376c37f17443
http://amarron.blog/detail.php?id=20170701
http://qiita.com/s-harada/items/5a8c12b0c456d155ba53
http://qiita.com/okuderap/items/b6211cc3524ad2385150#_reference-bb433e1d0cbf1d664ec6
アプリを削除
...やりたかったけどハマってる。
http://qiita.com/bbq-all-stars/items/be63ecddea15ec7453cc
公式
https://developer.apple.com/library/content/documentation/DeveloperTools/Conceptual/testing_with_xcode/chapters/04-writing_tests.html
チートシート
https://github.com/joemasilotti/UI-Testing-Cheat-Sheet#typing-text

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