LoginSignup
0
0

More than 5 years have passed since last update.

appiumでapkから各要素のidを調査する方法

Posted at

appiumを使ったandroidアプリのテストでは、各要素のidなどを指定して操作する。
特にダイアログボックス内のタイトルなど動的に生成した要素はidが不明だったりする。

方法

appiumのGUIを使う。

右上の虫眼鏡マークを押す


こんな画面が出るので、Desired Capabilitiesに要素を確認したいアプリとエミュレータの設定を書く。
appiumでテスト用コードを書くときに

desired_caps = {}
        desired_caps['platformName'] = 'Android'
        desired_caps['platformVersion'] = '8.0'
        desired_caps['deviceName'] = 'Nexus'
        # Returns abs path relative to this file and not cwd
        desired_caps['app'] = os.path.abspath(os.path.join(os.path.dirname(__file__),'apps/Chess Free.apk'))
        desired_caps['appPackage'] = 'uk.co.aifactory.chessfree'
        desired_caps['appActivity'] = '.ChessFreeActivity'

みたいな設定を書いているはずなので、それと同じものを書く。
appiumの導入~テストコード作成までの流れは筆者の過去の記事参照。


できたらエミュレータを立ち上げたうえで、Start Sessionを押す。
Save As ...を押すと今書いた設定を保存できる。


こんな画面になる。
左上の矢印3つがそれぞれセレクタ、スワイプ、タップになっている。
右側のセレクタで選択した要素の詳細が右のSelected Elementに表示される。

(アプリ設計時の)注意

たとえばfragmentは画面の上に重なっているだけなので、appiumでfind_elements_by_hogehoge()とかすると下にある要素が普通に引っかかる。
特にby_xpathは動作が不安定になりがちなので、layout作成時に複数重なったfragmentを含めてidが一意になるようにすべき。

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0