Wednesday, May 30, 2012

QTP sync on cursor


in the world of SOA, it may take a while for data to get back from the server so the client can display it. In the mean while,  the client application maybe hanging waiting for data with special type of cursor. We want QTP to wait for this waiting-cursor to going away before we continue to the next instruction, so below is the code for doing this.

you may want to enhance this to make it wait for a certain amount of time (or after a certain number of try) then throwing error if the waiting-cursor still exist because you don't want to wait forever for the waiting-cursor to go away.


extern.Declare micLong,"GetForegroundWindow","user32.dll","GetForegroundWindow"
extern.Declare micLong,"AttachThreadInput","user32.dll","AttachThreadInput", micLong, micLong,micLong
extern.Declare micLong,"GetWindowThreadProcessId","user32.dll","GetWindowThreadProcessId", micLong, micLong
extern.Declare micLong,"GetCurrentThreadId","kernel32.dll","GetCurrentThreadId"
extern.Declare micLong,"GetCursor","user32.dll","GetCursor"


Public Function get_Cursor(hWnd)
            pid = extern.GetWindowThreadProcessId(hWnd, NULL)
            thread_id=extern.GetCurrentThreadId()
            extern.AttachThreadInput pid,thread_id,True
            get_Cursor=extern.GetCursor()
            extern.AttachThreadInput pid,thread_id,False
        End Function


Function SynOnCursor
        Set currentBrowser = GUI_MAP.Item("Browser")
        cursor = Util.get_Cursor(currentBrowser.Object.hWnd)
        'FREE_CURSOR_IDS = new String[]{/*hand*/"65581", /*arrow*/"65553", /*edit*/"65555", "65557"};
       
        if(cursor = 65581 or cursor = 65553 or cursor = 65555 or cursor = 65557) then
                Component.logInfo("SynOnCursor[" & cursor & "] done !")
        else
                Component.logInfo("SynOnCursor[" & cursor & "]")
                wait(1)
                SynOnCursor
        end if
End Function