Jump to content

Recommended Posts

Posted

I am trying to get automate testing of a windows application and it all works so far (I've only created five tests) but I cannot seem to find the magic combination to get a key combination to work correctly.

In the interface Ctrl-K followed by Ctrl-S should show a keymap dialog.  But it's not exactly that.  It's Ctrl-K, keep holding down the control key, press S, release control key.  So I coded this:

Send("{CTRLDOWN}")

Send("k")

Send("s")

Send("{CTRLUP}")

But I will be darned if I can figure out why it isn't working.  I looked through the FAQ's (sorry if this is covered somewhere but I didn't find anything to address it).  I tried setting the SendKeyDownDelay to 10--maybe I need to set it longer yet?    I dropped a MsgBox right after this part of the code but that didn't seem to help me at all because the dialog isn't there which seems to say the keystrokes aren't registering. At one point it was behaving as if it weren't seeing the initial Ctrl-K but only seeing the Ctrl-S; that is, it was popping up a Save Dialog which is what happens when Ctrl-S is pressed.

I wish I could figure out how to debug this one myself (i did see something about debugging the scripts) but I do think that if I put a break point in the script I'll throw the timing off so I'm not even sure that would help me.   If this is a FAQ and I missed it, I do apologize.  Otherwise, if someone would look kindly on an AutoIt novice and suggest how I might figure out why this isn't working I'd appreciate it!

Posted

Hi @OldDutchCap,

1 hour ago, OldDutchCap said:

I am trying to automate regression tests for a text editor (Zed) on Windows

Do you think AutoIt and key pressing actions would fulfill your requirements on your test cases? To be honest, I don't think so 🤔 .

But you already wrote you had some success ...

16 hours ago, OldDutchCap said:

it all works so far (I've only created five tests)

... so please share your code (and the test cases) and we might be helpful 🤝 .

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted

Sorry.  I thought your request was against forum rules.  It is not obvious to help with so little information.  As we do not have your Zed editor, it is quite hard for us to know what is wrong and what could solve your issue.

Maybe you could use menu / context menu to activate the keymap screen ?

Or maybe the sequence of key pressed/released is not exactly what you described ?

ps.  when you post code, please use the method shown in the link.  Thanks. 

 

 

Posted

Apologies 9 and SolveSmart.  Yes I should have known better than to ask a question without sharing code.  

 

; ------
; Start Zed 
; ------

#include <MsgBoxConstants.au3>

Const $zedWindowClass = "[CLASS:Zed::Window]"

Func OutputResult($testName, $testResult)
    $resultString = "Test " & $testName 
    $resultString &= " " 
    $resultString &=  $testResult ? "Passed" : "Failed"
    return $resultString
EndFunc

Func StartZed()
    Run("pwsh c:\users\public\zed.ps1")
    $HWnd = WinWaitActive($zedWindowClass)
    return $HWnd
EndFunc

Func CloseZed($keystrokesToClose = Default)
    if $keystrokesToClose = Default Then
        ;~ Standard Windows Close Key Combo
        Send("!{F4}")
    else
        Send($keystrokesToClose)
    endif
EndFunc

Func TestZedStartupAndShutdown_CtrlQ()
    StartZed()
    Sleep(1000)
    CloseZed("^q")
    Sleep(1000)
    
    # Test 
    $actual = WinExists($zedWindowClass)
    $expected = 0
    return ($actual = $expected) 
EndFunc

Func TestZedStartupAndShutdown_StandardWindows()
    StartZed()
    Sleep(1000)
    CloseZed()
    Sleep(1000)
    
    # Test 
    $actual = WinExists($zedWindowClass)
    $expected = 0
    return ($actual = $expected) 
EndFunc

Func TestOpenSettingsTab()
    StartZed()
    Sleep(1000)
    Send("^,")
    Sleep(100)
    $winTitle = WinGetTitle("[ACTIVE]")
    $positionOfSettingsInTitle = StringInStr($winTitle,"settings")
    $actual = $positionOfSettingsInTitle > 0
    $expected = True
    CloseZed()
    return ($actual = $expected)
EndFunc

Func TestKeyboardMappings()
    Sleep(1000)
    Send("{CTRLDOWN}")
    Send("k")
    Send("s")
    Send("{CTRLUP}")
    Sleep(100)
    $winTitle = WinGetTitle("[ACTIVE]")
    $positionOfKeymapInTitle = StringInStr($winTitle,"keymap")
    $actual = $positionOfKeymapInTitle > 0
    $expected = True
    CloseZed()
    return ($actual = $expected)
EndFunc

Func TestSelectTheme()
    StartZed()
    Sleep(1000)
    Send("{CTRLDOWN}")
    Send("k")
    Send("t")
    Send("{CTRLUP}")
    Sleep(100)
    MsgBox($MB_ICONINFORMATION,"ZedTests","After Key Chord")
    $winTitle = WinGetTitle("[ACTIVE]")
    $positionOfSelectThemeInTitle = StringInStr($winTitle,"Theme")
    $actual = $positionOfSelectThemeInTitle > 0
    $expected = True
    CloseZed()
    return ($actual = $expected)
EndFunc    

Func RunAllTests()
    Local $testsToRun[] = ["TestZedStartupAndShutdown_CtrlQ", "TestZedStartupAndShutdown_StandardWindows", "TestOpenSettingsTab", "TestKeyboardMappings", "TestSelectTheme"]
    Local $resultOfTest[UBound($testsToRun)]

    For $i = 0 to UBound($testsToRun)-1
        $resultOfTest[$i] = Call($testsToRun[$i])
    Next
    
    For $i = 0 to UBound($testsToRun)-1
        MsgBox($MB_ICONINFORMATION, "Zed Tests", OutputResult($testsToRun[$i],$resultOfTest[$i]))
    Next
EndFunc

RunAllTests()

TestZedStartupAndShutdown_CtrlQ, TestZedStartupAndShutdown_StandardWindows, and TestOpenSettingsTab all work as expected.  TestKeyboardMappings and TestSelectTheme both fail.  Since both of those need to use the Ctrl key chord, I'm guessing that's the source of the problem although it might be other things. 

By the way, if there's anything in the FAQ's about how to check certain conditions holding true, I hope I wouldn't be imposing by asking you to point me to them.  As you can see, right now I'm simply checking to ensure windows got closed and I'm checking the windows title strings.  Since this is an attempt to automate regression testing I'd like to be able to check other things--such as the text displayed in windows and things of that sort.

Posted

Apologies 9 and SolveSmart.  Yes I should have known better than to ask a question without sharing code.  

 

; ------
; Start Zed 
; ------

#include <MsgBoxConstants.au3>

Const $zedWindowClass = "[CLASS:Zed::Window]"

Func OutputResult($testName, $testResult)
    $resultString = "Test " & $testName 
    $resultString &= " " 
    $resultString &=  $testResult ? "Passed" : "Failed"
    return $resultString
EndFunc

Func StartZed()
    Run("pwsh c:\users\public\zed.ps1")
    $HWnd = WinWaitActive($zedWindowClass)
    return $HWnd
EndFunc

Func CloseZed($keystrokesToClose = Default)
    if $keystrokesToClose = Default Then
        ;~ Standard Windows Close Key Combo
        Send("!{F4}")
    else
        Send($keystrokesToClose)
    endif
EndFunc

Func TestZedStartupAndShutdown_CtrlQ()
    StartZed()
    Sleep(1000)
    CloseZed("^q")
    Sleep(1000)
    
    # Test 
    $actual = WinExists($zedWindowClass)
    $expected = 0
    return ($actual = $expected) 
EndFunc

Func TestZedStartupAndShutdown_StandardWindows()
    StartZed()
    Sleep(1000)
    CloseZed()
    Sleep(1000)
    
    # Test 
    $actual = WinExists($zedWindowClass)
    $expected = 0
    return ($actual = $expected) 
EndFunc

Func TestOpenSettingsTab()
    StartZed()
    Sleep(1000)
    Send("^,")
    Sleep(100)
    $winTitle = WinGetTitle("[ACTIVE]")
    $positionOfSettingsInTitle = StringInStr($winTitle,"settings")
    $actual = $positionOfSettingsInTitle > 0
    $expected = True
    CloseZed()
    return ($actual = $expected)
EndFunc

Func TestKeyboardMappings()
    Sleep(1000)
    Send("{CTRLDOWN}")
    Send("k")
    Send("s")
    Send("{CTRLUP}")
    Sleep(100)
    $winTitle = WinGetTitle("[ACTIVE]")
    $positionOfKeymapInTitle = StringInStr($winTitle,"keymap")
    $actual = $positionOfKeymapInTitle > 0
    $expected = True
    CloseZed()
    return ($actual = $expected)
EndFunc

Func TestSelectTheme()
    StartZed()
    Sleep(1000)
    Send("{CTRLDOWN}")
    Send("k")
    Send("t")
    Send("{CTRLUP}")
    Sleep(100)
    MsgBox($MB_ICONINFORMATION,"ZedTests","After Key Chord")
    $winTitle = WinGetTitle("[ACTIVE]")
    $positionOfSelectThemeInTitle = StringInStr($winTitle,"Theme")
    $actual = $positionOfSelectThemeInTitle > 0
    $expected = True
    CloseZed()
    return ($actual = $expected)
EndFunc    

Func RunAllTests()
    Local $testsToRun[] = ["TestZedStartupAndShutdown_CtrlQ", "TestZedStartupAndShutdown_StandardWindows", "TestOpenSettingsTab", "TestKeyboardMappings", "TestSelectTheme"]
    Local $resultOfTest[UBound($testsToRun)]

    For $i = 0 to UBound($testsToRun)-1
        $resultOfTest[$i] = Call($testsToRun[$i])
    Next
    
    For $i = 0 to UBound($testsToRun)-1
        MsgBox($MB_ICONINFORMATION, "Zed Tests", OutputResult($testsToRun[$i],$resultOfTest[$i]))
    Next
EndFunc

RunAllTests()

TestZedStartupAndShutdown_CtrlQ, TestZedStartupAndShutdown_StandardWindows, and TestOpenSettingsTab all work as expected.  TestKeyboardMappings and TestSelectTheme both fail.  Since both of those need to use the Ctrl key chord, I'm guessing that's the source of the problem although it might be other things. 

By the way, if there's anything in the FAQ's about how to check certain conditions holding true, I hope I wouldn't be imposing by asking you to point me to them.  As you can see, right now I'm simply checking to ensure windows got closed and I'm checking the windows title strings.  Since this is an attempt to automate regression testing I'd like to be able to check other things--such as the text displayed in windows and things of that sort.

Oh and I'm working this on Windows 11 with AutoIt 3.0.16 (I think--sorry I don't have the machine handy right now to double check it).

Posted

Oh puh ... 😅 , okay. Of course we can try to solve your issue with the keys pressed in combination, but my recommendation is to (re)evaluate Desktop UI testing tools. AutoIt provides next to the mouse and key actions also ControlClick, ControlCommand mechanisms and so on. But Zed is written in Rust - so you would probably not be very successful with these options.

Anyhow, at the end it's your choise 🤝 .

I tweaked your code just a tiny bit (setting "Local" scope to variables and adding option "Opt("SendKeyDelay", 100)" which you can play with.

18 hours ago, OldDutchCap said:

etting the SendKeyDownDelay to 10--maybe I need to set it longer yet

Yes, 10 milliseconds are still not what a user would do in an UI. So try 100 or even 250 between each keystroke. Only for testing, later reduce the delay more and more.

#AutoIt3Wrapper_AU3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#AutoIt3Wrapper_AU3Check_Stop_OnWarning=y

#include-once
#include <MsgBoxConstants.au3>

; ------
; Start Zed
; ------

Opt("SendKeyDelay", 100)
;~ Opt("SendKeyDownDelay", 100)

Const $zedWindowClass = "[CLASS:Zed::Window]"

Func OutputResult($testName, $testResult)
    Local $resultString = "Test " & $testName
    $resultString &= " "
    $resultString &= $testResult ? "Passed" : "Failed"
    Return $resultString
EndFunc

Func StartZed()
    Run("pwsh c:\users\public\zed.ps1")
    Local $HWnd = WinWaitActive($zedWindowClass)
    Return $HWnd
EndFunc

Func CloseZed($keystrokesToClose = Default)
    If $keystrokesToClose = Default Then
    ;~ Standard Windows Close Key Combo
        Send("!{F4}")
    Else
        Send($keystrokesToClose)
    EndIf
EndFunc

Func TestZedStartupAndShutdown_CtrlQ()
    StartZed()
    Sleep(1000)
    CloseZed("^q")
    Sleep(1000)

    # Test
    Local $actual = WinExists($zedWindowClass)
    Local $expected = 0
    Return ($actual = $expected)
EndFunc

Func TestZedStartupAndShutdown_StandardWindows()
    StartZed()
    Sleep(1000)
    CloseZed()
    Sleep(1000)

    # Test
    Local $actual = WinExists($zedWindowClass)
    Local $expected = 0
    Return ($actual = $expected)
EndFunc

Func TestOpenSettingsTab()
    StartZed()
    Sleep(1000)
    Send("^,")
    Sleep(100)
    Local $winTitle = WinGetTitle("[ACTIVE]")
    Local $positionOfSettingsInTitle = StringInStr($winTitle, "settings")
    Local $actual = $positionOfSettingsInTitle > 0
    Local $expected = True
    CloseZed()
    Return ($actual = $expected)
EndFunc

Func TestKeyboardMappings()
    Sleep(1000)
    Send("{CTRLDOWN}")
    Send("k")
    Send("s")
    Send("{CTRLUP}")
    Sleep(100)
    Local $winTitle = WinGetTitle("[ACTIVE]")
    Local $positionOfKeymapInTitle = StringInStr($winTitle, "keymap")
    Local $actual = $positionOfKeymapInTitle > 0
    Local $expected = True
    CloseZed()
    Return ($actual = $expected)
EndFunc

Func TestSelectTheme()
    StartZed()
    Sleep(1000)
    Send("{CTRLDOWN}")
    Send("k")
    Send("t")
    Send("{CTRLUP}")
    Sleep(100)
    MsgBox($MB_ICONINFORMATION, "ZedTests", "After Key Chord")
    Local $winTitle = WinGetTitle("[ACTIVE]")
    Local $positionOfSelectThemeInTitle = StringInStr($winTitle, "Theme")
    Local $actual = $positionOfSelectThemeInTitle > 0
    Local $expected = True
    CloseZed()
    Return ($actual = $expected)
EndFunc

Func RunAllTests()
    Local $testsToRun[] = ["TestZedStartupAndShutdown_CtrlQ", "TestZedStartupAndShutdown_StandardWindows", "TestOpenSettingsTab", "TestKeyboardMappings", "TestSelectTheme"]
    Local $resultOfTest[UBound($testsToRun)]

    For $i = 0 To UBound($testsToRun) - 1
        $resultOfTest[$i] = Call($testsToRun[$i])
    Next

    For $i = 0 To UBound($testsToRun) - 1
        MsgBox($MB_ICONINFORMATION, "Zed Tests", OutputResult($testsToRun[$i], $resultOfTest[$i]))
    Next
EndFunc

RunAllTests()

Also, try ...

Send('^k')
Send("s")

; or 

Send("{CTRLDOWN}")
Send("k")
Send("{CTRLUP}")
Send("s")

; instead of

Send("{CTRLDOWN}")
Send("k")
Send("s")
Send("{CTRLUP}")

... maybe this works (like in VSCode).

Best regards
Sven

==> AutoIt related: 🔗 GitHub, 🔗 Discord Server

Spoiler

🌍 Au3Forums

🎲 AutoIt (en) Cheat Sheet

📊 AutoIt limits/defaults

💎 Code Katas: [...] (comming soon)

🎭 Collection of GitHub users with AutoIt projects

🐞 False-Positives

🔮 Me on GitHub

💬 Opinion about new forum sub category

📑 UDF wiki list

✂ VSCode-AutoItSnippets

📑 WebDriver FAQs

👨‍🏫 WebDriver Tutorial (coming soon)

Posted

Here what Zed says :

Quote

User keymaps

Zed reads your keymap from ~/.zed/keymap.json on MacOS (or ~/.config/zed/keymap.json on Linux). You can open the file within Zed with ctrl-k ctrl-s, or via zed: Open Keymap in the command palette.

Reading it should be

Send("^k")
Sleep(50)
Send("^s")

Or you could try to click on the command palette...

Posted

Sven, yeah I know it's written in Rust but I'm trying to test strictly from a UI perspective.  So the fact that it's in Rust shouldn't really affect anything.  I'll try your suggestions and see if they work.  Thank you!

@9 Yeah I read that as "ctrl-k" followed by "ctrl-s" but I tried that and it only brings up the save dialog (which is bound to ctrl-s).  It only works as expected when I press ctrl-k and then keeping the ctrl key pressed press s. 

At any rate, thanks for the excellent suggestions and my apologies for that double post.  I thought I was editing my original message.  And I couldn't find an option to delete the message. So, sorry again. 

As I said if either suggestion works out I'll mark the appropriate message as the solution. Or I'll share more detail if that's appropriate! 

Again many thanks to both of you!

Oh and while I'm thinking of it--Sven I thought that a local scope is the default scope inside a function?  I want to write good code so is it simply a matter of style or does the local need to be present?  And do I need to set the SendKeyDownDelay outside of the function or can I set it immediately before the keys I want to modify and then set it back afterward?  Something like this:

Func TestSelectTheme()
    StartZed()
    Sleep(1000)
    Local $currentKeyDownDelay = Opt("SendKeyDownDelay")
    Opt("SendKeyDownDelay", 100)
    Send("{CTRLDOWN}")
    Send("k")
    Send("t")
    Send("{CTRLUP}")
    Opt("SendKeyDownDelay", $currentKeyDownDelay)
    Sleep(100)
    MsgBox($MB_ICONINFORMATION, "ZedTests", "After Key Chord")
    Local $winTitle = WinGetTitle("[ACTIVE]")
    Local $positionOfSelectThemeInTitle = StringInStr($winTitle, "Theme")
    Local $actual = $positionOfSelectThemeInTitle > 0
    Local $expected = True
    CloseZed()
    Return ($actual = $expected)
EndFunc

 

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   1 member

×
×
  • Create New...