QTP常用对象Demo一览

上一篇 / 下一篇  2013-02-15 15:21:54 / 个人分类:功能自动化与QTP

一、WSH
  Dim oShell,appPath
  appPath = "c:\test11.txt"
  Set Shell = CreateObject("Wscript.Shell")
  Msgbox oShell.CurrentDirectory  '显示当前工作目录
  oShell.Exec appPath             '启动app
  oShell.SendKeys "^{TAB}"        '发送ctrl+TAB
  oShell.SendKeys "%{F4}"         '发送alt+F4
  oShell.SendKeys "+(EC)"         '按住shift的同时按EC
  oShell.SendKeys "+EC"           '按住E的时候按C,木有shift
  oShell.SendKeys "{^{DOWN} 10}"  ’连续发送10次ctrl+down
  oShell.Run Notepad.exe          '打开标题为notepad.exe的程序
  Set Shell = Nothing
 
二、FSO
   常用的FSO对象有FileSystemObject,Folder,TextStream,Drive.
   这里只罗列最常用的filesystemobject对象
   Dim folderPath
   folderPath = "F:\myFolder"
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set Folder = fso.CreateFolder(folderPath)
   Set myFile = fso.OpenTextFile(folderPath&"\11.txt",8,true)
   myFile.WriteLine "What a pity !"
   Set myFile = Nothing
   Set Flder = Nothing
   Set fso = Nothing
 
   另外,fso.GetFile(filePath)用于获取文件的全路径,此处filePath可使用相对路径
 
三、Des对象
   描述性编程分2种:
   1.直接描述性编程
     Dialog("text := login","index := 1").WinEdit("attached text := agent name:").Set "test"
   2.使用DESC对象
     Set myDesc = description.Create()
     myDesc("text").value = "OK"
     myDesc("index").value = "3"
     Dialog("text := login","index := 1").WinEdit(myDesc).Set "test"
     Set myDesc = nothing
   3.ChildObjects函数
     使用这个枚举函数可以获得父对象中的所有子对象
     Dim my_childObj
     Set my_childObj = description.Create
     my_childObj("html tag").value = "Input"
     my_childObj("type").value = "checkbox"
     Dim allCheckboxes,singleCheckbox
     Set allCheckboxes = Browser("Browser").Page("Page").ChildObjects(my_childObj)
     For each singleCheckbox in allCheckboxes
       singleCheckbox.Set "ON"
     Next
 
四、Excel.Application
'往excel里面读数据的公用方法
Function GetExcelCellValue(filePath,sheetName,cellRow,cellCol)
 Set excel = createobject("excel.application")
 Set excelSheet = CreateObject("excel.sheet")
 Set myexcel = excel.Workbooks.Open(filePath)
 Set mysheet = myexcel.Worksheets(sheetName)
    GetExcelCellValue = mysheet.cells(cellRow,cellCol).value
 myexcel.Close
 excel.Quit
 Set excelsheet = nothing
 Set excel = nothing
End Function
 
'往excel里面写数据的公用方法
Function WriteExcelCellValue(filePath,sheetName,cellRow,cellCol,cellText)
 Set excel = createobject("excel.application")
 Set excelSheet = CreateObject("excel.sheet")
 Set myexcel = excel.Workbooks.Open(filePath)
 Set mysheet = myexcel.Worksheets(sheetName)
        mysheet.cells(cellRow,cellCol).value = cellText
 If Ucase(mysheet.cells(cellRow,cellCol).value) = "PASS" Then
  mysheet.cells(cellRow,cellCol).font.colorindex = 50
  mysheet.cells(cellRow,cellCol).font.bold = true
  mysheet.cells(cellRow,cellCol).value = "PASS"
  elseif Ucase(mysheet.cells(cellRow,cellCol).value) = "FAIL" then
     mysheet.cells(cellRow,cellCol).font.colorindex = 3
     mysheet.cells(cellRow,cellCol).font.bold = true
     mysheet.cells(cellRow,cellCol).value = "FAIL"
   else
     mysheet.cells(cellRow,cellCol).font.colorindex = 0
 End If
 myexcel.Save
 excel.Quit
 Set excelsheet = nothing
 Set excel = nothing
End Function
 
五、XML对象
 
1.microsoftXML对象
  常用XML对象有XMLAttribute,XMLAttributesColl,XMLData,XMLElement,XMLElementsColl,XMLFile,XMLItemColl对象。
 
'查找xml文件中是否存在某个值
Function FindInfo(filePath,text)
 Set xmlDoc = CreateObject("Microsoft.xmlDom")
 xmlDoc.load(filePath)
 Set Root = xmlDoc.documentElement
 For i = 0 to Root.childNodes.Length - 1
  Set SaveItems = Root.childNodes.Item(i)
  ......
  If cstr(SaveItems.nodesvalue) = cstr(text) Then
   FindInfo = 1
   Set Root = nothing
   Set xmlDoc = nothing
  else
      FindInfo = 0
  End If
 Next
End Function
2.XMLUtil保留对象
  Set myXml = XmlUtil.CreateXML
  myXml.Load(filePath)
  或者 Set myXML2 = XmlUtil.CreateXMLFromFile(filePath)
  常用方法有Compare,GetRootElement,ChildElementsByPath.
 
六、DOM编程
  DOM目前已经成为各大浏览器的通用接口,其对外表现也是一个节点树,实际使用可以理解为使用的是对象的自身接口,即object对象自身的方法(QTP一般使用的是自身的封装接口,其实本质上也是对object对象做了一层封装)
  Set inputs = Browser("Browser").Page("Page").Object.all.tags("INPUT")
  Set myObj = Browser("Browser").Page("Page").Object.getElementById("td")
  msgbox myObj.innerText
  当然还有getElementsByName,getElementsByTagName这2个对象集合。
 
七、PDF文件读写
  常用对象有AcroExch.App,AcroExch.AVDoc,AcroExch.AVPageView,AcroExch.PDDoc等等
  Object.FindText(text,caseSensitive,wholdwordsonly,reset)
Function PDFFindText(filePath,text)
   Dim AcroApp,AcroAVDoc
   Dim gPDFPath,bReset,nCount
   Set AcroApp = CreateObject("AcroExch.App")
   'AcroApp.Show()
   Set AcroAvDoc = CreateObject("AcroExch.AVDoc")
   If AcroAvDoc.Open(filePath,"") Then
    AcroAvDoc.BringToFront()
    bReset = true
    Count = 0
    Do while AcroAvDoc.FindText(text,false,true,bReset)
     bReset = false
     nCount = nCount + 1
     wait 0,200
    Loop
   End If
   AcroApp.CloseAllDocs()
   AcroApp.Exit()
   msgbox "一共找到了"&nCount&"次"
   Set AcroAvDoc = nothing
   Set AcroApp = nothing  
End Function
 
未完待续!

TAG:

 

评分:0

我来说两句

Open Toolbar