Jump to content

How can I retrieve a Dynamic Control ID ?


jrahman
 Share

Go to solution Solved by jrahman,

Recommended Posts

Hi All:

Using AutoIT (v3.3.10.2) I am trying to automate a test for a handheld device using an emulator named MyMobiler on Windows 7.

The screen shots and relevant steps of my problem could be seen in the link below:

http://howtoreaddynamiccontrolid.blogspot.ca/

Problem description:

  1. Each time I start the emulator the Control ID changes to something new. It means, each new instance of the emulator will generate dynamic Control ID
  2. In my AutoIT script, how can I read this dynamic Control ID value of the emulator and use in the rest of the script without doing change the script every time I would run the test

Thanks

 

Link to comment
Share on other sites

Post in the ID structure...you can use regular expressions, or instance based.

HelpFile = 'Controls' to see all the ways to grab controls.

Send back the output of this...after starting the script, activate your window (quickly)

#include <Winapi.au3>
Sleep(5000)
Var_GetAllWindowsControls(WinGetHandle("[ACTIVE]"))
Func Var_GetAllWindowsControls($hCallersWindow)
    ; Get all list of controls
    $sClassList = WinGetClassList($hCallersWindow)

    ; Create array
    $aClassList = StringSplit($sClassList, @CRLF, 2)

    ; Sort array
    _ArraySort($aClassList)
    _ArrayDelete($aClassList, 0)

    ; Loop
    $iCurrentClass = ""
    $iCurrentCount = 1
    $iTotalCounter = 1

    For $i = 0 To UBound($aClassList) - 1
        If $aClassList[$i] = $iCurrentClass Then
            $iCurrentCount += 1
        Else
            $iCurrentClass = $aClassList[$i]
            $iCurrentCount = 1
        EndIf

        $hControl = ControlGetHandle($hCallersWindow, "", "[CLASSNN:" & $iCurrentClass & $iCurrentCount & "]")
        $text = StringRegExpReplace(ControlGetText($hCallersWindow, "", $hControl), "[\n\r]", "{@CRLF}")
        $aPos = ControlGetPos($hCallersWindow, "", $hControl)
        $sControlID = _WinAPI_GetDlgCtrlID($hControl)
        If IsArray($aPos) Then
            ConsoleWrite("Func=[Var_GetAllWindowsControls]: ControlCounter=[" & StringFormat("%3s", $iTotalCounter) & "] ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[" & StringFormat("%4s", $aPos[0]) & "] YPos=[" & StringFormat("%4s", $aPos[1]) & "] Width=[" & StringFormat("%4s", $aPos[2]) & "] Height=[" & StringFormat("%4s", $aPos[3]) & "] Text=[" & $text & "]." & @CRLF)
        Else
            ConsoleWrite("Func=[Var_GetAllWindowsControls]: ControlCounter=[" & StringFormat("%3s", $iTotalCounter) & "] ControlID=[" & StringFormat("%5s", $sControlID) & "] Handle=[" & StringFormat("%10s", $hControl) & "] ClassNN=[" & StringFormat("%19s", $iCurrentClass & $iCurrentCount) & "] XPos=[winclosed] YPos=[winclosed] Width=[winclosed] Height=[winclosed] Text=[" & $text & "]" & @CRLF)
        EndIf
        If Not WinExists($hCallersWindow) Then ExitLoop
        $iTotalCounter += 1
    Next
EndFunc   ;==>Var_GetAllWindowsControls
Edited by jdelaney
IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.
Link to comment
Share on other sites

 

Post in the ID structure...you can use regular expressions, or instance based.

Send back the output of this...after starting the script, activate your window (quickly)

 

Thanks jdelaney

,

As requested, below is the output of the script you attached earlier:

Note that, I am ONLY interested for the first control (ControlID=[5240388])

Func=[Var_GetAllWindowsControls]: ControlCounter=[  1] ControlID=[5240388] Handle=[0x0002076C] ClassNN=[      ATL:0025B3A01] XPos=[   0] YPos=[  54] Width=[ 240] Height=[ 320] Text=[].
Func=[Var_GetAllWindowsControls]: ControlCounter=[  2] ControlID=[59392] Handle=[0x0002076A] ClassNN=[     ReBarWindow321] XPos=[   0] YPos=[   0] Width=[ 240] Height=[  54] Text=[].
Func=[Var_GetAllWindowsControls]: ControlCounter=[  3] ControlID=[59392] Handle=[0x00020768] ClassNN=[   ToolbarWindow321] XPos=[   9] YPos=[  29] Width=[ 215] Height=[  23] Text=[].
Func=[Var_GetAllWindowsControls]: ControlCounter=[  4] ControlID=[5240448] Handle=[0x00060658] ClassNN=[    WTL_CommandBar1] XPos=[   9] YPos=[   2] Width=[ 227] Height=[  21] Text=[].
Edited by jrahman
Link to comment
Share on other sites

  • Solution

With the help from the example script sent by  jdelaney, I have successfully solved the problem to retrieve the ControlID of an Windows object which creates dynamic control ID every time a new instance is invoked.

Following code can be used for retrieving ID of any object whose Control is not predetermined or known.

Usage:

1. Copy and save the following code to your local drive (from where you usually run other .au3 scripts)

2. Change the first two global variable values (in this example, I am using 'MyMobiler' and '3')

3. Run the script

4 Activate the object under test by mouse click after script run

Expected Result:

1. A message box will appear with the ControlID

Comment:

1. You may replace the MsgBox with file or console output

Code:

#include <Winapi.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>

;//Change these two parameters value to find the dynamic control ID for an object
Dim $ObjectTitle = "MyMobiler"
Dim $ArrayElement = 3

GetDynamicControlID($ObjectTitle, $ArrayElement)

;//Function to get the Dynamic Control ID of an Windows object (in this example 'MyMobiler')
Func GetDynamicControlID($objTitle, $arrElement)
  ;Activate the object and retieve the list of windows controls in a string list
  WinWaitActive($objTitle)
  Local $stringClassList = WinGetClassList(WinGetHandle("[ACTIVE]"))
  If @error Then
     MsgBox($MB_SYSTEMMODAL, "ERROR:", "No window named " & $objTitle & " found and no string list of class retuned")
  Else
     ;Create an array and put all string list into that array
     Local $arrayClassList = StringSplit($stringClassList, @CRLF, 2)
     $ControlID = "[CLASS:" & $arrayClassList[$arrElement] & "; INSTANCE:1]"
     MsgBox($MB_SYSTEMMODAL, "SUCCESS:", "Window object named " & $objTitle & " found and string list of class returned: " & $ControlID)
  EndIf

  Return

EndFunc
Link to comment
Share on other sites

  • 5 years later...

#include "MsgBoxConstants.au3"
#include ".\libs\UIAWrappers.au3"
#include ".\libs\CUIAutomation2.au3"
#include "MsgBoxConstants.au3"
#include ".\libs\CUIAutomation2.au3"
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <WinAPI.au3>
#include <FileConstants.au3>
#include ".\libs\ISimpleDOM.au3"
#include ".\libs\MSAccessibility.au3"
#include "WinAPIProc.au3"
#include "WinAPISys.au3"
#include "APISysConstants.au3"
#include "GUIConstantsEx.au3"
#include <GUIListView.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>

; AutoItSetOption("MustDeclareVars", 1)
Sleep(15000)

_UIA_setVar("Global.Highlight", True)

; Wait 50 seconds for the Notepad window to appear.
WinWait("[Title:Edit Template - WINDOW_TITLE]", "", 50)

; Retrieve the handle of the Edit Template window...
WinActivate("[Title:Edit Template - WINDOW_TITLE]", "")
Local $hWnd = WinGetHandle("[Title:Edit Template - WINDOW_TITLE]")
MsgBox($MB_SYSTEMMODAL, "xyzgyVisitNote", $hWnd)        ; SK_VALID; WORKS

$iCmd = 6
Local $hWnd1 = _WinAPI_GetWindow ( $hWnd, $iCmd )

WinActivate("[ACTIVE]")
MsgBox($MB_SYSTEMMODAL, "xyzgyVisitNote", $hWnd1)        ; SK_VALID; WORKS
Sleep(2000)    ;

Local $sClassList = WinGetClassList($hWnd1)        ; HELP REQUIRED HERE? Why it returns nothing; Note- This is a runtime window and I managed to get handle

If @error Then
     MsgBox($MB_SYSTEMMODAL, "ERROR:", "Error Found")
  Else
     MsgBox($MB_SYSTEMMODAL, "ERROR:", "No Error Found")    ; Getting this as output
EndIf

$text = _WinAPI_GetClassName(WinGetHandle("[ACTIVE]"))
MsgBox($MB_SYSTEMMODAL, "xyzgyVisitNote", $text)        ; SK_VALID; WORKS

;~ $hControl = ControlGetHandle($hWnd1, "", "[CLASSNN:" & $text & "]")
;~ MsgBox($MB_SYSTEMMODAL, "xyzgyVisitNote", $hControl)

Local $hWnd2 = ControlGetHandle($hWnd1, "", "[class:ClumpEditorControl]")
MsgBox($MB_SYSTEMMODAL, "xyzgyVisitNote", $hWnd2)

$iCount = ControlListView($hWnd2, "", "[class:ListBox]", "GetItemCount")
MsgBox($MB_SYSTEMMODAL, "xyzgyNonCNote", $iCount)

_WinAPI_CloseWindow($hWnd1)    ;
MsgBox($MB_SYSTEMMODAL, "xyzgyNonCNote", "DONE")
 

Link to comment
Share on other sites

Hard to say, since I cannot run your script.  Have you tried other Win* () functions.  Like WinGetTitle ($hWnd1), what does it say ?

And use <> to post your code (just before emoticon).

Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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