发布新日志

  • 用QTP来操作DOS命令行

    2009-03-18 14:18:23

    Objectives

    The objectives of this article are :

    • Learn how to use DOS command lines in QTP.
    • Alternative complex tasks.
    • Alternative .NET methods.

    The following content is viewable because you are logged-in.
    Thanks for registering!

    cmd.exe allows access to the Microsoft Windows Command Prompt, also known as Microsoft DOS.
    To-date, cmd.exe is a 32-bit command prompt used in Windows NT, 2000, and XP and offers disk and file maintenance functions to your computer as well as network functions. cmd.exe can be found under System32 folder.

    SystemUtil.Run "cmd.exe","","C:\WINDOWS\system32″,"open"

    image

    Capture the window by QTP

    image

    image

    Window( "object class:=ConsoleWindowClass" )

    Print Window( "object class:=ConsoleWindowClass" ).GetROProperty( "title" )

    Typing to DOS window

    The Type method types the specified string in the window.

    Syntax: object.Type KeyboardInput.

    KeyboardInput : A String value. The text string and/or constants representing non-alphanumeric keys.

    imageimage

    Window( "object class:=ConsoleWindowClass" ).Type "Ping advancedqtp.com"

    Window( "object class:=ConsoleWindowClass" ).Type micReturn

    image

    Catching the Response

    The GetVisibleText method returns the text from the specified area. The text to capture must be visible in the application window when the step runs. The area is defined by pairs of coordinates that designate two diagonally opposite corners of a rectangle.

    Syntax : object.GetVisibleText ([Left], [Top], [Right], [Bottom])

    txt = Window( "object class:=ConsoleWindowClass" ).GetVisibleText()

    print txt

    image

    Problems…

    SystemUtil.Run "cmd.exe","","C:\WINDOWS\system32″,"open"

    Window( "object class:=ConsoleWindowClass" ).Type "dir"

    Window( "object class:=ConsoleWindowClass" ).Type micReturn

    txt = Window( "object class:=ConsoleWindowClass" ).GetVisibleText()

    print txt

    The GetVisibleText method "catches" only the text that is visible. large commands with large responses are useless…

    Solution

    Wscript.Shell Object

    The WSript.Shell object provides functions to read system information and environment variables, work with the registry and manage shortcuts. You create a WSript.Shell object whenever you want to run a program locally, manipulate the contents of the registry, create a shortcut, or access a system folder.

    Dim wShell

    Set wShell = CreateObject( "WScript.Shell" )

    image

    you will see this detailed description if you are using PDM.DLL version 9

    Exec Method

    Runs an application in a child command-shell, providing access to the StdIn/StdOut/StdErr streams.

    Syntax : object.Exec( strCommand )

    strCommand : String value indicating the command line used to run the script. The command line should appear exactly as it would if you typed it at the command prompt.

    The Exec method returns a WshScriptExec object, which provides status and error information about a script. run with Exec along with access to the StdIn, StdOut, and StdErr channels.

    %compsec%

    What if your colleague installed MS-Windows on D:\ Drive?  –> The C:\Windows\System32\cmd.exe will not work.

    Since every machine can have different installation directory, the environment %compsec% returns the full path of the cmd.exe file. image

    Executing a command

    • /C – Carries out the command specified by string and then terminates.
    • /K – Carries out the command specified by string but remains.

    image

    Executing the command

    Dim wShell, exec

    Set wShell = CreateObject( "WScript.Shell" )

    Set exec = wShell.Exec( "%comspec% /C ping advancedqtp.com" )

    WshScriptExec Object

    Provides status information about a script. run with Exec along with access to the StdIn, StdOut, and StdErr streams.

    WshScriptExec.Status Property

    Provides status information about a script. run with the Exec() method.

    • WshRunning ( = 0 ) - The job is still running.
    • WshFinished ( = 1 ) - The job has completed.

    WshScriptExec.ExitCode Property

    Returns the exit code set by a script. or program run using the Exec() method. Executables set an exit code when they finish running. This conveys the status information when a process ends. Often, it is used to send an error code (or some other piece of information) back to the caller. If the process has not finished, the ExitCode property returns 0. The values returned from ExitCode depend on the application that was called.

    WshScriptExec.StdOut Property

    The StdOut property contains a read-only copy of any information the script. may have sent to the standard output.

    • AtEndOfStream - Returns a Boolean value indicating whether the end of an input stream has been reached.
    • ReadAll - Returns all characters from a stream.
    • ReadLine - Reads an entire line from a stream.

    Handling Errors

    Run the following code. why is the result empty?

    Dim wShell, exec

    Set wShell = CreateObject( "WScript.Shell" )

    Set exec = wShell.Exec( "%comspec% /C png advancedqtp.com" )

    print exec.StdOut.ReadAll

    WshScriptExec.StdErr Property

    since the "png" command has a syntax error ( ping ), the STDOUT remains empty.

    Now, run the following code :

    Dim wShell, exec

    Set wShell = CreateObject( "WScript.Shell" )

    Set exec = wShell.Exec( "%comspec% /C png advancedqtp.com" )

    print exec.StdErr.ReadAll

    image

    Since we have a syntax error, the STDERR buffer is filled, instead of the STDOUT. but, what about a "bad" command ( without syntax errors )?

    Dim wShell, exec

    Set wShell = CreateObject( "WScript.Shell" )

    Set exec = wShell.Exec( "%comspec% /C ping no-exists" )

    print exec.StdOut.ReadAll

    in this case STDOUT will be filled, but not STDERR.

    image

    How could we know when an error occurred?

    Dim wShell, exec, outStr

    Set wShell = CreateObject( "WScript.Shell" )

    Set exec = wShell.Exec( "%comspec% /C ping arrdvancedqtp.com" )

    Do While exec.Status = 0

        If Not exec.StdOut.AtEndOfStream Then

            outStr = exec.StdOut.ReadAll

            If exec.ExitCode = 1 then

                Reporter.ReportEvent micWarning, "Command failed", outStr

            End If

          Exit Do

       End If

       If Not exec.StdErr.AtEndOfStream Then

          outStr = "STDERR: " & exec.StdErr.ReadAll

          Reporter.ReportEvent micFail, "Command failed", outStr

          Exit Do

       End If

       Wait 1

    Loop

    Print outStr

    Alternative .NET

    Set nt = DotNetFactory.CreateInstance( "Microsoft.VisualBasic.Devices.Network" )

    If CBool( nt.Ping( "www.advancedqtp.com" ) ) Then

       MsgBox( "Server pinged successfully." )

    Else

       MsgBox( "Ping request timed out." )

    查看(2535) 评论(2) 收藏 分享 管理

  • QTP正则表达式

    2008-09-26 17:41:11

    QTP正则表达式

     

    字符

    描述

    \

    将下一个字符标记为特殊字符或字面值。例如"n"与字符"n"匹配。"\n"与换行符匹配。序列"\\""\"匹配,"\(""("匹配。

    ^

    匹配输入的开始位置。

    $

    匹配输入的结尾。

    *

    匹配前一个字符零次或几次。例如,"zo*"可以匹配"z""zoo"

    +

    匹配前一个字符一次或多次。例如,"zo+"可以匹配"zoo",但不匹配"z"

    ?

    匹配前一个字符零次或一次。例如,"a?ve?"可以匹配"never"中的"ve"

    .

    匹配换行符以外的任何字符。

    (pattern)

    与模式匹配并记住匹配。匹配的子字符串可以从作为结果的 Matches 集合中使用 Item [0]...[n]取得。如果要匹配括号字符( ),可使用"\(" "\)"

    x|y

    匹配 x y。例如 "z|wood" 可匹配 "z" "wood""(z|w)oo" 匹配 "zoo" "wood"

    {n}

    n 为非负的整数。匹配恰好n次。例如,"o{2}" 不能与 "Bob 中的 "o" 匹配,但是可以与"foooood"中的前两个o匹配。

    {n,}

    n 为非负的整数。匹配至少n次。例如,"o{2,}"不匹配"Bob"中的"o",但是匹配"foooood"中所有的o"o{1,}"等价于"o+""o{0,}"等价于"o*"

    {n,m}

    m n 为非负的整数。匹配至少 n 次,至多 m 次。例如,"o{1,3}" 匹配 "fooooood"中前三个o"o{0,1}"等价于"o?"

    [xyz]

    一个字符集。与括号中字符的其中之一匹配。例如,"[abc]" 匹配"plain"中的"a"

    [^xyz]

    一个否定的字符集。匹配不在此括号中的任何字符。例如,"[^abc]" 可以匹配"plain"中的"p".

    [a-z]

    表示某个范围内的字符。与指定区间内的任何字符匹配。例如,"[a-z]"匹配"a""z"之间的任何一个小写字母字符。

    [^m-z]

    否定的字符区间。与不在指定区间内的字符匹配。例如,"[m-z]"与不在"m""z"之间的任何字符匹配。

    \b

    与单词的边界匹配,即单词与空格之间的位置。例如,"er\b" "never"中的"er"匹配,但是不匹配"verb"中的"er"

    \B

    与非单词边界匹配。"ea*r\B""never early"中的"ear"匹配。

    \d

    与一个数字字符匹配。等价于[0-9]

    \D

    与非数字的字符匹配。等价于[^0-9]

    \f

    与分页符匹配。

    \n

    与换行符字符匹配。

    \r

    与回车字符匹配。

    \s

    与任何白字符匹配,包括空格、制表符、分页符等。等价于"[ \f\n\r\t\v]"

    \S

    与任何非空白的字符匹配。等价于"[^ \f\n\r\t\v]"

    \t

    与制表符匹配。

    \v

    与垂直制表符匹配。

    \w

    与任何单词字符匹配,包括下划线。等价于"[A-Za-z0-9_]"

    \W

    与任何非单词字符匹配。等价于"[^A-Za-z0-9_]"

    \num

    匹配 num个,其中 num 为一个正整数。引用回到记住的匹配。例如,"(.)\1"匹配两个连续的相同的字符。

    \n

    匹配 n,其中n 是一个八进制换码值。八进制换码值必须是 1, 2 3 个数字长。例如,"\11" "\011" 都与一个制表符匹配。"\0011"等价于"\001" "1"。八进制换码值不得超过 256。否则,只有前两个字符被视为表达式的一部分。允许在正则表达式中使用ASCII码。

    \xn

    匹配n,其中n是一个十六进制的换码值。十六进制换码值必须恰好为两个数字长。例如,"\x41"匹配"A""\x041"等价于"\x04" "1"。允许在正则表达式中使用 ASCII 码。

     

     

     

    说明

    把下面代码拷贝到QTP中运行(替换“正则表达式”和“原字符串”):

     

    Function RegExpTest(patm,strng)

       Dim regEx,retVal

       Set regEX = New RegExp

       regEx.Pattern=patm

       regEx.IgnoreCase=False

       retVal = regEx.Test(strng)

       If retVal Then

           RegExpTest = "匹配成功."

           Else

           RegExpTest = "匹配失败."

       End If

    End Function

     

    MsgBox(RegExpTest("正则表达式","原字符串"))

     

     

     

     

     

     

     

     

    了解和使用正则表达式
    通过正则表达式QuickTest可以使用不同的值来标识对象和文本字符串。您可以在以下操作中使用正则表达式
    •       
    在对话框或编程描述中定义对象的属性值
    •       
    参数化步骤
    •       
    使用不同的值来创建检查点
    例如,如果要创建日期文本字符串的文本检查点,可以使用正则表达式,但显示的日期随当前日期变化。如果将日期定义为正则表达式,则检查点检查捕获的文本字符串是否与期望的日期格式匹配,而不是检查准确的日期值。
    正则表达式是指定复杂搜索短语的字符串。通过使用特殊字符,例如句点 (.)、星号 (*)、插字号 (^) 和方括号 ([ ]),您可以定义搜索条件。
    注意:
    您可以只将正则表达式应用于字符串类型的值。
    正则表达式的任何特殊字符前面带有反斜杠 (\) 时,QuickTestVuser 将搜索文字字符。

    定义正则表达式
    可以在编程描述中定义常量值、数据表参数值、环境参数值或属性值的正则表达式。有关定义属性值的详细信息,请参阅配置常量和参数值。
    常量值选项对话框或参数选项对话框的框中输入字符串的正则表达式语法,可以定义正则表达式。选中正则表达式复选框,以指示 QuickTest 将该值作为正则表达式处理。
    所有编程描述属性值都均自动作为正则表达式处理。有关编程描述的详细信息,请参阅使用编程描述。
    注意: 您可以只将正则表达式应用于字符串类型的值。
    默认情况下,除了句点 (.)、连字符 (-)、星号 (*)、插字号 (^)、方括号 ([ ])、圆括号 (())、货币符号 ($)、垂直线 (|)、加号 (+)、问号 (?) 和反斜杠 (\) 以外,QuickTest 正则表达式中的所有字符作为文字处理。当一个特殊字符前面带有反斜杠 (\) 时,QuickTest 将其作为文字字符处理。
    如果在常量值选项参数选项对话框的框中输入一个特殊字符,QuickTest 会询问您是否要在每个特殊字符前面添加一个反斜杠 (\)。如果单击,则相应的特殊字符前面就会加上一个反斜杠 (\),以指示 QuickTest 将该字符作为文字处理。如果单击QuickTest 将该特殊字符作为正则表达式字符处理。
    本节描述某些更常用的选项,可用于创建正则表达式
    •       
    使用反斜杠字符 ( \ )
    •       
    匹配任意单个字符 ( . )
    •       
    匹配列表中的任意单个字符 ( [xy] )
    •       
    匹配不在列表中的任意单个字符 ( [^xy] )
    •       
    匹配某个范围内的任意单个字符 ( [x-y] )
    •       
    特定字符的零次或多次匹配 ( * )
    •       
    特定字符的一次或多次匹配 ( + )
    •       
    特定字符的零次或一次匹配 ( ? )
    •       
    正则表达式进行分组 ( ( ) )
    •       
    匹配几个正则表达式中的一个表达式 ( | )
    •       
    在一行的开始进行匹配 ( ^ )
    •       
    在一行的结尾进行匹配 ( $ )
    •       
    匹配包括下划线在内的任一字母数字字符 ( \w )
    •       
    匹配任意非字母数字字符 ( \W )
    •       
    组合正则表达式操作符

    补充2

    正则表达式用法
    正则表达式的概念

      什么是UBB代码?什么是正则表达式

      UBB代码是HTML的一个变种。一般情况下,UBB论坛不允许你使用HTML代码,而只能用UBB代码替代HTML代码。
      UBB代码是一套由流行的UBB标签组成了固定代码,代码有统一的格式。用户只要遵循代码规则就可以实现用户想要的功能。如:
      想要显示粗体的how are you 字样,就应该输入 how are you而不是输入<b>how are you</b>

      你也许会问:ASP是怎样把 h

  • QTP与QC的完美结合实现自动化测试框架-业务组件测试

    2008-09-18 09:45:28

    QTP与QC的完美结合实现自动化测试框架-业务组件测试

    字体:        | 上一篇 下一篇 | 打印  | 我要投稿  | 每周一问,答贴有奖

            摘要:利用QTP和QC相结合搭建功能自动化测试框架

            关键词:自动化测试    测试框架    组件

            做功能自动化测试都会不约而同的遇到一个比较棘手的问题-测试框架的搭建。这也是直接影响功能自动化测试成功与否的关键。框架做的好可以使测试事半功倍,反之轻则很难看到工作的 成果重则会使整个测试失败。目前网上有很多关于测试框架的讨论,其中也有成型的测试框架,其中有很多好的思想在里边,很值得借鉴。但今天要讨论的不是网上 已有的,而是HP已经为我们设计好的一个测试体系,业务组件测试。他是利用QTP与QC的完美结合组成的一个体系架构。它可以轻易实现目前比较流行的三层 测试架构:脚本层,业务层,数据层相分离,为开展功能自动化测试提供一个高效、稳定、容易的测试实现。

            一.概述

            1.1业务组件(Bussiness Process Testing)简介

            业务组件是组成流程测试的基本单元,组合不同的业务组件可以实现不同的业务流程测试。如将fligt系统的登录最为一个组件,选择航班最为一个组件等。这样可以实现组件的复用,提高开发效率。

             1.2 Bussiness Process Testing的优点

            1)   相关业务人员可以在没有脚本的环境下组合业务组件,实现业务流程。

            2)   对业务人员的编程能力没有要求,业务人员只需了解系统的业务流程,不用关心具体的脚本实现。这一点也实现了业务层和脚本层的分离。

            3)   一旦某个组件开发完毕,即可在不同的流程中使用该组件,实现高可复用性,从而加快业务流程测试的速度。

            4)   明确的角色分工,业务人员负责流程的开发、组织;QTP工程师负责脚本的开发、维护以及相应函数库的开发、维护。

            5)   因为实现了脚本的复用,提高了自动化开发的效率,无形中就降低了测试过程中维护的时间和成本。

            1.3 Bussiness Process Testing的简易流程

    软件测试

            如图所示,整个过程分为2条线:第一个是由业务测试人员划分组件并组合不同的组件实现不同的流程测试;其次QTP专家负责组件的脚本具体实现并负责调试成功,上传到QC供业务测试人员调用。

            注:测试数据的组织后边介绍,以便实现三层的测试架构;此过程需要QC有Bussiness Process Testing组件许可的支持,也就是需要单独向HP购买。

            下边以QTP自带的示例程序演示整个流程的开发过程

            2.1划分组件

            本次将系统划分为:登录;选择航班并插入;打开订单;更新订单;删除订单;注销。这样划分仅为演示之用,不用在实际的测试之中。

            2.2组织业务测试流程

            本次只是用于演示,所以流程不会100%覆盖,在实际的测试过程中要达到100%的流程覆盖。本次测试流程如下:

            流程1:登录-选择航班并插入-注销

            流程2:登录-选择航班并插入-更新订单-注销

            流程3:登录-选择航班并插入-更新订单-删除订单-注销

            流程4:登录-打开订单-更新订单-删除订单-注销

            下边需要根据划分的组件来实现组件脚本的实现。

            2.3创建应用程序区域

            在开发脚本之前首先要做的是要创建一个应用程序区域。应用程序区域提供创建业务组件所需的所有资源和设置,每个业务组建都居于一个应用程序区域,并从这些应用程序区域集成这些资源和设置。在此创建一个名为“订票系统流程测试”的区域,如图所示。

            创建过程:依次选择:file-New-Function library。保存后自动上传至QC默认目录。

    软件测试

            在此也可以加载自己的函数库,对象库,恢复场景等,这样以后创建的组建都可以共享该应用程序区域的资源。同时也方便维护,这也是一个优点所在,例如一旦函 数库改变在此从新加载新的函数库即可,不用在脚本理修改。总之这个应用程序区域很重要,以后所有的脚本均是基于这个区域。应用程序路径一定要加载正确,否 则录制时不能生成脚本。

            2.4创建脚本

            在创建脚本之前最好在QC中组织好目录树,方便保存及调用。关于脚本的开发过程,每个人、每个公司都有自己的方法。在此源代码也没法一一贴出。所以在此只 列出输入参数和输出参数,方便后边的参数化以及数据组织。本次也采用最通用的方式即对象库解决对象识别问题。脚本的开发规范以及参数命名也以我自己惯用方 式。

    软件测试

Open Toolbar