如何等待对象和页面加载



我写了这个函数,它在单击任何链接或按钮后被调用。

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

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

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

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

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

你的代码根本不是动态的。此外,理想的等待方法是等待元素在页面中加载(换句话说,可见(,这将确保浏览器成功加载。在您的情况下,每当您调用函数 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

最新更新