Jump to content

Problem with ControlClick


Tee
 Share

Recommended Posts

We have some inconsistencies when we are trying to close form with OK button click. Have tried various ways but sometimes it just fails. ( I personally am total noob with AutoIT,l I just mainly run tests, but this problem bugs me quite a bit, there must be something we do wrong, because sometimes it just fails)

 

; todo : fast active removed, seems that doens't help much
Func CloseFormWithButton($ahWnd, $Instance, $mode = "Button")  ;Button

    If WinActivate($ahWnd) = 0 Then
        ;Sleep(1000)
        ;WinActivate($ahWnd)
        ; Set the state of the window to ennabled
        ;WinSetState($hWnd, "", @SW_ENABLE)
        ;TestFailed("CloseFormWithButton, ikkuna ei aktiivinen")  ,tämä pois tästä!
    EndIf

    ; Abount of time we ewaith before trying to close the window
    If $SpeedMode = True Then
        Sleep(500)
    Else
        Sleep(1200)
    EndIf


    If $mode = "Button" Then
        Sleep(1000)
        WinActivate($ahWnd)
        Sleep(500)
        ControlFocus($ahWnd, "", "[CLASS:TButton; INSTANCE:" & $Instance & "]")
        Sleep(500)
        If ControlClick($ahWnd, "", "[CLASS:TButton; INSTANCE:" & $Instance & "]") = 0 Then
            ;TestFailed("Mode: Button, CloseFormWithButton-click failed")
            ControlFocus($ahWnd, "", "[CLASS:TButton; INSTANCE:" & $Instance & "]")
            Send("{ENTER}") ; pitää olla default button!
            LoggingStart("BUTTON CLICK DOEN NOT WORK, IS  IN FAILD SAFE 'BRANCH'")
        EndIf
    Else     ; If want to close window with jsut using Enter-key use this branch
        ;_FastActivate($AhWnd)
        If ControlFocus($ahWnd, "", "[CLASS:TButton; INSTANCE:" & $Instance & "]") = 0 Then
            TestFailed("Mode: Enter, CloseFormWithButton-click Failed ControlFocus")
        EndIf
        Send("{ENTER}") ; Must be as default button
    EndIf
EndFunc   ;==>CloseFormWithButton

First of all, there should be an way to get rid al most of the Sleep() calls, maybe send (some fast responding) message to window, and continue when message pump has processed it.

I am mainly talking about the If WinActivate($ahWnd) = 0 Then branch. if ControlClick it tries to use Enter-key as failsafe, btu seems that it doen't help much.

Most of the time code Above works, but not always. Any help is very much appreciated.

Used AutoIT is 3.3.14.5

 

-Tee-

Link to comment
Share on other sites

Hello @Tee

Welcome to the AutoIt forum !

WinActivate is for activate a Windows. 

The most common used way to check if a window is active is : WinActive

 

If you want to be helped faster, and with pertinent information on your issue, I recommend you to read the topic in my signature. 

" How to ask help. "

 

All reproducer for your issue have to be run without modification from us. 

For that kind of request the best information for us.

-Is AutoIt Windows Info Tool return screenshot. 

-Screenshot of the application. 


Regards. 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

 To feet to your question directly. I would suggest you to use WinWaitActive. And then ControlClick immediately after. 

Here is a shot of this

$myReturn = WinWaitActive ( "title" [, "text" [, timeout = 0]] )

If $myReturn = 0 Then 
        MsgBox ( 0 , "Windows not found" , "The requested windows was not found in the provided timeout"  ) 
Else
        Sleep (20)
        ControlClick ( "title", "text", controlID [, button = "left" [, clicks = 1 [, x [, y]]]] )
EndIf

Another good way if you are 100% sure of the reliability of your script would be to use a while like that

Do 
    
    $myReturn = WinWaitActive ( "title" , ""[, timeout = 0] )
    
Until $myReturn <> 0

Edit : thx @Nine

 

Edited by caramen

My video tutorials : ( In construction )  || My Discord : https://discord.gg/S9AnwHw

How to Ask Help ||  UIAutomation From Junkew || WebDriver From Danp2 || And Water's UDFs in the Quote

Spoiler

 Water's UDFs:
Active Directory (NEW 2018-10-19 - Version 1.4.10.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Link to comment
Share on other sites

@caramen WinWaitActive will wait forever unless you put a timeout.  So using in a loop is useless the way you are doing it.

@Tee The best way to debug an inconsistent program is to put error handling (check for return values and @error) after each and every single statement.  That way you can follow the path used by your script and understand what is going on when it failed. 

I also highly recommend using control handle instead of its CLASSNN.  It allows you to verify with au3info.exe tool if you have the right handle.

Edited by Nine
Link to comment
Share on other sites

Aur AutoIT guy will test these. I have no all info currentöly why code is in the way it is. But it sometimes fails, Window seems to be active by the screenshot, but for reason or other OK button will not get clicked.

This code runs hundreds of times during the test and maybe fails couple of times, and not on same window. So this is bit troubling, to me anyways.

All feedback is greatly appreciated, thanks guys!

 

-Tee-

Link to comment
Share on other sites

Tee, replace your Send() lines with ControlSend() before you do anything else.  Send() just sends to the active window, which usually is what you set it to.  But if something randomly occurs in right before the send() to change the active window, your screwed.  On the other hand, ControlSend goes where you tell it.

So change the lines that say:

Send("{ENTER}")

to

ControlSend($ahWnd, "", "", "{ENTER}")

 

Code hard, but don’t hard code...

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...