简体   繁体   English

QTP可重复使用的同步功能

[英]qtp reuseable sync function

I'm learning QTP at the moment. 我正在学习QTP。 One problem i came across is webpage sync issue. 我遇到的一个问题是网页同步问题。 I do know how to use wait() sataement as well as Browser("Google").Page("Google").Sync. 我确实知道如何使用wait()序列以及Browser(“ Google”)。Page(“ Google”)。Sync。

But there has to be a better way to sync with the page. 但是必须有更好的方法来与页面同步。 I want QTP to wait at the same time i want script to go on as soon as the object is found. 我希望QTP同时等待,我希望脚本在找到对象后立即继续。 i don't want to change QTP settings because it will slow down script. 我不想更改QTP设置,因为它会减慢脚本速度。

Can you guys give me an example function preferably using for loop so that i can call the function every time it needs to verify checkpoint. 你们能给我一个示例函数,最好使用for循环,这样我每次需要验证检查点时就可以调用该函数。

Thanks in advance 提前致谢

result = Browser("Google").Page("Google").Exist(20)

or 要么

result = Browser("Google").Page("Google").WebElement("xyz").Exist(20)

This waits 20 seconds until the page, or in the second case an webelement exists. 这将等待20秒钟,直到该页面或第二种情况下存在一个Web元素。 The script will continue as soon as the object is found or the timeout is passed. 一旦找到对象或超时,脚本将继续。

Result will contain true or false depending if the object exists 结果将包含truefalse具体取决于对象是否存在

Please note that the object synchronization timeout you can find in your Test Settings is added to the .Exist(seconds) timeout except when you use .Exist() without a parameter like: 请注意,您可以在“测试设置”中找到的对象同步超时将添加到.Exist(seconds)超时中,除非您使用不带以下参数的.Exist()

' Quick check-and-continue to see if an object does not exist:

' We expect the page to be existing, wait for it at least 10 second:
if Browser("Google").Page("Google").Exist(20) then
    ' Do a quick check that the warning div does not exist, note the parameter
    ' less usage of Exist()
    if Browser("Google").Page("Google").WebElement("html id:=warningContainer").Exist() Then
        MsgBox "There was a warning on the page!"
    else
        MsgBox "Everything is fine!"
    end if
else
    MsgBox "The page did not exist!"
end if

EDIT: 编辑:

You can use Exist in a loop: 您可以循环使用Exist

' Never ending loop until found:
Do Until Browser("Google").Page("Google").WebElement("xyz").Exist()
    wait 1
Loop

' Or a loop with a timeout
timeout = 20
Do until (timeout = 0 OR Browser("Google").Page("Google").WebElement("xyz").Exist()
    wait 1
    timeout = timeout - 1
Next

I've implemented the wait 1 on purpose. 我已经故意实现了wait 1 You can do it without, but in my experience that can create unwanted random side effect like a browser page that never gets loaded causing your test to fail. 您可以不这样做,但是以我的经验,它可能会产生不必要的随机副作用,例如永远不会加载的浏览器页面导致测试失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM