QTP中的Wait()函数

发布于 2024-09-14 17:31:47 字数 181 浏览 10 评论 0原文

任何人都可以建议我一个可以在 QTP 中用于以下场景的函数...

因为有时页面导航需要时间,因此我们的脚本显示错误。为此,我们使用wait(time)函数,但它是QTP控件等待的固定时间。我想使用一个函数(我听说过同步函数,但不知道如何使用它),以便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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

指尖微凉心微凉 2024-09-21 17:31:47

处理这种情况的标准方法是使用 Page(或在某些情况下是 Browser)对象的 .Sync 方法。

我发现它非常不稳定,根据测试的应用程序,该功能可能会完美运行,而在其他情况下则不会等待足够长的时间。

该问题似乎主要与 Web 2.0 应用程序(基于 AJAX)有关。与服务器的网页连接通常比 java 脚本打开的异步连接更早关闭。

如果有一个视觉指南指示该页面仍在加载,您可以编写一个循环并检查该对象。一旦对象消失,您就可以恢复测试执行。

为了避免在需要同步的每个地方编写此代码,您可以使用以下代码用您自己的版本覆盖 QTP 本机方法:

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

    'Custom sync code'

End Function

谢谢,
马切伊

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

一个人的旅程 2024-09-21 17:31:47

同步功能正是您想要的。您可以通过多种方式使用它,例如:

  • 用于加载网页。
  • 要启用按钮或
    禁用。
  • 对于客户端-服务器通信
    结束。

想想 GUI 中发生了什么来指示您操作已完成。这个特定的对象和/或属性是您想要同步的对象和/或属性。

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

其他选项包括使用 WaitPropertyExist 命令将脚本与应用程序同步。

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

QTP 函数参考应该有助于使用这些函数并解释所使用的参数。如果您仍然遇到问题,请发布代码段,以便我们查看。

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.

情深已缘浅 2024-09-21 17:31:47

WaitProperty 方法是这种情况的最佳解决方案。

等待指定的对象属性达到指定的值或超过指定的超时,然后再继续下一步。

返回值: 布尔值。如果属性达到该值,则返回 TRUE;如果在属性达到该值之前达到超时,则返回 FALSE。 FALSE 返回值并不表示步骤失败。

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.

浮华 2024-09-21 17:31:47

最好的确定方法通常是对新页面中的对象使用 Exists(timeout)

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

宛菡 2024-09-21 17:31:47

'' 在您想要等待 obj 准备就绪的任何地方使用此函数

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

'' 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
知足的幸福 2024-09-21 17:31:47
  • 将此函数添加到您的库中
  • 单击后使用此函数
  • 这是一个等待函数,等待完成浏览器(IE)加载并写入完成,

例如

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

  • Browser().synchronize

这是一个很棒的函数,

享受一下

            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"
        '================================================================================================================
  • 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"
        '================================================================================================================
土豪我们做朋友吧 2024-09-21 17:31:47

我使用如下所示的东西,基本上做一个循环来检查
如果页面上的元素存在,一旦它确实存在,您就可以退出循环并继续测试。

但是,如果该元素不存在,您不想陷入无限循环,因此我有
添加了另一个条件,在放弃之前检查一定次数。

如果达到最大重试尝试次数,或者找到该元素,则退出循环。

示例代码:

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 

'

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 

'

仅此而已 2024-09-21 17:31:47

你好,我希望这会有所帮助。这里首先检查对象( webelement )是否存在,然后检查它是否被禁用,然后调用同步,因此最好使用等待。谢谢
可用于各种网页控件。

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

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
只涨不跌 2024-09-21 17:31:47

将同步添加到预期的下一个页面对象。

例如,如果下一个浏览器和页面是“第 1 页”,那么该行将是:

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

或者,如果您不确定将出现哪个页面,只需使用

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

上面的行应该检查下一个浏览器和页面(假设只打开一个浏览器)当时)。

如果您想深入检查下一个元素的 Html 属性,您可以使用任何对象的 .Object 方法并检查其 HTML 属性,例如 css 等。

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.

ˉ厌 2024-09-21 17:31:47
'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

当您确定单击某个按钮后应用程序中发生更改的对象后,例如单击“保存”后,应用程序返回一条消息,您可以将该对象放在 Exist(1) 语句中。

对象的参数“1”表示应检查元素的时间,低于该值将无法始终起作用(我是根据经验这么说的),而高于该值则不可取。

这是我经常使用的一个片段,并且效果很好,我认为它会满足您的需求。

如果对象没有 Exist 方法,请尝试 .Object.isDisabled 或 .Object。 (检查此处自动完成显示的内容)例如 Browser("Browser").Page("Page").WinElement("Message").Object.isDisabled 其中“Object”是尝试使用本机对象方法的方法。

请注意,UFT 用于自动化功能测试,需要一些时间来检查对象(一些毫秒)。
因此,对象出现,QTP 需要几毫秒或更长时间才能看到它存在或对象的属性更改了值。

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

烟雨凡馨 2024-09-21 17:31:47

您可以使用以下 4 个属性:

  1. 等待 - 您也可以在此处提供您想要的时间。
    例如 - wait 0,500
  2. Sync - 将等待页面刷新。
    例如 - browser.page.sync()browser.page.sync(30)
  3. 等待属性 - 您可以提供手动等待时间。< br>
    例如 - 等待 25 秒:
    browser.page.WaitProperty "text", "Simple Interest",25000
  4. Exist - 检查对象是否在所需的时间内存在。
    例如 - browser.page.exist(30)

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)
情绪操控生活 2024-09-21 17:31:47

这是一种常见行为,应用程序需要一些时间才能导航到其他页面,而该页面的时间不固定。如果您使用 Wait(50) ..这将等待 50 秒,因为这是硬编码的等待...
如果您的脚本花费的时间少于 50 秒(假设为 20 秒),则脚本将强制 UFT 等待 30 秒以上,这会增加执行时间。
我根据我的应用程序行为使用了下面的

步骤:

代码如下:

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

2) 将下一页对象存储在存储库中以进行验证

Browser("Browser姓名”)。 Page ("PageName").Webelement("Webelement name").waitproperty("Webbutton", "HomePage",30000)

这将给出布尔返回值。

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文