要素の特定を詳しく
子要素の特定をする
宣言 | 用途 |
---|---|
XCUIElementQuery.children(matching:) | 子要素(直下の要素)を探す |
XCUIElementQuery.descendants(matching:) | 子孫(孫以降も)を探す |
XCUITestでは一度に複数のUI要素に対して操作を行えない。そのため、要素を1つに絞り込む必要がある。
検索結果で要素が確実に1つの時は element
とすることで XCUIElement
としてレスポンスされる。ただし、複数要素だった場合はエラーになるので注意。
let textField = app.scrollViews["scrollview"].children(matching: .staticText).element
複数特定されたUI要素に対し一つずつ順番に処理を行う
宣言 | 用途 |
---|---|
allElementsBoundByIndex: [XCUIElement] { get } | 各UI要素にアクセスできる |
■ 使用例
import XCTest import UIKit class SampleTestCases: XCTestCase { func testExample() { let app = XCUIApplication() let buttons = app.buttons for button in buttons.allElementsBoundByIndex { if button.label = "Login" { button.tap() } } } }
n番目にある要素に対して操作を行う
宣言 | 用途 |
---|---|
func element(boundBy index: Int) -> XCUIElement | index番目のUI要素,indexは0始まり |
var firstMatch: XCUIElement {get} | 1番目のUI要素 |
■ 使用例
import XCTest import UIKit class SampleTestCases: XCTestCase { func testExample() { // 4番目にあるボタン app.buttons.element(boundBy: 3) // 1番目にあるテーブルのセル app.tables.cells.element(boundBy: 0) // 1番目にあるボタン app.buttons.firstMatch } }
要素が見つかったらその時点で検索を止めるため、firstMatch
の方が処理が速い。