UIテストや一般的なUIテストからアプリケーションの状態にアクセスする方法を尋ねている可能性があります。
私はそれはので、私はそれが私がについて多くを知らないと、うまくいけばチャイムする他の人々を促し、正しいだろう何かだから答えるつもりはかなり興味深い質問だと思います。
背景:私は2つのビューを含むビューコントローラの基本的なプロジェクトを設定する基本的なマジック8ボールプロジェクト
:ラベルとボタン。ボタンをタップすると、ランダムなメッセージとラベルのテキストを更新:、
import UIKit
struct EightBall {
static let messages = ["Yes", "No", "It's not certain"]
var newMessage: String {
let randomIndex = Int(arc4random_uniform(UInt32(EightBall.messages.count)))
return EightBall.messages[randomIndex]
}
}
class ViewController: UIViewController {
let ball = EightBall()
@IBOutlet weak var messageLabel: UILabel!
@IBAction func shakeBall(_ sender: Any) {
messageLabel.text = ball.newMessage
}
}
基本的なUIテスト
はここでボタンをタップ自動化する方法を示すコメントUIテストだし、ラベルの値をつかみ、ラベルの値が有効なメッセージであるかどうかをチェックします。
import XCTest
class MagicUITests: XCTestCase {
// This method is called before the invocation of each test method in the class.
override func setUp() {
super.setUp()
// In UI tests it is usually best to stop immediately when a failure occurs.
continueAfterFailure = true
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
XCUIApplication().launch()
}
func testValidMessage() {
// Grab reference to the application
let app = XCUIApplication()
// #1
// Grab reference to the label with the accesability identifier 'MessageLabel'
let messagelabelStaticText = app.staticTexts["MessageLabel"]
// Tap the button with the text 'Shake'
app.buttons["Shake"].tap()
// get the text of the label
let messageLabelText = messagelabelStaticText.label
// #2
// check if the text in the label matches one of the allowed messages
let isValidMessage = EightBall.messages.contains(messageLabelText)
// test will fail if the message is not valid
XCTAssert(isValidMessage)
}
}
#1で、私はラベルを取得するために使用しているアプローチは、ラベルにaccessibilityIdentifier
プロパティにアクセスすることです。このプロジェクトではストーリーボードを通じて入力しましたが、コードでビューを設定する場合は、accessibilityIdentifier
プロパティを直接設定することができます。
ここで紛らわしいもう一つの点は、ビューの階層をナビゲートしていないビューの要素にアクセスするには階層のプロキシですが、ラベルを取得する構文が奇妙な 'staticTexts '(ポストの下にある参照はこれをより詳細に説明している)。 #2のために

私は私のプロジェクトで定義された構造を検査しています。ユニットテストでは、私は@testable import ProjectName
をインポートし、これをアクセスすることができましたが、残念ながら、このアプローチは、UIテストのために動作しません。
代わりに、あなたはUIテストからアクセスしたい任意のソースファイルを対象として含まれていることを確認する必要があります。

もっとUIテスト参照:
[要素が配列内にあるかどうかを確認する方法](http://stackoverflow.com/questions/24102024/how)配列に要素があるかどうかを確認する) –
配列のcontainsメソッドでtrueをアサートする – DatForis
メタ・アドバイスを答えから移動:ようこそスタック・オーバーフロー!すでに複数回質問されている「値が配列に含まれているかどうかをチェックするにはどうすればよいか」という質問が表示されているので、いくつかの下降声があります。あなたが尋ねる次の質問は、間違って解釈されていないことを確認するために[良い質問をする方法](https://stackoverflow.com/help/how-to-ask)を素早く見てください。 – halfer