Jump to content
Sign in to follow this  
jrahman

How can I retrieve a Dynamic Control ID ?

Recommended Posts

jrahman

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

 

Share this post


Link to post
Share on other sites
jdelaney

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.

Share this post


Link to post
Share on other sites
jrahman

 

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

Share this post


Link to post
Share on other sites
jrahman

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

Share this post


Link to post
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
Sign in to follow this  

×