1. はじめに
androidのテスト自動化で、アプリの画面外要素を拾う時に
「swipeじゃ嫌です」
って言われました。
それまではこんな感じでした。
swipeのサンプルコード
sleep(2)
swipe :start_x => 500, :end_x => 500, :start_y => 700, :end_y => 400, :duration => 300
sleep(2)
swipe :start_x => 500, :end_x => 500, :start_y => 400, :end_y => 700, :duration => 300
sleep(2)
swipe :start_x => 500, :end_x => 500, :start_y => 400, :end_y => 700, :duration => 300
微笑ましいを通り越して、ダサいと思ったので、
公式ドキュメントを一生懸命読みましたら、なんとか解決しましたので投稿します。
2. 実行環境
items | version | notes |
---|---|---|
appium | 1.5 or later | locatorにnameが使えなくなってるが克服済 |
Android | 4.2 or later | selendroid守備範囲外 |
Ruby | 2.3 or 2.1.5 | windowsの場合2.1.5で |
設定アプリ | - | サンプルコードはAndroidのプリイン「設定」アプリで動かしています |
3. サンプルコード
(1) やりたいこと
Androidの設定アプリを起動して、
画面外の「画面設定」項目までスクロールしてタップ。
(2) スクロールしてタップ。の部分のコード
画面外要素「画面設定」までスクロールしてタップ
find_element(:uiautomator, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "画面設定")').click
(3) 解説
出処はappium.ioなんですね(公式最強説)
http://appium.io/slate/en/master/?ruby#uiautomator-uiselector
appiumは要素の拾い上げにUiSelector, UiScrollableをサポートしてる
そうなので、画面外要素を見つけに行くためUiScrollableを利用します。
んで、公式のサンプルを真似っこして上記のコードを作成。
コードの解説
find_element
(:uiautomater,
'new UiScrollable(
new UiSelector().scrollable(true).instance(0))
.getChildByText(
new UiSelector().className("android.widget.TextView"), "画面設定")').click
【解説】
1行目: 要素を探すfind_element
2行目: UiAutomater属性で検索
3行目: UiScrollableオブジェクトを生成
4行目: UiScrollableのなかでUiSelectorオブジェクトを生成、スワイプしつつアイテム検索できるオブジェクトにする
5行目: 検索用のオブジェクトを渡すメソッド
6行目: android.widget.TextView要素の値が「画面設定」の要素を探してクリック。
(4) コード全体
端末のスリープ設定値が30秒であることをテスト
# viva test automation
# require gems
require 'rubygems'
require 'spec'
require 'appium_lib'
require 'sauce_whisk'
require 'selenium-webdriver'
# capabilities
describe 'pre in app update' do
def desired_caps
{
caps: {
platformName: 'Android',
deviceName: 'adb devices | grep -e "device$" | sed -e "s/device//"',
appPackage: 'com.android.settings',
appActivity: '.Settings'
},
appium_lib: {
wait: 10
}
}
end
before do
Appium::Driver.new(desired_caps).start_driver
end
after do
driver_quit
sleep(5)
end
it 'screen settings' do
Appium.promote_appium_methods Object
find_element(:uiautomator, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "画面設定")').click
sleep(3)
find_element(:uiautomator, 'new UiScrollable(new UiSelector().scrollable(true).instance(0)).getChildByText(new UiSelector().className("android.widget.TextView"), "スリープ")').click
check = find_element(:xpath, "//android.widget.CheckedTextView[@checked='true']").text
expect(check).to include ("30秒")
end
end
4. あとがき
特に解説部分なんか、
オブジェクトとかクラスとかメソッドとかちゃーんと理解できてない気がするので
きっと後から「バカなこと書いてんなー」と思うんだろうな…
勉強します。ハイ。