Jump to content

WebSocket WSS and HTTPS IUIAutomation Javascript

Recommended Posts

I am trying to reveal more information (to integrate in IUIAutomation and UIAWrappers) from any browser like Chrome, Firefox, Safari, IE by

1. injecting piece of javascript with websocket

2. After communication is there do communication with websocket and eval(<jscode>) to get more information from HTML page to do testautomation

Stuff works well with http websites / ws and localhost but not with https and wss most likely due to handshakestuff of websocket and certificate not there

Maybe some people can direct me further on websocket in JavaScript talking with a server in AutoIT

Steps so far

0. Reading stuff like https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers 
and other websocket stuff on google

For AutoIT I could not find a finished WebSocket Server


1. With IUIAutomation 

2. Type javascript/bookmarklet into addressbar with websocket js code

3. Start Autoit TCP listener (probably not advanced enought to deal with whole websocket protocol)

4. Type javascript (shift+alt+t) to start the websocket javascript

5. connect / sendmessage / disconnect  

Example code (try once when you are in to press shift alt t) that triggers at least te concept of connecting/disconnecting

a. run program that types stuff for you in addressbar

b. shift+alt+t will trigger the step to type javascript:testWebSocket();void(0);  in addressbar (otherwise you can do that by hand)

#include <MsgBoxConstants.au3>
#include <string.au3>

HotKeySet("+!t", "testIt") ; Shift-Alt-t

local $jsCode="javascript:var wsUri = " & """" & "wss://localhost:65534/" & """"
$jsCode = $jsCode & ";function testWebSocket() {websocket = new WebSocket(wsUri);"
$jsCode = $jsCode & "websocket.onopen = function(evt) {onOpen(evt)};"
$jsCode = $jsCode & "websocket.onclose = function(evt) {onClose(evt)};"
$jsCode = $jsCode & "websocket.onmessage = function(evt) { onMessage(evt) };"
$jsCode = $jsCode & "websocket.onerror = function(evt) { onError(evt) };}"
$jsCode = $jsCode & "function onOpen(evt) { alert(" & """" & "CONNECTED" & """" &");} "
$jsCode = $jsCode & "function onClose(evt) { alert(" & """" & "DISCONNECTED" & """" &");} "
$jsCode = $jsCode & "function onMessage(evt) { alert(" & """" & "Message" & """" &" + evt.data);} "
$jsCode = $jsCode & "function onError(evt) { alert(" & """" & "ERROR:" & """" &" + evt.data); } "
$jsCode = $jsCode & ";void(0);"

;~ $jsCode=stringreplace($jsCode,"{","\{")

;~ *** Standard code Flexible ***
#include "UIAWrappers.au3"
AutoItSetOption("MustDeclareVars", 1)

Local $oChrome=_UIA_getObjectByFindAll($UIA_oDesktop, "Title:=.*Google Chrome;controltype:=UIA_WindowControlTypeId;class:=Chrome_WidgetWin_1", $treescope_children)
Local $oAddressBar=_UIA_getObjectByFindAll($oChrome, "title:=Adres.*;ControlType:=UIA_EditControlTypeId", $treescope_subtree)

;~ _UIA_action($oAddressBar,"highlight")
_UIA_action($oAddressBar,"setvalue", $jsCode )
_UIA_action($oAddressBar,"sendkeys", "{ENTER}")

consolewrite($jsCode & @CRLF)

; I am the server, start me first! (Start in second the TCPConnect example script).
; javascript:var wsUri = "wss://";function testWebSocket() {websocket = new WebSocket(wsUri);websocket.onopen = function(evt) {onOpen(evt)};websocket.onclose = function(evt) {onClose(evt)};  websocket.onmessage = function(evt) { onMessage(evt) };websocket.onerror = function(evt) { onError(evt) };} function onOpen(evt) { alert("CONNECTED");} function onClose(evt) { alert("DISCONNECTED");} function onMessage(evt) { alert("Message" + evt.data);} function onError(evt) { alert("ERROR:" + evt.data); } ;void(0);
;~ var ws = new WebSocket('wss://localhost:15449/', {
;~   protocolVersion: 8,
;~   origin: 'https://localhost:15449',
;~   rejectUnauthorized: false
;~ });


func testIt()
    _UIA_action($oAddressBar,"setvalue","javascript:testWebSocket();void(0);" )
    _UIA_action($oAddressBar,"sendkeys", "{ENTER}")

Func Example()
    TCPStartup() ; Start the TCP service.

    ; Register OnAutoItExit to be called when the script is closed.

    ; Assign Local variables the loopback IP Address and the Port.
    Local $sIPAddress = "" ; This IP Address only works for testing on your own computer.
    Local $iPort = 65534 ; Port used for the connection.

    ; Bind to the IP Address and Port specified with a maximum of 100 pending connexions
    ;(Take a look at the example of this function for further details).
    Local $iListenSocket = TCPListen($sIPAddress, $iPort, 100)
    Local $iError = 0

    ; If an error occurred display the error code and return False.
    If @error Then
        ; Someone is probably already listening on this IP Address and Port (script already running?).
        $iError = @error
        MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not listen, Error code: " & $iError)
        Return False

    ; Assign Local variable to be used by Listening and Client sockets.
    Local $iSocket = 0
    Do ; Wait for someone to connect (Unlimited).
        ; Accept incomming connexions if present (Socket to close when finished; one socket per client).
        $iSocket = TCPAccept($iListenSocket)

        ; If an error occurred display the error code and return False.
        If @error Then
            $iError = @error
            MsgBox(BitOR($MB_SYSTEMMODAL, $MB_ICONHAND), "", "Could not accept the incoming connection, Error code: " & $iError)
            Return False
    Until $iSocket <> -1 ;if different from -1 a client is connected.

    ; Assign a Local variable the data received.
    Local $sReceived = TCPRecv($iSocket, 2048) ;we're waiting for the string "tata" OR "toto" (example script TCPRecv): 4 bytes length.

    consolewrite("Received:" & $sReceived & @CRLF)
    consolewrite("Received:" & _hextostring($sReceived))

    TCPSend($iSocket, "We could send stuff like: document.innerHTML")
    ; Close the Listening socket to allow afterward binds.

    MsgBox($MB_SYSTEMMODAL, "", "Client Connected.")

    ; Close the socket.
EndFunc   ;==>Example

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.
EndFunc   ;==>OnAutoItExit


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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...