Tee Posted April 30, 2021 Share Posted April 30, 2021 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) expandcollapse popup; 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 More sharing options...
caramen Posted April 30, 2021 Share Posted April 30, 2021 (edited) 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 April 30, 2021 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
caramen Posted April 30, 2021 Share Posted April 30, 2021 (edited) 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 May 1, 2021 by caramen Xandy 1 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 - WikiOutlookEX (2018-10-31 - Version 1.3.4.1) - Download - General Help & Support - Example Scripts - WikiExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example ScriptsPowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & SupportExcel - Example Scripts - WikiWord - Wiki Tutorials:ADO - Wiki Link to comment Share on other sites More sharing options...
Nine Posted April 30, 2021 Share Posted April 30, 2021 (edited) @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 April 30, 2021 by Nine Xandy 1 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
JockoDundee Posted April 30, 2021 Share Posted April 30, 2021 Why are you using Send instead of ControlSend()? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Tee Posted May 4, 2021 Author Share Posted May 4, 2021 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 More sharing options...
JockoDundee Posted May 4, 2021 Share Posted May 4, 2021 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now