Jump to content

Recommended Posts

Posted (edited)

I have 25 applications .exe on some 30,000+ PCs that my company is testing to ensure they're all working due to changes the SCCM team is planning to rolling out.  I have started the following UI Automation script to check for a test app, 7Zip, launch it, and capture the console output and write the result to a log.  I cannot get it to invoke the application to launch.  Everything else works, right up until the launching/invoking of the application.  I have very limited understanding of UI Automation (just started learning) and have read and study as much as I can about it from  https://docs.microsoft.com/en-us/dotnet/framework/ui-automation/ui-automation-fundamentals.  I am using Larsj and Junkew's UIA UDF.  Any help is much appreciated.

#cs ----------------------------------------------------------------------------

 AutoIt Version: 3.3.14.5
 Author:         myName

 Script Function:
    Template AutoIt script.

#ce ----------------------------------------------------------------------------

; Script Start - Add your code below here
#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7

;#AutoIt3Wrapper_UseX64=n ; If target application is running as 32 bit code
;#AutoIt3Wrapper_UseX64=y ; If target application is running as 64 bit code

#include "UIA_Constants.au3" ; Can be copied from UIASpy Includes folder
;#include "UIA_Functions.au3" ; Can be copied from UIASpy Includes folder
;#include "UIA_SafeArray.au3" ; Can be copied from UIASpy Includes folder
;#include "UIA_Variant.au3" ; Can be copied from UIASpy Includes folder
#include <file.au3>

Opt("MustDeclareVars", 1)
Global $fLogfile = "C:\Temp\Logs\AppsCheck.log"

If Not FileExists($fLogfile) Then
    DirCreate("C:\Temp\Logs\")
EndIf

_Test7zip()

Func _Test7zip()
    ; Create UI Automation object
    Local $oUIAutomation = ObjCreateInterface($sCLSID_CUIAutomation8, $sIID_IUIAutomation6, $dtag_IUIAutomation6)
    If Not IsObj($oUIAutomation) Then Return _LogIt("$oUIAutomation ERR" & @CRLF)
    _LogIt("$oUIAutomation OK" & @CRLF)

    ; Get Desktop element
    Local $pDesktop, $oDesktop
    $oUIAutomation.GetRootElement($pDesktop)
    $oDesktop = ObjCreateInterface($pDesktop, $sIID_IUIAutomationElement9, $dtag_IUIAutomationElement9)
    If Not IsObj($oDesktop) Then Return _LogIt("$oDesktop ERR" & @CRLF)
    _LogIt("$oDesktop OK" & @CRLF)

    _LogIt("--- Find Start Button window/control ---" & @CRLF)

    Local $pCondition0
    $oUIAutomation.CreatePropertyCondition($UIA_ClassNamePropertyId, "Start", $pCondition0)
    If Not $pCondition0 Then Return _LogIt("$pCondition0 ERR" & @CRLF)
    _LogIt("$pCondition0 OK" & @CRLF)

    Local $pButton1, $oButton1
    $oDesktop.FindFirst($TreeScope_Descendants, $pCondition0, $pButton1)
    $oButton1 = ObjCreateInterface($pButton1, $sIID_IUIAutomationElement9, $dtag_IUIAutomationElement9)
    If Not IsObj($oButton1) Then Return _LogIt("$oButton1 ERR" & @CRLF)
    _LogIt("$oButton1 OK" & @CRLF)

    _LogIt("--- Invoke Start Button Pattern (action) Object ---" & @CRLF)

    Local $pInvokePattern1, $oInvokePattern1
    $oButton1.GetCurrentPattern($UIA_InvokePatternId, $pInvokePattern1)
    $oInvokePattern1 = ObjCreateInterface($pInvokePattern1, $sIID_IUIAutomationInvokePattern, $dtag_IUIAutomationInvokePattern)
    If Not IsObj($oInvokePattern1) Then Return _LogIt("$oInvokePattern1 ERR" & @CRLF)
    _LogIt("$oInvokePattern1 OK" & @CRLF)
    $oInvokePattern1.invoke()
    Sleep(1000)

    _LogIt("--- Find 7-zip Manager window/control ---" & @CRLF)

    Local $pCondition1
    $oUIAutomation.CreatePropertyCondition($UIA_AutomationIdPropertyId, "{493f9f47-e8b3-4f5f-8e13-1bc2c14acee7}", $pCondition1)
    If Not $pCondition1 Then Return _LogIt("$pCondition1 ERR" & @CRLF)
    _LogIt("$pCondition1 OK" & @CRLF)

    Local $pListItem1, $oListItem1
    $oDesktop.FindFirst($TreeScope_Descendants, $pCondition1, $pListItem1)
    $oListItem1 = ObjCreateInterface($pListItem1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
    If Not IsObj($oListItem1) Then Return _LogIt("$oListItem1 ERR" & @CRLF)
    _LogIt("$oListItem1 OK" & @CRLF)

    _LogIt("--- ExpandCollapse 7zip Pattern (action) Object ---" & @CRLF)

    Local $pExpandCollapsePattern1, $oExpandCollapsePattern1
    $oListItem1.GetCurrentPattern($UIA_ExpandCollapsePatternId, $pExpandCollapsePattern1)
    $oExpandCollapsePattern1 = ObjCreateInterface($pExpandCollapsePattern1, $sIID_IUIAutomationExpandCollapsePattern, $dtag_IUIAutomationExpandCollapsePattern)
    If Not IsObj($oExpandCollapsePattern1) Then Return _LogIt("$oExpandCollapsePattern1 ERR" & @CRLF)
    _LogIt("$oExpandCollapsePattern1 OK" & @CRLF)
    $oExpandCollapsePattern1.Expand()
    Sleep(1000)

    _LogIt("--- Invoke 7zip window/control ---" & @CRLF)

    Local $pCondition2
    $oUIAutomation.CreatePropertyCondition($UIA_AutomationIdPropertyId, "W~{6D809377-6AF0-444B-8957-A3773F02200E}\7-Zip\7zFM.exe", $pCondition2)
    If Not $pCondition2 Then Return _LogIt("$pCondition2 ERR" & @CRLF)
    _LogIt("$pCondition2 OK" & @CRLF)

    Local $pCustom1, $oCustom1
    $oListItem1.FindFirst($TreeScope_Descendants, $pCondition2, $pCustom1)
    $oCustom1 = ObjCreateInterface($pCustom1, $sIID_IUIAutomationElement, $dtag_IUIAutomationElement)
    If Not IsObj($oCustom1) Then Return _LogIt("$oCustom1 ERR" & @CRLF)
    _LogIt("$oCustom1 OK" & @CRLF)
    $oCustom1.Invoke()
    Sleep(1000)
EndFunc   ;==>_Test7zip

Func _LogIt($sText)
    Local $hFile = FileOpen($fLogfile, 1)
    _FileWriteLog($hFile, $sText & @CRLF)
    FileClose($hFile)
EndFunc

 

Edited by LisHawj
Posted

There are several issues in your text information:

Launching an application
You'll typically use the Run() function to launch an application. You'll not use UI Automation code. UI Automation code is not used until at least the application top window is opened.

Console and terminal windows
You cannot use either UI Automation or Classic automation code to automate console or terminal windows. You use functions designed specifically for the purpose eg. StdoutRead(). There is a brief discussion here in the Various topics section (slightly below the middle of the post). You can find countless examples in this forum.

UI Automation documentation
The documentation you've linked to is the wrong documentation. It's UI Automation in the .NET Framework. The UI Automation that both Junkew and I use is implemented through Windows API functions where you'll find the top of documentation here.

It's very important to read the right documentation. There may be differences in function names, numbers and order of parameters and many other differences. Be sure to read the correct documentation at all times. Especially when you google. Half of the results will usually be about the wrong documentation (the .NET Framework documentation).

Posted

@LarsJ - Thank you for pointing out the documentation error and linking to the correct one.  I'll read up on the links you've provided.  This was absolutely driving me nuts. :)

 

Again, thank you!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...