QTP知识搜集

来源:互联网 发布:ubuntu下怎么安装win10 编辑:程序博客网 时间:2024/05/19 15:22
  • 将我的电脑磁盘系统信息写入文本文件

    2007-08-20 22:03:28

    Dim a1,a2,a3,a4
    Set a1=createobject("scrīpting.filesystemobject")
    Set a2=a1.opentextfile("e:/info.txt",2,true)
    Set a3=a1.drives
    For each a4 in a3
            If a4.isready Then
                    a2.writeline a4.RootFolder.Type & a4.driveletter & "的文件系统是 '"&a4.filesystem&"'"
        else
                a2.writeline"the disk of " & a4.driveletter & " is not ready"
            End If
    Next
    a2.close

    运行后e:/info.txt文件内容如下:

    本地磁盘C的文件系统是 'NTFS'
    本地磁盘D的文件系统是 'NTFS'
    本地磁盘E的文件系统是 'FAT32'
    本地磁盘F的文件系统是 'NTFS'
    the disk of G is not ready
    the disk of H is not ready

  • 将字符串复制进剪贴板

    2007-08-19 21:19:33

    strCopy = "This text has been copied to the clipboard." 
    Set ōbjIE = CreateObject("InternetExplorer.Application") 
    objIE.Navigate("about:blank") 
    objIE.document.parentwindow.clipboardData.SetData "text", strCopy 
    objIE.Quit

    '再从剪贴板复制到编辑器

    Browser("网易电子邮箱 - 极速3.0Beta").Page("网易电子邮箱 - 极速3.0Beta").Frame("index").WebEdit("WebEdit_2").Object.focus
    wait 1
    Set wshShell = CreateObject("Wscrīpt.Shell")
    wshShell.SendKeys "{TAB}"
    wait 1
    wshShell.SendKeys "^v"
    wait 2

  • 修改完善的写log文件的QTP脚本

    2007-08-06 15:00:54

    参考自:http://bbs.51testing.com/thread-79991-1-1.html

    Public Sub WriteLogs(result)
     Const ForReading = 1, ForWriting = 2, ForAppending = 8
     Dim fileSystemObj, fileSpec
     Dim currentTime, currentDate, logName, logFile, message
     currentDate = Date
     currentTime = Time
     logName = "log"
     Set fileSystemObj = CreateObject("scrīpting.FileSystemObject")
     fileSpec ="D:/" &logName& ".txt" 'change this according to your directory
     If not (fileSystemObj.FileExists(fileSpec)) Then 
      Set logFile = fileSystemObj.CreateTextFile(fileSpec, 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)
     'result = "本测试用例通过!"
     logFile.WriteLine (currentDate & "  " & currentTime & "  " & result )
     logFile.Close
     Set logFile = Nothing
     Set fileSystemObj = Nothing
    End Sub

  • VBS脚本一行代码太长,使用换行符

    2007-08-03 17:55:05

    如果代码一行太长,想写成两行的话,要在行尾加下划线_)作续行符,例如:

     

    a = (1 + 2 + 3) * (1 + 2 + 3)

    '写成两行
    a = (1 + 2 + 3) * _
    (1 + 2 + 3)

    要想把多行代码写成一行,要用冒号:)作分隔符。例如:

     

    a = 1
    b = 2
    c = 3

    '写成一行
    a = 1 : b = 2 : c = 3
  • 读取文本文件的几个方法(ReadLine, SkipLine, ReadAll)

    2007-07-31 15:25:51

    ReadLine方法:读取文本文件的一行,每执行一次,文件读取指针自动移到下一行,再执行时,读取的就是下一行的内容,如:
    msg1=myfile.ReadLine         '读取第一行
    msg2=myfile.ReadLine           '读取第二行
    msg3=myfile.ReadLine           '读取第三行
    msgbox msg1
    msgbox msg2
    msgbox msg3
    SkipLine方法:跳过下一行,执行之后,文件读取指针移到下一行的下一行行首,如:
    msg1=myfile.ReadLine       '读取的是第一行
    myfile.SkipLine              '此句执行过后,指针已经移到了第三行行首

    msg2=myfile.ReadLine         '读取的是第三行
    msgbox msg1
    msgbox msg2
    ReadAll方法就是读取整个文件的内容,如:
    Dim fso, myfile,msg, tmp, i
    Set fso=CreateObject("scrīpting.FileSystemObject")   
    Set myfile = fso.openTextFile("C:/login.txt",1,false)
    'msg1=myfile.ReadLine
    'msg2=myfile.ReadLine
    'msg3=myfile.ReadLine
    msg = myfile.ReadAll
    tmp = split(msg, vbcrlf)    '将各行的内容存放到tmp数组中
    For i=0 to Ubound(tmp)
      msgbox tmp(i)
    Next
    myfile.close

  • 我的QTP模板

    2007-07-23 20:50:45

    '-------------------脚本说明---------------
    '测试对象:     
    '测试员:  David Gao
    '编写日期:
    '测试功能:
    '进展程度:
    '基本思路:
    '主要功能函数:
    '没解决的问题:
    '--------------------脚本内容-------------
    wait 5
    Dim firstTitle
    firstTitle = Browser("micClass:=Browser").Page("micClass:=Page").GetROProperty("title")
    Do while firstTitle="找不到服务器"
     Browser("micClass:=Browser").Refresh
    Loop

    Dim objectCount, tags, element, innerText()
    Set tags=Browser("Browser").Page("Page").Object.all

    objectCount=0
    For Each element in tags
        If Ucase(element.tagname)="A" Then
      ReDim Preserve innerText(objectCount+1)
            innerText(objectCount)=element.InnerText
         ōbjectCount=objectCount+1
        end if
    Next

  • QTP自动发邮件

    2007-07-18 17:47:48

    http://www.51testing.com/?3528/action_viewspace_itemid_5211.html

    http://bbs.51testing.com/viewthread.php?tid=54183

    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 = "gzj08@126.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

  • 在回放过程中,怎么保证IE窗口总是最大化?

    2007-07-12 17:08:10

    http://bbs.51testing.com/thread-79929-1-13.html

    http://www.51testing.com/?4696/action_viewspace_itemid_12121.html

    上面那个链接并没有介绍最大化IE窗口的方法(我参考他的方法修改如下),而第二个链接是改变QTP本身窗口的大小

    'Open an IE browser with the method CreateObject()
    Dim objIE, hwndIE
    Set ōbjIE = CreateObject("internetexplorer.application")
    objIE.Visible = true
    objIE.Navigate "http://www.tudou.com/index.html" 'The parameter URL is the destination address for the IE browser

    'Wait a while till the page is fully loaded
    Wait(10)

    'Move and resize the browser
    hwndIE = objIE.HWND
    'Operate the IE browser as a window object with descrīptive programming
    'The window object can be uniquely identified by the parameter hwndIE, which is the window's handle of the browser
    'Window ("hwnd:=" & hwndIE).Minimize '最小化
    Window ("hwnd:=" & hwndIE).Maximize '最大化

    'Release the object after the adjustment
    Set ōbjIE = nothing

    获取新打开的IE窗口的句柄

    dim hwndIE
    hwndIE=Browser().GetROProperty("HWND")
    Window("hwnd:=" & hwndIE).Maximize
  • 判断打开了多少IE窗口

    2007-07-11 16:41:59

    Set Brow_page=Descrīption.Create()
    Brow_page("micclass").value="Browser"
    set Brow =desktop.childobjects(Brow_page)
    msgbox Brow.count()
  • 脚本是严格按照用例情况来录制的

    2007-07-10 17:31:36

    (摘自walker1020的《循序渐进学习QTP三步曲》)

    “脚本是严格按照用例情况来录制的。” 你的测试用例规定了需要测试哪些功能,用到哪些测试数据。如果没有测试用例,那么你用QTP进行测试就没有依据,就成了无的放矢、缘木求鱼了。
    QTP只是代替了手工测试,只是一个工具而已。测试工程师要做的事情就是 制定测试计划、编写测试用例、录制脚本、优化和回放脚本、分析TestReport。 在测试流程中,使用QTP 只是其中的一个环节。如果QTP 什么都能做,那么就不需要我们这些测试人员了。 QTP不能代替一切测试工作,就像 机器永远不能代替人一样!
  • 输出链接的target属性

    2007-07-10 16:48:39


    set a= Browser("土豆网 - 视频 - 播客 - 每个人都是生活的导演")
    set b= Browser("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演")
    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
    'msgbox i_LinkCount

    Dim i_Link

    For i_Link=0 to i_LinkCount
        If Trim(BrowserPage.object.links(i_Link).target)<>"" Then
      msgbox BrowserPage.object.links(i_Link).target
      msgbox i_Link
      'BrowserPage.object.links(i_Link).click
        End If

    Next
    End Function

  • 检查链接的有效性(参考自:风过无息)

    2007-07-10 16:41:11


    set a= Browser("土豆网 - 视频 - 播客 - 每个人都是生活的导演")
    set b= Browser("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演")
    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 changein current window
    End If

    'msgbox i_Link
    'msgbox BrowserPage.Link("index:=" & i_Link).GetROProperty("index")
    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:=" & i_CreationTime).Sync
    Browser("CreationTime:=" & i_CreationTime).close ' Close the link open.
    If i_Link = 6 Then
     
    Browser("制作相册 - 土豆网 - 播客 个人多媒体").Dialog("Microsoft Internet Explorer").Activate
    Browser("制作相册 - 土豆网 - 播客 个人多媒体").Dialog("Microsoft Internet Explorer").WinButton("确定").Click
    End If


    Next
    End Function

  • Link参数化

    2007-07-10 16:37:31

    SystemUtil.Run "C:/Program Files/Internet Explorer/IEXPLORE.EXE","","C:/Documents and Settings/dgao","open"
    Browser("Browser").Page("Page").Sync
    Browser("Browser").Navigate "http://www.tudou.com/index.html"
    Browser("Browser").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Link("首 页").Click
    Browser("Browser").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Sync

    Dim i, j, tags, element, arr()
    Set tags=Browser("Browser").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Object.links

    i=0
    For Each element in tags
        If Ucase(element.tagname)="A"  Then       'and left(element.InnerText,1)="["
                            ReDim Preserve arr(i+1)
                        arr(i)=element.InnerText
        i=i+1
        end if
    Next
    'msgbox i
    'msgbox arr(24)
    For j=0 to 10
     Browser("Browser").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Link("首 页").SetTOProperty "target",""
        Browser("Browser").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Link("首 页").SetTOProperty "Text",arr(j)
        Browser("Browser").Page("土豆网 - 视频 - 播客 - 每个人都是生活的导演").Link("Text:=" & arr(j)).Click
        Browser("Browser").Back
    Next

  • QTP学习

    2007-07-06 12:24:00

    检查Link的有效性:http://bbs.51testing.com/thread-18089-1-4.html

    http://www.51testing.com/?3528/action_viewspace_itemid_11210.html