发布新日志

  • [原创]在QTP中向WORD文档添加纪录

    2007-06-28 19:15:26

    '建立一个WORD对象
    Set ōWord = CreateObject("Word.Application")
    '打开在C盘下temp.doc,并且是可写的权限
    oWord.documents.open "c:\temp.doc",ForWriting, True
    Set ōDoc = oWord.ActiveDocument
    Set ōRange = oDoc.content
    '在文档里增加数据
    oRange.insertafter "test"
    '释放对象
    Set ōRange = Nothing
    Set ōDoc = Nothing
    Set ōWord = Nothing

     

    除了增加数据以外,在我的blog还有增加图片的方法,不过向WORD操作中的表格的数据需要点时间继续实践才能实现.

  • [原创]QTP日志实践的几点启示

    2007-06-24 13:43:29

    如需转载或者任何商业使用,请和笔者联系.谢谢.

    背景:在日常使用QTP中,因为有QC的存在,导致了QTP的察看结果的功能并不是用的很顺手,所以笔者在脱离开QC的情况下,在工作实践中总结了QTP的日志实现的一些方法。

    1、  生成txt文件。这是从开发那边得到的启示。

    首先定义一个sub

    Public Sub WriteLineToFile(message)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fileSystemObj, fileSpec
    Dim currentTime
    currentDate = Date
    currentTime = Time
    testName = "log"
    Set fileSystemObj = CreateObject("scrīpting.FileSystemObject")
    fileSpec ="C:\" &testName& ".txt" 'change this according to your directory
    If Not (fileSystemObj.FileExists(filespec)) Then  
    Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True)  
    logFile.WriteLine ("#######################################################################")  
    logFile.WriteLine (currentDate & currentTime & " Test: " & environment.Value("TestName") )  
    logFile.WriteLine ("#######################################################################")  
    logFile.Close  
    Set logFile = Nothing
    End If
    Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)
    logFile.WriteLine (currentDate & currentTime & " " & message)
    logFile.Close
    Set logFile = Nothing
    Set fileSystemObj = Nothing
    End Sub

    这样就能在txt中直接明了的看到自己的日志了。(个人感觉比看QTPresults好多了了,当然QTP自身的results还有错误图片等等,下面会介绍我的解决方案)

     

    题外话:在实际应用中,我会在sub中增加一个flag,来标志不同的严重等级,在不同的情况下控制输出不一样类型的日志。

    2、  使用QTP自身的抓图功能

    Public Function capture_desktop()

    Dim datestamp

    Dim filename

    datestamp = Now()

    filename = Environment("TestName")&"_"&datestamp&".png"

    filename = Replace(filename,"/","")

    filename = Replace(filename,":","")

    filename = "C:\QTP_ScreenShots"&""&filename

    Desktop.CaptureBitmap filename

    Reporter.ReportEvent micFail,"image","<img src='" & filename & "'>"

    End Function

    该函数主要就是用到Desktop.CaptureBitmap的这个方法,把桌面的图片抓下来,这样比较自由的抓下任意时候桌面的图片,方便我们检查结果。

     

    题外话:可以通过Function的返回值和上面提到的方法结合在一起的话,效果会更好的。

    3、  使用outlook发送邮件

    Public Sub SendEmail(testname,strsubject,stremailcontent,attachment,strrecipient)
    Set ōut = CreateObject("Outlook.Application")
    Set mapi = out.GetNameSpace("MAPI")
    Set email = out.CreateItem(0)
    email.Recipients.Add(strrecipient)
    email.Subject = strsubject
    email.Body = stremailcontent
    Set ōAttachment = email.Attachments.Add(attachment)
    email.Send
    Set ōutlook = Nothing
    Set mapi = Nothing
    End Sub

    题外话:假如我们把日志和抓图都结合在一起作为附件发出去的话,在自动化测试中后期会很有用,当然前提是不使用QC的情况下。

     

    4、  这是一个针对上面第3点如何把抓图放在一个doc中。

    Set ōWord = CreateObject("Word.Application")
    oWord.DisplayAlerts = False
    oWord.Visible = False
    oWord.documents.open "testWordDoc.doc"
    Set ōDoc = oWord.ActiveDocument
    Set ōRange = oDoc.content
    oRange.ParagraphFormat.Alignment = 0
    oRange.insertafter vbcrlf '& " " & vbcrlf
    oRange.collapse(0)
    oRange.InlineShapes.AddPicture "ImagePath.bmp", False, True
    oWord.ActiveDocument.Save
    oWord.Application.Quit True
    Set ōRange = Nothing
    Set ōDoc = Nothing
    Set ōWord = Nothing

    当然QTPexcelword结合的不错的,所以在测试中灵活运动,效果会很好的。

     

    总结:毕竟大规模的QTP测试中,需要总结实践的很多,这是笔者是在如何将测试结果的产生中抛砖引玉,学无止境,和大家一起进步。

  • [原创]在QTP中使用描述性编程

    2007-06-24 13:32:48

    如需转载或者任何使用,请和笔者联系.谢谢.

    背景介绍:在QTP使用中描述性编程是一个提高QTP脚本的利用率的很好的方式。

    术语:描述性编程,环境变量,正则表达式

    描述性编程介绍:通常QTP是根据对象库来识别不同的对象,而描述性编程是QTP外另一种识别对象的途径,它不依赖于对象库,通过增加一些对象的描述来达到识别对象的目的。

    说明,本例子是以17testing的登陆界面为测试页面进行描述的。(URL[url]http://www.17testing.com/bbs/logging.php?action=login[/url])。本例子使用QTP9.0进行操作。

    步骤一、录制脚本

    Browser("一起测试网软件测试论坛 :: 软件测试专业网站").Page("一起测试网软件测试论坛 :: 软件测试专业网站").WebEdit("username").Set "222"

    这是录制一个输入用户名的操作。

     

    步骤二、初级描述性编程

    Browser("title:=一起测试网软件测试论坛 :: 软件测试专业网站.*").Page("title:=一起测试网软件测试论坛 :: 软件测试专业网站.*").WebEdit("type:=text","name:=username").Set "222"

    使用SPY查看对象属性,然后进行脚本的强化修改。这边有个小技巧,就是使用了.*,这是一个正则表达式的应用,因为在测试用,发现BrowserPagetitle对象是“一起测试网软件测试论坛 :: 软件测试专业网站 - powered by Discuz!”使用正则能是参数化简单一些而已。

     

    步骤三、描述性编程的提高

    Dim obj_Desc

    Set obj_Desc = Descrīption.Create

    obj_Desc(“type”).value= “text”

    obj_Desc(“name”).value= “username”

    Browser("title:=一起测试网软件测试论坛 :: 软件测试专业网站.*").Page("title:=一起测试网软件测试论坛 :: 软件测试专业网站.*").WebEdit(obj_Desc).Set "222"

    使用SPY查看对象属性,然后进行脚本的强化修改。这边有个小技巧,就是使用了.*,这是一个正则表达式的应用,因为在测试用,发现BrowserPagetitle对象是“一起测试网软件测试论坛 :: 软件测试专业网站 - powered by Discuz!”使用正则能是参数化简单一些而已。

     

    步骤四、使用自定义的环境变量

    file>>setting>>environment 中选择user-defined,增加三个环境变量。

    browser="一起测试网软件测试论坛 :: 软件测试专业网站.*".

    page="一起测试网软件测试论坛 :: 软件测试专业网站.*".

    wededit_username="username"

    这样脚本被修改为:

    Pbrowser=environment.Value("browser")

    Ppage =environment.Value("page")

    Dim obj_ text

    Set obj_text = Descrīption.Create

    obj_ text(“type”).value= “text”

    obj_ text(“name”).value= environment.Value("wededit_username ")

    Browser("title:="&Pbrowser).Page("title:="&Ppage).WebEdit(obj_ text).Set "121212"

    当然参数化的方式很多,这边介绍的是使用环境变量

     

    步骤五、使用导入环境变量

    <Environment>

           <Variable>

                  <Name>wededit_username</Name>

                  <Value>username</Value>

           </Variable>

           <Variable>

                  <Name>page</Name>

                  <Value>一起测试网软件测试论坛 :: 软件测试专业网站.*</Value>

           </Variable>

           <Variable>

                  <Name>browser</Name>

                  <Value>一起测试网软件测试论坛 :: 软件测试专业网站.*</Value>

           </Variable>

    </Environment>

    你可以手工导入,也可以用方法LoadFromFile自动导入。

     

    总结:优点是当对象一些属性变更以后,脚本能更容易维护。比如说对于一些通用的对象,比如说saveresetcancel等按钮,一个页面3个,30个页面就90个对象,假如save变成保存,那对象库就会有很大改动了。而使用了描述性编程只需要在导入XML的地方该一个值就好了。当然描述性编程的作用远远不止这些,这次只是抛砖引玉,共同进步。

  • [转载]QTP测试页面的连接是否可用.

    2007-05-23 18:36:51

    set a= Browser("Yahoo!")
    set b= Browser("Yahoo!").Page("Yahoo!")
    call CheckLinks(a,b)
    Function CheckLinks (BrowserObject,BrowserPage)
    CheckLinks=TRUE
    Dim s_URL,i_CreationTime
    Dim s_LinkOuterText,s_LinkInnerText,s_Linkhref
    s_URL=BrowserPage.GetROProperty("url")
    i_CreationTime=1
    i_LinkCount=BrowserPage.object.links.length - 1

    Dim i_Link

    For i_Link=0 to i_LinkCount
    If Trim(BrowserPage.object.links(i_Link).target)="" Then
    BrowserPage.object.links(i_Link).target="_blank" ' Set the link to open i a new window so that we dont have any change in current window
    End If

    BrowserPage.object.links(i_Link).click
    On error resume next
    Browser("CreationTime:=" & i_CreationTime).sync
    Browser("CreationTime:=" & i_CreationTime).Page("micClass:=Page").sync
    On error goto 0
    Dim s_LinkDetails

    IHTML = Browser("CreationTime:=" & i_CreationTime).Page("micClass:=Page").object.Body.innerHTML
    'Check if page was not able to be displayed
    If (InStr(IHTML,"HTTP 404") <> 0) Or (InStr(IHTML,"cannot be displayed") <> 0) Then
    s_LinkDetails="Link Broken" + vbcrlf + "Link Details:" +vbcrlf
    s_LinkDetails=s_LinkDetails+"OuterText: "+ s_LinkOuterText + vbcrlf
    s_LinkDetails=s_LinkDetails+"InnerText: "+ s_LinkInnerText + vbcrlf
    s_LinkDetails=s_LinkDetails+ "href: " + s_Linkhref+ vbcrlf
    s_LinkDetails=s_LinkDetails+ "Links Open in New Browse: " & bNewBrowser & vbcrlf
    Reporter.ReportEvent micWarning,"Check Link(" & i_Link & ") -> " & s_LinkOuterText ,s_LinkDetails
    CheckLinks=FALSE
    Else
    s_LinkDetails="Link Working" + vbcrlf + "Link Details:" +vbcrlf
    s_LinkDetails=s_LinkDetails+"OuterText: "+ s_LinkOuterText + vbcrlf
    s_LinkDetails=s_LinkDetails+"InnerText: "+ s_LinkInnerText+ vbcrlf
    s_LinkDetails=s_LinkDetails+ "href: " + s_Linkhref+ vbcrlf
    s_LinkDetails=s_LinkDetails+ "Links Open in New Browse: " & bNewBrowser & vbcrlf
    Reporter.ReportEvent micPass,"Check Link(" & i_Link & ") -> " & s_LinkOuterText ,s_LinkDetails
    End If

    Browser("CreationTime:=1").close ' Close the link open.
    Next
    End Function


     

  • [论坛] [分享]QTP学习分享(更新20070514)

    2007-05-15 18:06:35

    1.在测试中我们使用QTP调试脚本的时候一般就是DEBUG或者MSGBOX察看一些信息,其实有时候也可以使用print来实现批量的察看信息但是不影响程序运行.
    运行脚本:
    a="100"
    print a
    ~~~~~~~~~~~~~~~~~~~~~~~~~
    2.取datatable特定行的数据可以这样使用
    运行脚本:
    DataTable.GetSheet("Action1").GetParameter("test").ValueByRow(4)
    ~~~~~~~~~~~~~~~~~~
    3.Wait Seconds [, Milliseconds]可以精确到毫秒.
    ~~~~~~~~~~~~~~~~~~
    4.在自定义的function里面数组作为返回值.
    运行脚本:
    circuit = "399937"
    Function trimString(circuit)
    Dim holdArray(5)
    holdArray(0) = Left(circuit, 2)
    holdArray(1) = Right(circuit, 2)
    msgbox holdArray(0) 'showed 39
    trimString = holdArray' I get an out of range error here
    End Function
    dim myArray
    'here I want to assign the return array to another array
    myArray = trimString(circuit)
    ' and then call one element from it
    msgbox myArray(1)
    ~~~~~~~~~~~~~~~
    5.计算一个操作的时间.
    运行脚本:
    Browser("Browser").Page("Page").Image("getRates").Click
    var_StartTime = Timer
    Browser("Browser").Page("Page").Sync
    Browser("Browser").Page("Page").Check CheckPoint("Check1")
    var_EndTime = Timer
    intRespTime = round ((var_EndTime - var_StartTime), 2 )
    msgbox (intRespTime)

    [ 本帖最后由 风过无息 于 2007-5-14 15:26 编辑 ]
  • [总结]QTP Timeout Settings

    2007-05-08 09:32:26

    1.      Object Sync Timeout:这是QTP在等待一个对象显示的时间

    具体设置方法: Test Settings, Run Tab, the “Object Synchronization timeout:” setting.

    请注意在8.x版本中单位是毫秒,而在9.x 就变成秒了。

    程序中设置: Setting(“DefaultTimeout”) [=milliseconds]

    2. Browser Navigation Timeout
    帮助里这样定义 “sets the maximum time (in seconds) that QuickTest waits for a Web page to load before running a step in the test”. 那和上面的Object Sync Timeout setting有什么区别的,MI没有说,我猜想是在测试一个显示很缓慢的页面的时候使用吧。针对Navigation而不是一个对,从脚本看:

    Browser(“Browser”).Page(“Page”).WebRadioGroup(“Name:=txt_Name”,”html tag:=INPUT”).set “Test”

    这可能是一开始Browser的等待时间吧。

    具体设置方法: Test Settings, Web tab, “Browser navigation timeout:” X “seconds”

    在程序中怎么实现还没有解决,谁可以帮助我?

    3. Default Load Time
    QTP help 这样定义Instructs QuickTest to add a specified number of seconds to the page load time property specified in each Page checkpoint.”. 很迷惑,按照帮助的理解就是检查点额外的等待时间,过了这个时间再没有出现检查的数据可能就会返回错误了。
    具体设置方法: Options, Web Tab, “Add” x “seconds to page load time”
    程序中设置: Setting(“DefaultLoadTime”) [=seconds]

    4. Activate Window
    定义“specifies the time (in tenths of a second) that QuickTest waits before it sets the focus on an application window when using the pointing hand to point to an object in the application (for Object Spy, checkpoints, Step Generator, Recovery Scenario Wizard, and so forth)”. 我一直使用的默认的值,挺好的。呵呵。
    具体设置方法: Options, General tab, “When pointing at a window, activate it after” X “tenths of a second”
    程序中设置,暂无。

    5. .Exists(Timeout)
    用的是毫秒作为单位.

    6.  .WaitProperty(x, y, Timeout)
    用的是毫秒作为单位.

    7. Wait seconds [, milliseconds]

    这个就是我们经常用的wait,不过后面可以选择毫秒可能有的人就不知道了。

     

     

    以上是我总结的一些QTP关于时间的设置的,很多概念自己也不算清楚,希望和大家一起在学习中进步。

  • [转载]不用checkpoint的Bitmap比较

    2007-04-18 09:07:40

    以下是我用文件对BITMAP进行比较的方法.
    但是这种方法只能比较两张图片的全部.不能比较比较图片中的部分区域.
    LcdDlgTitle = "LCD Device(μPD16432B)"
    LcdDlgTitle1 = "LCD1 Device(μPD16432B)"
    dialog(LcdDlgTitle ).CaptureBitmap "c:\lcd.bmp",true
    dialog(LcdDlgTitle1 ).CaptureBitmap "c:\lcd1.bmp",true

      Dim FS, File1, File2
       FilePath1 = "c:\lcd.bmp"
       FilePath2 = "c:\lcd2.bmp"
       Set FS = CreateObject("scrīpting.FileSystemObject")
       If ( FS.GetFile(FilePath1).Size <> FS.GetFile(FilePath2).Size) Then
        msgbox "FilePath1:=" &  FS.GetFile(FilePath1).Size  & "endl"
        msgbox "FilePath2:=" &  FS.GetFile(FilePath2).Size & "endl"
             CompareFiles = 1
             'Exit Function
       End If
       Set File1 = FS.GetFile(FilePath1).OpenAsTextStream(1, 0)
       Set File2 = FS.GetFile(FilePath2).OpenAsTextStream(1, 0)
      
       CompareFiles = False
       Do While File1.AtEndOfStream = False
             Str1 = File1.Read(1000)
             Str2 = File2.Read(1000)
             CompareFiles = StrComp(Str1, Str2, 0)
             If CompareFiles <> 0 Then
                      CompareFiles = 1
                      Exit Do
             End If
       Loop
       File1.Close()
       File2.Close()
    通过像素点的比较
    Dim mybmp1
    Dim mybmp2
    Set mybmp1 = LoadPicture ("c:\lcd.bmp")
    Set mybmp2 = LoadPicture ("c:\lcd1.bmp")
    msgbox mybmp1.Height  
    msgbox mybmp1.Width
    For   Y   =   0   To   mybmp1.Height   
      For   X   =   0   To   mybmp2.Width   
      fColor1   =   mybmp1.Point(X,   Y)
      fColor2   =   mybmp2.Point(X,   Y)     
      if   fColor1<>fColor2   then     
      msgbox   "不一样!"   

      End   If   
      Next   'X
      Next   'Y
  • [分享]在QTP中获取机器的内存占有量的方法。

    2007-04-05 13:00:29

    只是为了解决一些项目的特殊需求要来获得内存的占有的情况。和大家分享。

    On Error Resume Next
    strComputer = "localhost"
    Set ōbjWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
    Set colItems = objWMIService.ExecQuery("Select * from Win32_PerfRawData_PerfOS_Memory",,48)

    For Each objItem in colItems
    msgbox objItem.AvailableBytes
    Next
    Set ōbjWMIService = Nothing
    Set colItems = Nothing
  • [原创]QTP的在txt中写日志的方法

    2007-03-30 18:26:20

    就是为了在脚本中出现特殊情况的时候写到txt文件中方便查找。

    Public Sub WriteLineToFile(message)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    Dim fileSystemObj, fileSpec
    Dim currentTime
    currentDate = Date
    currentTime = Time
    testName = "log"
    Set fileSystemObj = CreateObject("scrīpting.FileSystemObject")
    fileSpec ="C:\" &testName& ".txt" 'change this according to your directory
    If Not (fileSystemObj.FileExists(filespec)) Then  
    Set logFile = fileSystemObj.CreateTextFile(fileSpec, ForWriting, True)  
    logFile.WriteLine ("#######################################################################")  
    logFile.WriteLine (currentDate & currentTime & " Test: " & environment.Value("TestName") )  
    logFile.WriteLine ("#######################################################################")  
    logFile.Close  
    Set logFile = Nothing
    End If
    Set logFile = fileSystemObj.OpenTextFile(fileSpec, ForAppending, False, True)
    logFile.WriteLine (currentDate & currentTime & " " & message)
    logFile.Close
    Set logFile = Nothing
    Set fileSystemObj = Nothing
    End Sub
  • [分享]在QTP取得IP地址

    2007-03-29 17:02:43


    set IPConfigSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
    ("select IPAddress from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

    for each IPConfig in IPConfigSet
    if Not IsNull(IPConfig.IPAddress) then
    for i=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
    Msgbox IPConfig.IPAddress(i)
    next
    end if
    next

  • [原创]QTP测试页面字体颜色的办法

    2007-03-23 15:34:30

    和大家分享一下刚学的测试字体颜色的方法。

    set iDisplay = Browser().Page().WebElement().Object
    ' Get the currentstyle object
    set iDisplayStyle = iDisplay.currentstyle
    ' Access the Display attribute
    sTmp = ""
    sTmp = iDisplayStyle.color

    'This way gets you the color of object.

    当然字体大小,什么字体也一样可以测试了。

  • [原创]用Schedule定时运行QTP脚本

    2007-03-23 09:58:57

    创建一个.vbs,在控制面板的Schedule里面定义自己的task就好

    vbs的脚本如下:

    Dim qtApp 'As QuickTest.Application ' Declare the Application object variable
    Dim qtTest 'As QuickTest.Test ' Declare a Test object variable
    Dim qtResultsOpt 'As QuickTest.RunResultsOptions ' Declare a Run Results Options object variable
    Set qtApp = CreateObject("QuickTest.Application") ' Create the Application object
    qtApp.Launch ' Start QuickTest
    qtApp.Visible = True ' Make the QuickTest application visible
    ' Set QuickTest run options
    qtApp.Options.Run.ImageCaptureForTestResults = "OnError"
    qtApp.Options.Run.RunMode = "Fast"
    qtApp.Open "C:\ Your Test Path Here", True ' Open the test in read-only mode
    ' set run settings for the test
    Set qtTest = qtApp.Test
    qtTest.Settings.Run.OnError = "NextStep" ' Instruct QuickTest to perform next step when error occurs
    Set qtResultsOpt = CreateObject("QuickTest.RunResultsOptions") ' Create the Run Results Options object
    qtResultsOpt.ResultsLocation = "C:\Your Results Path Here" ' Set the results location
    qtTest.Run ' Run the test
    'qtTest.Close ' Close the test
    Set qtResultsOpt = Nothing ' Release the Run Results Options object
    Set qtTest = Nothing ' Release the Test object
    Set qtApp = Nothing ' Release the Application object

  • [原创]QTP使用outlook发邮件

    2007-03-20 17:04:09

    因为需要把一些错误信息发给相关人员,让工具更自动化。

    Dim objOutlook
    Dim objOutlookMsg
    Dim olMailItem
    ' Create the Outlook object and the new mail object.
    Set ōbjOutlook = CreateObject("Outlook.Application")
    Set ōbjOutlookMsg = objOutlook.CreateItem(olMailItem)
    Set mapi = objOutlook.GetNameSpace("MAPI")
    ' Define mail recipients
    objOutlookMsg.To = "
    my@email.com"
    'objOutlookMsg.CC = "
    my@email.com"
    ' objOutlookMsg.BCC = "
    my@email.com"
    ' Body of the message
    objOutlookMsg.Subject = "QTP Test Mail"
    objOutlookMsg.Body = "This is a test mail"
    'Display the email
    objOutlookMsg.Display
    ' Send the message
    objOutlookMsg.Send
    ' Release the objects
    objOutlook.quit
    Set ōbjOutlook = Nothing
    Set mapi = Nothing

  • [原创]QTP中产生随机字符串

    2007-03-20 12:15:05

    产生背景:经常会有一些输入域的测试,少的有255,多的更可怕,在准备数据的时候会比较头疼,所以写了个函数和大家分享。

    Function makestring(inputlength)
    If IsNumeric(inputlength) Then
    For I = 1 To inputlength
    'you may add a random function here
    A = Array("a","b","c")
    Randomize
    x=RandomNumber (0,2)
    B = A(x)
    makestring =makestring +B
    Next
    msgbox ("output the string:"&makestring )
    else
    msgbox ("error format:"&inputlength)
    End If
    End Function
    Call makestring("8")

  • [整理]QTP中&和_的用法。

    2007-02-25 09:11:58

      ForWriting = 2
       Set fso = CreateObject("scrīpting.FileSystemObject")
       Set f = fso.OpenTextFile("c:\testfile.html", ForWriting, True)
       html="HTML"
       
       f.Write    "<HTML>"   &_
       "<HEAD>"  &_
        "<TITLE>一个简单的HTML示例</TITLE>"  &_
        "</HEAD>"  &_
        "<BODY>"  &_
        "<CENTER>"  &_
        "<H3>欢迎光临"   &_
         "的主页</H3>"  &_
             "<BR>"  &_
             "<HR>"  &_
          "<FONT SIZE=2> "  &_
               "  这是我第一次做主页,无论怎么样,我都会努力做好!"  &_
          "</FONT>"&"</CENTER>"&" </BODY>"  &_
          "  "&" </HTML>"
       wait(5)
    f.Close

    这是一段QTP脚本,其中使用到了&和_。

    & 负责连接字符串,当然也可以隐性把整数,浮点数等其他数据类型转换为字符串
    _ 续行连接,如某句代码一行显示过长,我们可以把代码换行写,但句末要加上"_"符号.当语句本执行时,编译器看到了这句句末有"_"时,就会知道语句还没结束,只不过换行了,跳到下面继续读.
     
  • [转载]QTP中MS SQL SERVER数据库连接的一种简单方法

    2007-02-12 09:02:06

    QTP中MS SQL SERVER数据库连接的一种简单方法

    在QTP中,我们可以通过复制数据库数据到databable中,实现数据的参数化.但这样的做法往往比较烦琐.换一种思路,在Expert View中直接添加连接数据库代码如何?接下来,我们就来完成这个工作.

      对于MS sql server数据库的连接,我们首先要知道所用的MS sql server数据库的连接字符串.这里告诉大家一个轻松获取连接字符串的方法: 新建.txt文件,修改文件名(包括后缀)为XX.udl,双击打开XX.udl文件,在"Provider"选项中选择"Microsoft OLE DB Provider for SQL Server",在"Connection"中,选择相应的server name和database,有用户名和密码的输入用户名和密码,点击右下方的"Test Connection"验证数据库是否连通.点击"OK".把XX.udl文件用记事本方式打开,以"Provider"开始的部分就是你的数据库连接字符串.

      知道了连接字符串,接下来我们来完善代码部分.

      Dim Cnn //定义变量

      Dim Rst //定义变量

      Dim strCnn //定义变量

      strCnn="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=sa;Initial Catalog=test;Data Source=SIMONVM" //将获得的连接字符串赋值到

      strCnn Set Cnn=CreateObject("ADODB.Connection") //生成数据库连接对象

      Cnn.Open strCnn //打开数据库 Set Rst=CreateObject("ADODB.Recordset") //生成记录集对象  

      Rst.Open "select OrgName from Organization",Cnn //书写SQL语句

      Rst.MoveFirst //将焦点定在第一行

      还可以通过Rst.Field( )函数取出对应行的值(参看QTP Tutorial Help).

      对于以上代码,我只是完成了MS sql server数据库的连接和执行命令部分.后续对表格的应用还未涉及.希望大家指点和补充.
  • [原创]QTP中根据日期计算年龄的解决办法

    2007-01-13 14:36:36

    在论坛上看到有关QTP中根据日期计算年龄的解决办法,所以写了一个function来处理。
    涉及函数:
    1、date:取当前系统时间;
    2、isdate:判断输入的是否日期型的;
    3、year:取得年的值。

    具体的脚本:
    Public Function old(MyDate)
    If isdate(MyDate) then
        MyYear = Year(MyDate)
        Cyear=Year(date)
            if (MyYear>Cyear)  then
            msgbox "出生年("&MyYear&")不可以大于当前年 "
            else
            ōld=Cyear-MyYear
            end if
        else
        msgbox "错误的日期"
        end if
    End Function
    ddate="October 19, 1962"
    old1=old(ddate)
    msgbox("您的岁数是:"old1)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    如许转载,请和作者联系,谢谢。
  • 使用数据库做测试数据驱动

    2007-01-10 09:14:16

     

    事件的场景:在测试过程中,我们可能用到数据库中一些初始的数据进行测试,所以我们就希望把数据按照我们的预先设计导入到一个excel里面,然后在后面的测试中引入进来,用于测试。

    具体操作:

    '创建excel的对象

    Set ExcelObj = CreateObject("Excel.Application")

    '指定一个execl的格式,这个需要我们事先定制好

    aatype="F:\templet.xls"

    '打开excel

    ExcelObj.Workbooks.Open(aatype)

    '设置操作的工作表

    Set NewSheet = ExcelObj.Sheets.Item(2)

    '以下是数据库操作

    Dim rs,sq,pkey

    set conn=createobject("adodb.connection")

    set  rs=createobject("adodb.recordset")

    set  rs1=createobject("adodb.recordset")

    conn.open= "Provider=OraOLEDB.Oracle.1;Password=*;Persist Security Info=True;User ID=*;SERVER=*;Data Source=*;DBQ=*;"

    sql="select * from table"

    rs.open sql,conn

    i=1

    do while not rs.eof

                         values1=rs("pkey")

                         values2=rs("name")

                         NewSheet.Cells(i+1,1).Value =values1

                         NewSheet.Cells(i+1,2).Value =values2 

           i=i+1

             rs.movenext

    Loop

    '保存excel数据

    ExcelObj.ActiveWorkbook.Saveas "F:\data.xls"

    ExcelObj.Quit

    Set ExcelObj = Nothing

    rs.close

    set rs=nothing

    rs1.close

    set rs1=nothing

    conn.close

    set conn=nothing

    这样的话,我们在别的脚本里面直接把excel的数据import进来就能使用了。

  • 我的SP心得

    2007-01-03 17:47:16

        工作以后总对考试犯怵,特别是英文的,虽然现在换了一个外资公司,每天也都看英文,但是离开了金山词霸,心里就没有底了。

    参加考试的动力是为了公司争取测试的项目,MI的产品使用很广泛,有时候客户会制定要求使用这样的工具进行测试。所以我就被推在不得不考的境地了(过了公司报销,过不了自己倒霉)。

        因为没有正规的复习资料,所以在做了一些简单的例子,了解大概功能以后,我开始看帮助,先看中文,然后看英文,不懂的拿中文对照,这样至少把QTP的大概的功能过了一遍。因为我们做项目不一定会把所有知识点都用上,而且看一遍帮助很必要,你会发现很多新的功能可以在你项目中应用并提高我们脚本的质量。当然工具的学习最关键的是使用,看一点实践一点,这样知识就巩固了。

        其次是看前人的总结,51testing 上有很多问题和解决方案都是对帮助上知识点的补充和升华,我是怀着激动的心情去仔细看每一篇文章,每到一个知识点,都习惯了找个例子实践一把,收获的喜悦难以言表。

        最后,当朋友或者论坛上发现有新的问题的时候,自己就会跃跃欲试了,虽然有时候给的办法不是最好的,但是经过自己的思考,查找和调试,再加上强人们的指点,收获多多,体会多多。

        对于SP考试的话,感觉难度不大,还是在考我们对工具的掌握,大部分问题就是你对工具的了解。MI考前给的几道题目一定要看啊,那是送分的题目。考试的时候一定要看清楚题目,问什么看清楚了答案就很清楚了。此外可以带纸制的字典,而且70%就合格,难度就小多了。最后祝愿大家好运。

  • 抛砖引玉:QTP处理定时运行程序的办法

    2006-12-18 18:17:29

    在不用TD或者QC的情况,很难精确控制QTP脚本运行的时间,我的办法是增加等待时间。

    Function DiffADate(theDate)
       DiffADate = DateDiff("s", Now, theDate)
       wait(DiffADate)
    End Function
    Call DiffADate("2006-12-18 17:25:00")

    这个办法笨了点,不知道有没有更好的办法。欢迎大家讨论。

     

432/3<123>
Open Toolbar