25
Powershell UI Automation Jakub Čupera Technology Hour 24.10.2013

PowerShell UIAtomation

Embed Size (px)

DESCRIPTION

Talks about PowerShell UIAutomation used by Y Soft for automating GUI Windows installer testing in multiple languages. How to integrate PowerShell with continuous integration system Jenkins.

Citation preview

Page 1: PowerShell UIAtomation

Powershell UI Automation

Jakub Čupera Technology Hour 24.10.2013

Page 2: PowerShell UIAtomation

Installer

Page 3: PowerShell UIAtomation

Installer

Need of automated GUI tests!

Page 4: PowerShell UIAtomation

Which tool?

● Scripting languages / API

● .NET, Java libraries

● Plugins into VS/Eclipse

● QAliber● SilkTest (Java, .NET, ...)

● Jemmy (Java)

● UI Automation (.NET)

● Powershell UI Automation● TestComplete● TestAutomationFX● AutoHotkey (macros)

● Sikuli (image recognition)

● Selenium (web)

...

GUI Tools

Page 5: PowerShell UIAtomation

And winner is...

Powershell UI Automation

Page 6: PowerShell UIAtomation

Why?

● Free tool, open-source● Known technology

● Simple implementation● Jenkins integration

● Other tools (Pester)● Screenshot generation

Page 7: PowerShell UIAtomation

Powershell

● Scripting language

● Management framework

● Integrated since Windows XP

Page 8: PowerShell UIAtomation

Powershell

● Dynamically typed variables ($)● Conditions● Loops (while, do, for, foreach)

● Lambda expressions● Exception handling● Access to .NET ( [string]::Format(...) )● Can run Cmdlets, PS scripts, functions,

executables

Scripting language

Page 9: PowerShell UIAtomation

Powershell

● Pipeline (| operator)

● Tab completion● Support wildcards (*)

● External modules

Scripting language

Page 10: PowerShell UIAtomation

Powershell Cmdlets

● Services ● Server Management● Windows Server Update● Scheduled Tasks● DNS/IP management● Active Directory● Exchange● ...

Management Framework

Page 11: PowerShell UIAtomation

UI Automation

● API to access/manipulate GUI elements

● Successor to MSAA

● Released by Microsoft in 2005

● Native part since Vista/Server 2008

● Separate packages down to Windows XP

● Managed API since .NET 3.0

● Used also for assistive technologies

Page 12: PowerShell UIAtomation

Powershell UI Automation

● Powershell module using UIA API ● Wide range of supported UI operations● Supports Win32, Windows Forms, WPF,

Metro UI, Java SWT, PowerBuilder, Delphi ● Requires Powershell 2.0+ and .NET 3.5+

Page 13: PowerShell UIAtomation

Installation

● Download

http://uiautomation.codeplex.com● Extract

~\Documents\WindowsPowershell\Modules● Run Powershell and import

Import-Module (ipmo)

Page 14: PowerShell UIAtomation

Available actions

● Getting window (Get-UIAWindow)● Getting components (Get-UIAButton, Get-UIAEdit,)● Invoking actions (Invoke-UIACheckBoxToggle)● Setting values (Set-UIAEditText)● Getting values (Get-UIAEditText)● Browsing menu (Get-UIAMenuItem, Invoke-UIAMenuItem)● Checking state (Test-UIAControl)● Screenshots (Save-UIAScreenshot)● Get component attributes (Read-UIAControlName)● Waiting for element state (Wait-UIAButtonEnabled)● Start-UIARecorder● …....

Page 15: PowerShell UIAtomation

Getting Window

● Process Name (-ProcessName, -pn)

● Process Id (-ProcessId, -pid)

● By Title (-Name)

● From process

● Wildcards can be used

● Returns AutomationElement object

● Get-UIAActiveWindow

● What if more processes are started?

Get-UIAWindow

Page 16: PowerShell UIAtomation

Getting a Control

● Various Cmdlets to obtain all possible GUI elements

Get-UIAButton, Get-UIAEdit, Get-UIARadioButton, ...

● Returns AutomationElement object

● Many aliases

Get-UIAEdit = Get-UIATextBox

● Cmdlets wrap Get-UIAControl

● Name (-Name, -n)

● Automation Id (-AutomationId)

● Wildcards can be used

● Multiple objects can be selected ([x-y])

● Popups are also processed

Get-UIAControl

Page 17: PowerShell UIAtomation

Actions

● Mouse clicking

Invoke-UIAButtonClick, Invoke-UIAMenuItemClick, Invoke-UIACheckBoxToggle ...● Scrolling

Invoke-UIAScrollBarScroll● Handling texts

Get-UIAEditText, Set-UIAEditText● And many others

Invoke-UIAMenuItemExpand, Invoke-UIACalendarScroll, Set-UIAFocus,...

● Actions are type and context specific

Invoke-UIAAction

Page 18: PowerShell UIAtomation

Other Cmdlets

● Read-UIAControlName, Read-UIAControlAutomationId

Get-UIAButton | Read-UIAControlName

● Save-UIAScreenshot

Save-UIAScreenShot -Path “c:\temp“ -Name 'test.jpg' -As jpeg

● Test-UIAControlState

Test-UIAControlState -SearchCriteria @{Name=“OK“, ControlType=“button“ }

● Wait-UIAControlState

Get-UIAButton | Wait-UIAButtonEnabled -Timeout 20

● Start-UIARecorder

● Move-UIACursor

● ...

Other Cmdlets

Page 19: PowerShell UIAtomation

Passing Objects

● Passing control object as parameter

$btn = Get-UIAButton -Name „Next“

Invoke-UIAButtonClick -InputObject $btn

● Passing object through pipe

Get-UIAButton -Name „Next“ | Invoke-UIAButtonClick

● No need to pass window

Page 20: PowerShell UIAtomation

Global Settings

● Global settings can be modified

Logging, Timeouts, Highlighting, Screenshots, Error handling, ...

● Usage:

[UIAutomation.Preferences]::Key = $value

● Cmdlet Show-UIAModuleSettings

● Some settings can be overriden locally

Get-UIAButton -Timeout 20

Page 21: PowerShell UIAtomation

Other Functionality

● Custom event handling-OnErrorEvent, -OnSuccess

● Cmdlets as tests -TestResultName

● Save screenshot on error

● Detailed logging

Other Functionality

Page 22: PowerShell UIAtomation

Examples

Page 23: PowerShell UIAtomation

Tips & Tricks

● Tools

UIVerify, UISpy, Spy++

● Getting control nane

Get-UIAWindow | Get-UIAButton | Read-UIAControlName

Get-UIAWindow | Get-UIAControl | Read-UIAControlName

● Get cmdlets related to control

Get-Command -Module UIA* *Button*

Page 24: PowerShell UIAtomation

Gotchas

● Lack of documentation

● Last update in February 2013

● Still officially Beta

● Loosing focus on window

● Needs to set focus on element to perform action

● User account limitations

● Save screenshot on error by default

Page 25: PowerShell UIAtomation

Do you want to know more?

● http://msdn.microsoft.com (UIA API, .NET)● http://uiautomation.codeplex.com ● http://softwaretestingusingpowershell.com