SwiftUI component UITest シリーズ DisclosureGroup 編
Sponsor Link
対象要素の取得
SwiftUI の DisclosureGroup は、accessibility ID を使って取得できます。
XCUIElementTypeQueryProvider からの取得には、buttons を使用します。
accessibility ID を使って取得
1 2 3 4 5 6 |
let app = XCUIApplication() app.launch() let toggle = app.buttons["MyDisclosureGroup"] |
状態のチェック
DiscloseGroup が開かれているかを、DisclosureGroup 自体から取得することはできませんでした。
当面の対策としては、Disclose 対象となっている要素を取得して exists/isHittable に該当するかどうかで判断することはできそうです。
操作
DisclosureGroup に対して、.onTap を指定すると、Disclose のトグル動作を行います。
テストコード
以下では、dg1 という accessibility ID を付与した DisclosureGroup に対して、tap 後で、Text が表示に切り替わっていることをテストしています。
DisclosureGroup テスト例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// UI tests must launch the application that they test. let app = XCUIApplication() app.launch() // (1) let dcg1 = app.buttons["dcg1"] // get disclosure // (2) var txt1 = app.staticTexts["txt1"] // get item in disclosure, but failed... // (3) dcg1.tap() // (4) XCTAssertTrue(txt1.isHittable) |
コード解説
- DisclosureGroup を取得
- DisclosureGroup に含まれる要素を取得 (*1)
- DisclousreGroup を tap することで、open/close を制御
- DisclosureGroup に含まれている要素が表示されているかをテスト (*1)
ですので、(4) のテストも動作しません。
さらに調べたところ、DisclosureGroup に含まれる Text 要素が、DisclosureGroup と同じ identifier を持つことがわかりましたが、これが正しい動作とは思えません。
なお、表示されている状態でないと取得することはできませんでした。
まとめ:SwiftUI の DisclosureGroup をテストする
SwiftUI の DisclosureGroup をテストする
- XCUIApplication から、accessibility ID 指定で DisclosureGroup を取得する
- XCUIApplication から、buttons 経由で取得する
- tap() で open/close 動作となる
- DisclosureGroup に含まれている要素は、取得できない(Text が取得できないことを確認)
注意
ドキュメントがほとんどなく、手探りで書いてます。
結論としては、DisclosureGroup を UITest 対象にするには、時期尚早っぽいです。
調べた範囲では、上記のような状況でテストできそうもないのですが、ご存知の方、教えていただけると嬉しいです。
説明は以上です。
不明な点やおかしな点ありましたら、こちらまで。
Sponsor Link