Guest User

Untitled

a guest
Mar 24th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. #REQUIRES -Version 3.0
  2.  
  3. # This is a simple sample for access the MS UIAutomation in PowerShell.
  4. # In this sample:
  5. # 1. Load the MS UIA via System.Reflection.Assembly
  6. # 2. Launch the AUT ( calc.exe )
  7. # 3. Find the AutomationElement via the AUT Process Id
  8. # 4. Find buttons via 'ClassName' and 'Name' property
  9. # 5. Click the '1', '+', '1', '=' buttons.
  10. # At last, we will get '2' in the result of calc App.
  11.  
  12. # Load MS UIAutomation assemblies
  13. Write-Host "Loading MS UIA assemblies"
  14. [void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClient")
  15. [void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationTypes")
  16. [void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationProvider")
  17. [void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClientsideProviders")
  18.  
  19.  
  20. # Register client side provider
  21. Write-Host "Register client-side provider"
  22. $client = [System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClientsideProviders")
  23. [Windows.Automation.ClientSettings]::RegisterClientSideProviderAssembly($client.GetName())
  24.  
  25. # Launch the AUT ( calc.exe ) & sleep 10 seconds for wait the process start
  26. Write-Host "Launching the AUT"
  27. $autProc = Start-Process calc -PassThru
  28. Start-Sleep -s 10
  29.  
  30. # Find the calc Element via the process Id
  31. Write-Host "Searching the AUT Root element"
  32. $rootElement = [Windows.Automation.AutomationElement]::RootElement
  33. $condAUTProc = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ProcessIdProperty, $autProc.Id)
  34. $autElement = $rootElement.FindFirst([Windows.Automation.TreeScope]::Children, $condAUTProc)
  35.  
  36. # Find the buttons '1', '+', '='
  37. Write-Host "Searching the button elements"
  38. $condBtn = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ClassNameProperty, "Button")
  39.  
  40. $condName = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, "1")
  41. $condTarget = New-Object Windows.Automation.AndCondition($condBtn, $condName)
  42. $btn1Element = $autElement.FindFirst([Windows.Automation.TreeScope]::Descendants, $condTarget)
  43.  
  44. $condName = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, "+")
  45. $condTarget = New-Object Windows.Automation.AndCondition($condBtn, $condName)
  46. $btnPlusElement = $autElement.FindFirst([Windows.Automation.TreeScope]::Descendants, $condTarget)
  47.  
  48. $condName = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::NameProperty, "=")
  49. $condTarget = New-Object Windows.Automation.AndCondition($condBtn, $condName)
  50. $btnEqualElement = $autElement.FindFirst([Windows.Automation.TreeScope]::Descendants, $condTarget)
  51.  
  52. # Click the buttons
  53. Write-Host "Clicking the buttons"
  54. $btn1Element.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern).Invoke()
  55. Start-Sleep -s 1
  56. $btnPlusElement.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern).Invoke()
  57. Start-Sleep -s 1
  58. $btn1Element.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern).Invoke()
  59. Start-Sleep -s 1
  60. $btnEqualElement.GetCurrentPattern([Windows.Automation.InvokePattern]::Pattern).Invoke()
  61. Start-Sleep -s 1
  62.  
  63. Write-Host "Finished. Please check the results on the AUT."
Add Comment
Please, Sign In to add comment