PowerShell

UI Automation PowerShell Extensionsを試しに使ってみました。

d.sunnyone.org: WindowsアプリのUI自動操作をUI Automation PowerShell Extensionで行う」の記事を読んで、UI AutomationのPowerShell用ラッパーがあることを知ったので、早速試してみました。

・UI Automation PowerShell Extensions – Home
http://uiautomation.codeplex.com/

■ 使用準備

  1. 上記ページからZipファイル(2014/10/16 時点ではUIAutomation.0.8.7B3.NET35.zip)をダウンロードして適当なフォルダ(ここではC:\System\UIAutomation)に展開します。
  2. 管理者権限でPowerShellを起動します。UI Automation PowerShell Extensionsを利用するにはPowerShell 2.0以上が必要になるので、「Windows7にPowerShell3.0をインストールする手順」等を参考に、事前にインストールしておいてください。
  3. Set-ExecutionPolicy Bypass」を実行します。
  4. 一時的に試用するだけなので、「ipmo C:\System\UIAutomation\UIAutomation.dll」を実行してモジュールを読み込みます。
  5. (gmo uia*).Name」を実行して、「UIAutomation」が表示されれば準備完了です。
  6. UIAutomationPowerShellExtensions_01

■ UI Automation PowerShell Extensionsを使って電卓操作

UI Automation PowerShell Extensions付属のUIAutomationSpy.exeを実行すると、下記のようなコードを生成することができます。

Get-UiaWindow -Class 'CalcFrame' -Name '電卓' | `
Get-UiaPane -Class 'CalcFrame' | `
Get-UiaPane -Class '#32770' | `
Get-UiaButton -AutomationId '131' -Class 'Button' -Name '1'

UIAutomationPowerShellExtensions_02

このコードとドキュメントを参考に、“電卓を起動して「1」ボタンをクリックする”コードを書くと、下記のようになります。

$calc = Start-Process calc -PassThru | Get-UIAWindow
$btn = $calc | Get-UiaButton -Class 'Button' -Name '1'
$btn | Invoke-UiaButtonClick

UIAutomationPowerShellExtensions_03

上記の通り、非常に短いコードでアプリケーションを操作できることが分かります。
UI Automation PowerShell Extensions – Documentation」を見る限り、コントロールのダブルクリックや右クリックも簡単にできるみたいです。

作成したアプリケーションをテストするときにはかなり役立ちそうなモジュールですね!
知っておいて損は無いだろうと思います。

Office クリップボードをマクロで操作する(UI Automation)前のページ

Office 365 Soloをインストールしてみました。次のページ

関連記事

  1. Windows 10

    「ファイル名を指定して実行」からMicrosoft Edgeを起動する

    以前書いた記事で、Microsoft EdgeをVBScriptから起…

  2. PowerShell

    [PowerShell]PdfSharpを使ってPDFファイルを作成する

    大分前になりますが、当ブログでiTextSharpを使ってPDFファイ…

  3. Office関連

    Excelを使わずにCSVからExcelファイルに変換するPowerShellコード

    CSVファイルからExcelファイルに変換する処理を自動化したい、Ex…

  4. Windows 10

    PowerShellでMicrosoft Edgeを操作する

    前回の記事でWebDriverを使ってMicrosoft Edgeを操…

コメント

  1. この記事へのコメントはありません。

  1. 2019年 1月 03日

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP