2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Windows Application Driver で Windows アプリケーションにアタッチ

Last updated at Posted at 2021-03-02

Windows Application Driver を利用して起動済みアプリにアッタッチして操作します。
Pythonですぐ動作が確認できるコードがWeb上で見当たらなかったので同じように困った方の参考になれば嬉しいです。

#1. Windows Application Driver の使い方。
他の方の良い記事を参考にしてください。

  • 要素の取得
C:\Program Files (x86)\Windows Kits\10\bin\XX\x64\inspect.exe
  • Application Driverの起動
C:\Program Files (x86)\Windows Application Driver\WinAppDriver.exe

#2. Pythonで起動済みワードパットにアタッチ。
起動済みワードパットのファイルタブをクリック後、開くをクリックするアクションをします。

attach_wp.py
# -*- coding: utf-8 -*-
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

def getDriverFromWin(win):
    win_handle1 = win.get_attribute("NativeWindowHandle")
    win_handle = format(int(win_handle1), 'x') 
    desired_caps = {}
    desired_caps["appTopLevelWindow"] = win_handle
    driver = launchApp(desired_caps)
    driver.switch_to_window(win_handle)
    return driver

def launchApp(desired_caps):
    dut_url = 'http://127.0.0.1:4723'
    driver = webdriver.Remote(
    command_executor = dut_url,
    desired_capabilities = desired_caps)
    return driver

#nameで指定したクラスにアタッチします.    
def SetUp_SelectClassName(name):
    #set up appium
    desired_caps = {}
    desired_caps["app"] = "Root"
    desktop = launchApp(desired_caps)
    win = WebDriverWait(desktop, 120).until(EC.presence_of_element_located((By.CLASS_NAME,name)))
    driver = getDriverFromWin(win)
    return driver
      
def Click_SelectName(appdriver, name):
   element = appdriver.find_element_by_name(name)
   if True == element.is_enabled():
       element.click()
       
def tearDown(driver):
    driver.quit()    

if __name__ == '__main__':
  #Windows Appliction Driverを起動 . 
  #ワードパッドを起動しておく. 
  appdriver = SetUp_SelectClassName('WordPadClass')
  #ファイルタブをクリック.
  Click_SelectName(appdriver,"ファイル タブ")
  #フォルダを開く.
  Click_SelectName(appdriver,"開く")
  #終了.
  tearDown(appdriver)

参考元

2
3
1

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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?