bugfinder
bugfinder

Reputation: 203

How to wait for object and page load

I wrote this function which is being called after clicking any links or buttons.

Function BrowerSync
         If Browser("micclass:=Browser").Page("micclass:=Page").Exist(60) then
            BrowerSync = 1
         End  if
End Function

It works fine most of the cases. However, I am experiencing two issues:

If the browser is already loaded before UFT calls the function, I have seen that UFT is still waiting for the page to be loaded. Instead, it should not wait and move on to the next step.

If UFT calls the function but the browser is not opened, UFT still waits for the browser to open and load. Instead it should not wait and move on to the next step.

How can I edit my function to fix the above two issues?

Upvotes: 0

Views: 2384

Answers (1)

mbn217
mbn217

Reputation: 914

Your code is not dynamic at all. Also the ideal way to wait is to wait for an element to load in the page (in other word to be visible) that will make sure that the browser loaded successfully. In your case anytime you call the function BrowerSync it waits for to find that object for 60 seconds and then it will exit the function. I will suggest you that you wait for an element in the page an make it dynamic by using the time as parameter for the method so you can wait sometimes 60 seconds and sometimes 10seconds , based on your browser. Below is my function that i use to wait for a webelement

    PageLoad_Performance = 10   'this will be used in DWaitForWebElement Function , set the time in seconds, if time less than time added,element found and pages loaded
    '++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'Function Name: WaitForWebElement
'Description: Its a dynamic Conditional Wait, It will wait for 
'               Webelement until It Exists
'Arguments: classVal - The class property Value of the Object
'           innertextVal - The innertext Value of the object 
'Created By: 
'Date: 10/25/2016
'Usage: Call WaitForWebElement (classVal,innertextVal)

Function WaitForWebElement(classVal,innertextVal)

    Dim Total_Time,oDesc

    Total_Time=1
    'create description of the object
    Set oDesc = Description.Create
    oDesc.Add "MicClass","WebElement"
    oDesc.Add "class",classVal
    oDesc.Add "innertext",innertextVal

    With Browser("title:=yourtitleofthepage.*").Page("title:=yourtitleofthepage.*")
        'this while loop , it will wait until the object exist , it will never exit the loop unless object is Found
        While .WebElement(oDesc).Exist(1) = False
            wait 1 ' we loop and check and then wait one second
            'every time we loop we increment total time by 1 second , to check at the end to total time to load that page
            Total_Time = Total_Time+1
        Wend    

        If Total_Time < PageLoad_Performance Then 'if the page loads in less than 10 seconds Performance Report will Pass
            'do your report             
        Else
            'do your report
        End If

    End With
    'clean up
    Set oDesc = nothing

End Function

Upvotes: 0

Related Questions