如何等待对象和页面加载

时间:2018-06-19 17:07:01

标签: hp-uft

我编写了此函数,单击任何链接或按钮后即会调用该函数。

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

在大多数情况下都可以正常工作。但是,我遇到两个问题:

如果在UFT调用该函数之前已经加载了浏览器,我已经看到UFT仍在等待页面加载。相反,它不应该等待并继续下一步。

如果UFT调用了该函数但未打开浏览器,则UFT仍将等待浏览器打开并加载。相反,它不应该等待并继续下一步。

如何编辑我的函数来解决以上两个问题?

1 个答案:

答案 0 :(得分:0)

您的代码根本不是动态的。同样理想的等待方式是等待元素加载到页面中(换句话说,可见),以确保浏览器成功加载。在您的情况下,任何时候调用函数BrowerSync都会等待60秒找到该对象,然后它将退出函数。 我建议您等待页面中的某个元素,并使用time作为该方法的参数使其动态,以便根据您的浏览器,有时可以等待60秒,有时可以等待10秒。下面是我用来等待网络元素的函数

    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