QTP|UFT之同步等待

在QTP|UFT自动化测试Web网站时,经常遇到这样一种情况,当点击了一个链接或按钮之后,会跳转到一个新的页面或者页面需要加载一些新的控件,而加载这些元素需要一定的时间,但QTP脚本已经执行到下一行,在目标元素还没有被加载显示出来的时候就已经开始寻找下一步需要操作的对象,最终的结果当时找不到对象而报错。因此,我们需要在这些步骤之间加一些操作等待元素加载,也就是我们常说的同步等待。QTP|UFT中可以用那些方式同步呢?总结如下:

1. Wait - 万能钥匙

Wait 5

2. Exit - 在一步操作之前检查,下一步对象是否存在?

If Browser("micClass:=Browser").Page("micClass:=Page").WebTable("***").Exist(5) Then
    'Next Step
End If 

3. Sync - Web中最常用的同步方法,但只适用于Browser对象和Page对象,作用是自动等待页面完成加载

Browser("micClass:=Browser").Sync  
Browser("micClass:=Browser").Page("micClass:=Page").Sync  

4. WaitProperty(PropertyName, PropertyValue, [TimeOut])

 作用:在TimeOut的时间内等待对象的属性值,如果在规定时间内出现了属性值则执行下一步,否则继续等直到超时。

If Browser("micClass:=Browser").Page("micClass:=Page").WebElement("***").WaitProperty("Text", "Ready", 50) Then
    'Next Step
End If 

5. IE之Busy

 1 '#################################################################################################
 2 '#################################################################################################
 3 'Function Name: Func_IESync
 4 'Description: Use this function to wait page elements loading
 5 'Parameters: intSyncSeconds: wait seconds
 6 'Assumptions: IE has been opened
 7 'Return: N/A
 8 'Example: Func_IESync 60
 9 '#################################################################################################
10 'Modified History:  Initial release  12/12/2014   Calvin                                                   
11 '#################################################################################################
12 Function Func_IESync(intSyncSeconds)
13     'Define wait time
14     Dim intWaitTime: intWaitTime = 0
15     'Invoking current IE
16     Set objIE = GetObject("", "InternetExplorer.Application")
17     'Sync and wait
18     Do While objIE.Busy
19         If intSyncSeconds > intWaitTime Then
20             Wait 5
21             intWaitTime = intWaitTime + 5
22         Else
23             Reporter.ReportEvent micFail, "Sync Waiting "&intSyncSeconds&" Seconds", "Synchronization timeouts."
24             Exit Do
25         End If
26     Loop
27     'Release object
28     Set objIE = Nothing
29 End Function
View Code

6. 下一步元素某个属性

 1 '#################################################################################################
 2 '#################################################################################################
 3 'Function Name: Func_PageElementSync
 4 'Description: Use this function to wait web element loading
 5 'Parameters: objElement: a web object
 6 '             intSyncSeconds: wait seconds
 7 'Assumptions: N/A
 8 'Return: N/A
 9 'Example: Func_PageElementSync Browser("HPFS-PC").Page("PartnerDetailsLookup").WebButton("Find"), 30
10 '#################################################################################################
11 'Modified History:  Initial release  12/12/2014   Calvin                                                   
12 '#################################################################################################
13 Function Func_PageElementSync(objElement, intSyncSeconds)
14     'Define wait time
15     Dim intWaitTime: intWaitTime = 0
16     'Sync and wait
17     Do Until objElement.Object.readyState = "complete"
18         If intSyncSeconds > intWaitTime Then
19             Wait 5
20             intWaitTime = intWaitTime + 5
21         Else
22             Reporter.ReportEvent micFail, "Sync Waiting "&intSyncSeconds&" Seconds", "Synchronization timeouts on element: "& objElement.ToString()
23             Exit Do
24         End If
25     Loop
26 End Function
View Code

7. 进度条同步

 1 '#################################################################################################
 2 '#################################################################################################
 3 'Function Name: Func_ProcessHandling
 4 'Description: Use this function to sync processing or loading
 5 'Parameters: intSyncSeconds: wait seconds
 6 'Assumptions: N/A
 7 'Return: N/A
 8 'Example: Func_ProcessHandling 60
 9 '#################################################################################################
10 'Modified History:  Initial release  12/16/2014   Calvin                                                   
11 '#################################################################################################
12 Function Func_ProcessHandling(intSyncSeconds)
13     'Define wait time
14     Dim desSync, objLoading, strLoadingVisible
15     Dim intWaitTime: intWaitTime = 0
16     'Describe loading or Processing object
17     Set desSync = Description.Create()
18     desSync("MicClass").Value = "WebElement"
19     desSync("html tag").Value = "DIV"
20     desSync("class").Value = "loading ui-state-default ui-state-active.*"
21     desSync("html id").Value = ".*(load|Load|Processing|processing|Process|process).*"
22     '5 seconds to show process object
23     For Iterator = 0 To 10 Step 1
24         'Set process object
25         Set objLoading = Browser("micClass:=Browser").Page("micClass:=Page").ChildObjects(desSync)
26         'Check loading object exist
27         If objLoading.Count > 0 Then
28             Reporter.ReportEvent micDone, "Check Sync Object Exist", "Sync object exist."
29             Exit For
30         Else
31             Wait 1        
32         End If
33         '5 seconds can not show process object
34         If Iterator = 10 Then
35             Reporter.ReportEvent micWarning, "Check Sync Object Exist", "There is no sync object."
36             Exit Function
37         End If
38     Next
39     'Execute Synchronization
40     Do While true
41         'Initialize visible
42         strLoadingVisible = ""
43         'Get all loading object visible property
44         For intIterator = 0 To objLoading.Count - 1 Step 1
45             strLoadingVisible = strLoadingVisible & objLoading.Item(intIterator).GetROProperty("visible")
46         Next
47         'Whether a loaded object is visible, if not, exit waiting
48         If Instr(Lcase(strLoadingVisible), "true") = 0 Then
49             reporter.ReportEvent micDone, "loading icon", "loading icon is not exist"
50             Exit Do
51         ElseIf intSyncSeconds > intWaitTime Then
52             Wait 2
53             intWaitTime = intWaitTime + 2
54             reporter.ReportEvent micDone, "Wait time", intWaitTime
55         Else
56             Reporter.ReportEvent micFail, "Sync Waiting "&intSyncSeconds&" Seconds", "Synchronization timeouts for loading data."
57             Exit Do
58         End If
59     Loop
60     'Release object
61     Set desSync = Nothing
62     Set objLoading = Nothing
63 End Function
View Code

 

未完待续。。。


 

posted @ 2015-07-21 18:28  狙击不倒翁  阅读(1286)  评论(0)    收藏  举报