galstar
galstar

Reputation: 51

Wait() function in QTP

Can any body suggest me a function which I can use in QTP for following scenario...

As sometimes page navigation take times due to which our script shows an error. For that we use the wait(time) function, but it is a fixed time for which the QTP control waits. I want to use a function (I have heard about the sync function, but don't know how to use it) so that QTP waits only for the time, time taken in navigation (not more/less then it).

Upvotes: 4

Views: 29632

Answers (12)

Nikita M
Nikita M

Reputation: 1

this is a common behavior that application will take some time to navigate to other page which is not fixed time.If you are using Wait(50) ..this will wait 50 sec as this is hardcoded wait... if your script is taking less than 50 sec suppose 20 sec then scripting is forcing UFT to wait for more 30 sec which increases execution time. I used below based on my application behavior

Steps:

Code is as follows :

1) browser().page.().webbutton("login").click

2) Store the next page object in the repository for verification

Browser("Browser name"). Page ("PageName").Webelement("Webelement name").waitproperty("Webbutton", "HomePage",30000)

This will give Boolean return value .

Upvotes: 0

Akhil A
Akhil A

Reputation: 1

You can use from these 4 properties:

  1. Wait - here also you can provide desired time you want.
    Ex - wait 0,500
  2. Sync - will wait till page is refreshed.
    Ex - browser.page.sync() or browser.page.sync(30)
  3. Wait property - You can provide manual wait time.
    Ex - wait for 25 seconds:
    browser.page.WaitProperty "text", "Simple Interest",25000
  4. Exist - Checks if the object is present or not for the desired time.
    Ex - browser.page.exist(30)

Upvotes: 0

xrayder
xrayder

Reputation: 71

'Set here the timeout so that the code doesn't stuck here (infinite loop)
var_timeout = 60 

x=0
do
  x=x+1
loop until Browser("Browser").Page("Page").WinElement("Message").Exist(1) or x>var_timeout

After you identify the object that changes in your application after you click a certain button, as in after clicking Save, the application return a message, you can put the object with Exist(1) statement.

The Parameter "1" of the object represents time in which the element should be checked, lower than that will not work always (I say this from experience) and higher is not desirable.

This is a snippet I use very often and I have good results, and I think it will suit your needs.

If the object does not have the Exist method please try .Object.isDisabled or .Object. (check what autocomplete shows here) for example Browser("Browser").Page("Page").WinElement("Message").Object.isDisabled where "Object" is a method that tries to use native object methods.

Please note that UFT is used for automated functional testing and will take some time to check the object (some miliseconds). So the object appears and it takes some miliseconds or more for QTP to see that it exists or that a property of the object changed value.

Upvotes: 0

user5612655
user5612655

Reputation: 190

Add sync to the next Page Object expected.

For Example if the next Browser and Page are "Page 1", so line will be:

Browser("Page 1").Page("Page 1").sync

Or if you re not sure which Page will come just use

Browser("title:=.*").Page("title:=.*").sync

Above line should check any browser and page which comes next(assuming there is only one browser open at that time).

If you want to go deep and check next elements Html Properties you can use .Object method of any object and check its HTML properties like css etc.

Upvotes: 0

Viraj John Maria
Viraj John Maria

Reputation: 87

Hi i hope this may help. Here first check the object ( webelement ) is present and then checks is it disabled and then it calls a sync so it will be better to use for wait. Thank you can be used for all kinds of web controls.

Set NavigationTab = Browser ().Page().WebElement()
PerformWait ( 10 , 10 , NavigationTab )


Function PerformWait ( intDisableTime , intDelay , object )

if CheckExist ( intDelay , object ) Then

    if ValidateDisabled ( object , intDisableTime ) Then

        object.Sync
        Reporter.ReportEvent 0 , "Element is ready to use" , "The  specified element is ready to use" & Date & Time


    Else

       Reporter.ReportEvent 3 , "Object Disabled." , "Object remains disabled after specified time : " & refDisableTime & Date & Time   

    End If

Else

    Reporter.ReportEvent 3 , "Element not present." , "The specified element not present : " & Date & Time

End If

End Function

Function CheckExist ( intDelay , object )

object.RefreshObject

' -- validating the object is exist or not.
If object.Exist ( intDelay ) Then

    CheckExist = True

Else

    CheckExist = False

End If

End Function


Function ValidateDisabled ( object , intDisableTime )


For Iterator = 1 To intDisableTime Step 1

    ' -- validating the object is disabled or not.
    If object.GetROProperty ( "disabled" ) = 1 Then

        wait 1  
        ValidateDisabled = False

    Else

        ValidateDisabled = True
        Exit For    

    End If

    Iterator = Iterator + 1
Next

End Function

Upvotes: 0

Sudhir
Sudhir

Reputation: 1472

I use something as per below, basically do a loop which checks to see if an element on the page exists, once it does exist you can exit the loop and continue with your test.

However, if the element does not exist, you do not want to be caught up in an endless loop, therefore I have added another condition which checks a certain number of times before giving up.

If either the maximum number of retry attempts are reached, or if the element is found, then the loop is exited.

Example code:

Dim blnPageElementReady, intmaxRetryAttempts, x

blnPageElementReady = False
intMaxRetryAttempts = 10
x = 0

Do Until (blnPageElementReady = True OR intMaxRetryAttempts = x) 

    If Browser("b").Page("p").WebElement("SomeValue").Exist Then
        blnPageElementReady = True
    End If
    x = x + 1

Loop

If intMaxRetryAttempts = x Then
'   Exit Test code goes here.....
End If

' continue with test on page:

Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace").Click 
Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace").Set username 
Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace_2").Click 

'

Upvotes: 0

Firas Al-Barghouthi
Firas Al-Barghouthi

Reputation: 21

  • add this function to your library
  • use this function after you your click
  • it's a wait function wait to finish browser(IE) loading and write done

for example

  • Browser().page().link().click()

  • Browser().synchronize

it's a great function

enjoy

            Public Sub Synchronize (ByVal objObject)

                '------------------------------------------------------             
                    Dim Counter
                    Counter = 0
                '------------------------------------------------------     

                    If objObject.Exist(1) Then
                        '------------------------------------------------------     
                            ' Clear Error if any ...
                            Err.Clear
                            On Error Resume Next

                            ' Browser sync ...
                            objObject.Sync

                        '------------------------------------------------------             
                            If Err.Number <> 0 Then
                                While (Err.Number <> 0 and Counter <= 100)
                                    Err.Clear
                                    On Error Resume Next
                                    objObject.Sync
                                    Counter = Counter + 1
                                wend
                            End If
                        '------------------------------------------------------     
                    Else
                        'QTPGenerateReporter "Check Browser - Synchronize Function", "Should Exist", "Does not Exist", "Fail", "-"
                    End If

            End Sub

            RegisterUserFunc "Browser", "Synchronize", "Synchronize"
            RegisterUserFunc "Page", "Synchronize", "Synchronize"
        '================================================================================================================

Upvotes: 0

Venu Gopi
Venu Gopi

Reputation: 21

'' Use this function where ever you want to wait until the obj is ready

Public Function WaitforObject(ByRef objRef, intWaitSecs)

  WaitforObject= False

  blnFlag= False
  intCount=1
  If ISObject(objRef) Then
    For iWaitSecs = 1 to intWaitSecs
        blnFlag = objRef.Exist(1)
        If blnFlag Then                         
            Do While (LCase(Trim(objRef.getROProperty("attribute/readyState")))<>"complete") or (Trim(objRef.getROProperty("attribute/readyState"))<>"4" )
                If intCount= intWaitSecs Then
                    WaitforObject = True
                    Exit Do
                End If
                intCount=intCount+1
            Loop
            Exit For
        End If           
    Next
     If Not blnFlag Then
            wait 2
            WaitforObject = True
    End If

  End If  

End Function

Upvotes: 0

Manu
Manu

Reputation: 11

WaitProperty Method is best solution for this scenario.

Waits until the specified object property achieves the specified value or exceeds the specified timeout before continuing to the next step.

Return Value: A Boolean value. Returns TRUE if the property achieves the value, and FALSE if the timeout is reached before the property achieves the value. A FALSE return value does not indicate a failed step.

Upvotes: 1

Motti
Motti

Reputation: 114825

The best way to be sure is often to use Exists(timeout) on an object from the new page.

Upvotes: 0

Maciej Zaleski
Maciej Zaleski

Reputation: 431

The standard way of dealing with this kind of scenarios is to use .Sync method of the Page (or in some cases Browser) objects.

I found it very temperamental and depending on the tested application this function might work perfectly and on the other occasion will not wait long enough.

The problem seems to be related mainly to Web 2.0 applications (AJAX based). Web page connection to the server is usually closed much earlier then asynchronous connection opened by java script.

If there is a visual guide indicating that that page is still loading you could write a loop and check for that object. Once the object disappear you could resume test execution.

To save yourself writing this code in every place where you need to sync you could overwrite QTP native method with your own version with following code:

RegisterUserFunc "Page", "Sync", "SyncToPage"
Function SyncToPage(oPage)
    'Call native function first'
    oPage.Sync

    'Custom sync code'

End Function

Thanks, Maciej

Upvotes: 6

Edward Leno
Edward Leno

Reputation: 6327

The sync functionality is what you want. You can use it in a variety of ways, such as:

  • For a web page to load.
  • For a button to become enabled or disabled.
  • For client-server communications to finish.

Think about what is happening in the GUI to indicate to you that the operation has completed. This particular object and/or property is what you want to sync on.

http://qtp.blogspot.com/2007/09/qtp-sync-wait-and-synchronization.html

Other options include using the WaitProperty and Exist commands to synchronize the script with the application.

http://www.sqaforums.com/showflat.php?Number=555273

The QTP function reference should help with using these functions and explaining the parameters used. If you are still having issues, post a code segment so we can take a look.

Upvotes: 2

Related Questions