XCUITestでUITestを作成する場合、UI要素の検索でXCUIElementTypeQueryProviderの定義済みクエリを
使うこともあると思います。
定義済みクエリを使用する時、クエリと実際のUI要素との対応が一部曖昧な部分があったので、
よく使いそうなUI要素との対応を備忘録的に記録しておこうと思います。
UILabel
func test_UILabelを取得() {
let label = app.staticTexts["UILabel"] // accessibility identifierを指定
XCTAssertTrue(label.exists) // 指定したaccessibility identifierのUI要素が存在するか確認
}
UIImageView
func test_UIImageViewを取得() {
let imageView = app.images["UIImageView"]
XCTAssertTrue(imageView.exists)
}
UITextField(secureTextEntryではない)
func test_UITextFieldを取得() {
let textField = app.textFields["UITextField"]
XCTAssertTrue(textField.exists)
}
UITextField(secureTextEntryである)
func test_secureTextEntryのUITextFieldを取得() {
let secureTextField = app.secureTextFields["UITextField_Secure"]
XCTAssertTrue(secureTextField.exists)
}
UIButton
func test_UIButtonを取得() {
let button = app.buttons["UIButton"]
XCTAssertTrue(button.exists)
}
UITextView
func test_UITextViewを取得() {
let textView = app.textViews["UITextView"]
XCTAssertTrue(textView.exists)
}
UISegmentedControl
func test_UISegmentedControlを取得() {
let segmentedControl = app.segmentedControls["SegmentedControl"]
XCTAssertTrue(segmentedControl.exists)
}
UISlider
func test_UISliderを取得() {
let slider = app.sliders["UISlider"]
XCTAssertTrue(slider.exists)
}
UISwitch
func test_UISwitchを取得() {
let uiSwitch = app.switches["UISwitch"]
XCTAssertTrue(uiSwitch.exists)
}
UIView
func test_UIViewを取得() {
let view = app.otherElements["UIView"]
XCTAssertTrue(view.exists)
}
終わりに
上記のUI以外のクエリは以下のリンクをご参照ください。
定義済みクエリを全て確認できます。
クエリはiOS, macOSの区別なく定義されているので一部iOSでは使えないものもあります。(touchBarなど)
https://developer.apple.com/documentation/xctest/xcuielementtypequeryprovider