Sign in to follow this  
Followers 0
mickyk

Send problem in browser tests with no driver plugin or extension (or accessibility)

1 post in this topic

Hi All,

I'm a newbe here so please bare with me.

I'm trying to make a script that will open Chrome (If it is already open set focus to its window) copy the URL from the address-bar and paste it to a new message box.

I understood that I can use or chrome accessibility with code snippets found on this site (or use selenium driver) but I want to be able to compile the script into an exe and run it on different computers.

I wrote a script that sometimes works but sometimes it seems that the keystrokes are not sent to the browser and I cannot understand why. (The window is clearly in focus)

Here is my latest version of the script:

;The idea of the script is to find the window handle of a currently running chrome or open it and then get the handle.

;Using this handle send F6 to the chrome window to go to the address-bar use ^C to copy the url and Clipboard functions to get the copied data.

 

#include <MsgBoxConstants.au3>
#include <Constants.au3>
#include <Clipboard.au3>

InfoMsg(GetChromeAdressBarValue(0))

Func GetAppPath($appExe)
    $regPath = "HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionApp Paths" & $appExe
    $var = RegRead($regPath, "Path")
    If ($var <> "") Then
        $var = $var & "" & $appExe
    Else
        $var = RegRead($regPath, "")
    EndIf
    $var = StringReplace($var, ";", "")
    Return $var
EndFunc   ;==>GetAppPath

Func InfoMsg($msg, $timeout = 0)
    MsgBox($MB_ICONINFORMATION + $MB_SYSTEMMODAL, "Info", $msg, $timeout)
EndFunc   ;==>InfoMsg

Func ErrorMsg($msg, $timeout = 0)
    MsgBox($MB_ICONERROR + $MB_SYSTEMMODAL, "Error In AutoIt Script", $msg, $timeout)
EndFunc   ;==>ErrorMsg

Func ActivateApp($prefixName, $winActivatePath, $openAppBeforeAction)
    $appName = $prefixName & ".exe"
    $appPath = GetAppPath($appName)
    $appWindowsHandle = 0
    ;InfoMsg($appPath)
    If ($appPath = "") Then
        ErrorMsg("Application " & $prefixName & " is not installed")
    Else
        ;InfoMsg("Checking " & $prefixName & " active")
        $appWindowsHandle = WinActivate($winActivatePath)
        If ($appWindowsHandle) Then
            Sleep(500)
        Else
            If (Not $openAppBeforeAction) Then
                ErrorMsg("Application " & $prefixName & " is not active")
            Else
                ;InfoMsg("Running " & $appPath)
                If (Run($appPath)) Then
                    $appWindowsHandle = WinWaitActive($winActivatePath, "", 2)
                    Sleep(1000)
                    ;InfoMsg("Application " & $prefixName & " is active")
                EndIf
            EndIf
        EndIf
    EndIf
    Return $appWindowsHandle
EndFunc   ;==>ActivateApp

Func GetChromeAdressBarValue($openChromeBeforeAction = 0)
    $abUrl = ""
    $chromeWindowsHandle = ActivateApp("chrome", "[REGEXPCLASS:Chrome*]", 1)
    If ($chromeWindowsHandle) Then
        _ClipBoard_SetData("")
        InfoMsg("Win Handle " & $chromeWindowsHandle & " Getting URL", 1)
        Sleep(2000) ; wait for page load to end
        $chromeWindowsHandle = WinActivate($chromeWindowsHandle)
        If ($chromeWindowsHandle) Then
            Send("{ESC}{ESC}{ESC}{F6}{CTRLDOWN}c{CTRLUP}{ENTER}")
            $abUrl = _ClipBoard_GetData(1)
            If ($abUrl = "") Then
                InfoMsg("Testing Fullscreen", 1)
                Send("{ESC}{ESC}{ESC}{F11}")
                Sleep(1500)
                Send("{F6}{CTRLDOWN}c{CTRLUP}{ENTER}{ESC}{F11}")
                $abUrl = _ClipBoard_GetData(1)
            Else
                InfoMsg("Strange: '" & $abUrl & "'", 2)
            EndIf
        EndIf
    EndIf
    Return $abUrl
EndFunc   ;==>GetChromeAdressBarValue
 

Thanks in advance,

Micky

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  
Followers 0