end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

(再) install WindowsApplicationDriver

https://end0tknr.hateblo.jp/entry/20190323/1553338368

先日、上記entryにて、Windows Application Driver をinstallしていますが、 あまりに中途半端なので、再度、hands-on。

find_element(s)by_accessibility_id() , find_element(s)by_class_name() , find_element(s)by_id() , find_element(s)by_name() , find_element(s)by_tag_name() , find_element(s)by_xpath() 等、要素探索のmethodが用意されていますが、 「WinAppDriver UI Recorder」でXPATHを調べ find_element_by_xpath()による要素探索が最も良かった。

ただし、アプリによっては「WinAppDriver UI Recorder」や「inspect.exe」による 調査中にアプリが強制終了する点が、なかなか難しい。

事前準備

Windows Application Driver

以下の WinAppDriver.exe を起動すると、port:4723 経由でwindowsアプリを操作できます。

WinAppDriver UI Recorder

https://github.com/Microsoft/WinAppDriver/releases/download/UiR_v1.0-RC/WinAppDriverUIRecorder.zip

上記zipを解凍し、 WinAppDriverUiRecorder.exe を起動すると、 アプリの各要素のXPATHを調べることができます。

inspect.exe

「WinAppDriver UI Recorder」と同様ですが、 アプリの各要素のクラス名や要素名等の様々な項目を調べることができます。

https://developer.microsoft.com/ja-jp/windows/downloads/sdk-archive からダウンロードし、インストールすると、以下に配備されます。

C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64\inspect.exe

https://developer.microsoft.com/ja-jp/windows/downloads/sdk-archive

sample script

# -*- coding: utf-8 -*-
from appium import webdriver
import time

def main():
    ## アプリ起動
    desired_caps = {}
    desired_caps["app"] = r"C:\SCOOP\SCOOP22\Program\PLANMAN.exe"
    driver = webdriver.Remote(
        command_executor='http://127.0.0.1:4723',
        desired_capabilities= desired_caps)

    win_planman = init_planman(driver)
    print win_planman

    ## ↑ここまではうまく動作しました。
    ## ↓この先で、ListViewやTreeViewを探索したかったのですが
    ##   アプリが強制終了するなど、うまく動作しませんでした。
    pane_tree = win_planman.find_elements_by_class_name("ListLiew")
    print pane_tree

    
    ## 終了
    driver.quit()

def init_planman(driver):
    ## logon画面から遷移
    xpath_str = \
        "/".join(['/Pane[@Name="デスクトップ 1"][@ClassName="#32769"]',
                  'Window[@Name="ログオン"][@ClassName="ThunderRT6FormDC"]'])
    diagram_logon = driver.find_element_by_xpath(xpath_str)

    xpath_str = \
        "//Button[@Name=\"OK\"][@ClassName=\"ThunderRT6CommandButton\"]"
    diagram_logon.find_element_by_xpath(xpath_str).click()

    
    ## version check画面から遷移
    xpath_str = \
        "/".join(['/Pane[@Name="デスクトップ 1"][@ClassName="#32769"]',
                  'Window[@Name="SCOOP22 マネージャ"][@ClassName="#32770"]'])
    try:
        diagram_ver_chk = driver.find_element_by_xpath(xpath_str)
    except Exception as e:
        pass  ## version check画面が単に見つからない場合、無視して進みます
    else:
        xpath_str = "//Button[@Name=\"OK\"][@ClassName=\"Button\"]"
        diagram_ver_chk.find_element_by_xpath(xpath_str).click()
    
    ## plan manager探索
    xpath_str = \
        "/".join(['/Pane[@Name="デスクトップ 1"][@ClassName="#32769"]',
                  'Window[@Name="SCOOP22 マネージャ - soop22"]'+
                  '[@ClassName="ThunderRT6FormDC"]'])
    try:
        win_planman = driver.find_element_by_xpath(xpath_str)
    except Exception as e:
        print e , " " , xpath_str
        return

    return win_planman


if __name__ == '__main__':
    main()