Question

I'm learning QTP at the moment. 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.

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. i don't want to change QTP settings because it will slow down script.

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.

Thanks in advance

Was it helpful?

Solution

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. 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

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:

' 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:

' 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. 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top