4

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

Donal Fellows
  • 133,037
  • 18
  • 149
  • 215
galstar
  • 51
  • 1
  • 1
  • 3
  • Could you post the code in which you use wait so we can make an exact improvement suggestion? I don't think Sync or Exists is the best way in all situations. Sometimes it makes sense to create your own "waiting" loop which might call wait with a very small time value, so you can react on the "ready" situation real fast, but without wasting CPU time (i.e. creating heavy CPU load just for the waiting process). But it depends. So "sho uz zee codez" once again :) ! – TheBlastOne May 03 '11 at 05:47

12 Answers12

6

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

Maciej Zaleski
  • 441
  • 6
  • 7
  • 1
    +1 because answer indicates that .Sync is not a 100% solution for Ajax apps that do asynchroneous server roundtrips. – TheBlastOne Apr 07 '11 at 15:21
2

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.

Edward Leno
  • 6,257
  • 3
  • 33
  • 49
  • Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace").Click Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace").Set username 'Read User name from datasheet Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace_2").Click Browser("Home_2").Page("Home").WebEdit("ctl00$uxMNJDefaultContentPlace_2").Set Password'Read Password from datasheet Browser("Home_2").Page("Home").Link("Sign In").Click Now after sign in it talkes time to redirect the page to another page..but in mean while QTP shows error as it couldn't find the next object. – galstar Aug 19 '10 at 13:33
  • So i use wait(10) after signup operation.. but some time it takes more then 10 millisec/sec to redirect to another page...this is the issue.and if i increase it to wait(100)...then QTP wait for 100 millisec/sec for next step but sign in operation take only 11 millisec/sec. – galstar Aug 19 '10 at 13:34
  • @galstar - After the signin operation, you need something like ... Browser("Home_2").Page("New Page").WaitProperty("name", "Welcome: User 123", 120000) This will wait for up to 120 seconds for an object called 'name' on the 'New Page' to have a value of 'Welcome: User 123'. This will indicate that the login was successful. – Edward Leno Aug 19 '10 at 15:28
1

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.

Manu
  • 11
  • 1
0
  • 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"
        '================================================================================================================
0

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 

'

Sudhir
  • 1,432
  • 3
  • 15
  • 16
0

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
0

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.

user5612655
  • 200
  • 1
  • 11
0

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

Motti
  • 110,860
  • 49
  • 189
  • 262
0
'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.

xrayder
  • 71
  • 1
  • 3
0

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)
Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
0

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 .

0

'' 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
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Venu Gopi
  • 21
  • 2
  • 1
    Welcome on SO, here, it is a good practice to explain why to use your solution and not just how. That will make your answer more valuable and help further reader to have a better understanding of how you do it. I also suggest that you have a look on our FAQ : http://stackoverflow.com/faq. – ForceMagic Oct 29 '12 at 16:02