Shiratesを使ってみよう - select関数 -
Shiratesのselect関数
を紹介します。
画面要素の取得方法
Shiratesでは画面要素を取得するのにAppiumと同様に以下の方法を使用できます。
- text
- content-desc
- resource-id
- class
- xpath
テキスト属性(text)による画面要素の取得
Shiratesでは下記のようにAppiumよりも簡素な記述で要素を取得できます。
select("テキスト")
また、スクロールする画面においては以下のように記述することができます。
selectWithScrollDown("テキスト")
アクセシビリティ属性(content-desc)による画面要素の取得
content-descを指定して要素を取得する場合は"@"を付与します。
select("@上へ移動")
select関数の詳細はこちらを参照ください。
サンプルコード
サンプルコードの入手
本記事で説明するサンプルコードの完成版はこちらから入手してください。 https://github.com/wave1008/shirates-samples-selectors
SelectorTest
import org.junit.jupiter.api.DisplayName import org.junit.jupiter.api.Order import org.junit.jupiter.api.Test import shirates.core.driver.commandextension.* import shirates.core.testcode.UITest class SelectTest : UITest() { @Test @Order(10) @DisplayName("テキスト属性で要素を取得する") fun selectByText() { scenario { case(1) { action { it.select("ネットワークとインターネット") }.expectation { it.textIs("ネットワークとインターネット") } } case(2) { action { it.selectWithScrollDown("システム") }.expectation { it.textIs("システム") } } } } @Test @Order(20) @DisplayName("アクセシビリティ属性(content-desc)で要素を取得する") fun selectByAccessibility() { scenario { case(1) { condition { it.tapWithScrollDown("システム") }.action { it.select("@上へ移動") }.expectation { it.accessIs("上へ移動") } } } } }
サンプルコードの実行結果