开始学习QTP 一. 关于Web 1. 自动打开IE 'open IE and navigate to 163_email=========================================== Browser1 = "IE" StartURL = "http://mail.163.com/" If Browser1 = "IE" Then Set IE = CreateObject("InternetExplorer.Application") IE.Visible = true IE.Navigate StartURL End If 或: SystemUtil.Run "iexplore.exe", "http://mail.163.com/"
e.g. SystemUtil.Run "iexplore.exe", "http://newtours./" '启动IE Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").WebEdit("name:=userName").Set "test" Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").WebEdit("name:=password").Set "test" Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").Image("name:=login").Click 'Reporter.ReportEvent micPass,"正常","login succesully" ’调试语句 If Browser("title:=Find a Flight: Mercury Tours:").Exist(10000) Then Browser("title:=Find a Flight: Mercury Tours:").Close 'reporter.ReportEvent micFail,"失败","没有取到当前浏览器" '调试语句 End If ExitAction 'focus on current page =================================================== Set Browser_flight = Browser("title:=Welcome: Mercury Tours - Windows Internet Explorer") Set home_page = Browser_flight.page("title:=Welcome: Mercury Tours") 'close current page ====================================================== Browser("title:=Welcome: Mercury Tours - Windows Internet Explorer").Close
2. 调试 Reporter.ReportEvent micPass,"pass","检查XXX通过" reporter.ReportEvent micFail,"失败","没有找到对象XXX" reporter.ReportEvent micDone,"异常","定位不到对象xxxx"
3.new action
Set qtApp = CreateObject("QuickTest.Application") qtApp.Launch qtApp.Visible = True qtApp.New
ActionContent = "Window(""Calculator"").WinButton(""1"").Click" & vbCrLf & "Window(""Calculator"").WinButton(""+"").Click" & vbCrLf & "Window(""Calculator"").WinButton(""+"").Click" & vbCrLf & "Window(""Calculator"").WinButton(""="").Click" ActionDescr = "A new sample action for the test." ActionName = "Action" + CStr(qtApp.Test.Actions.Count + 1)
4.Web element 常用方法 a. CaptureBitmap Method Browser("Mercury Tours").Page("Find Flights").WebEdit("numPassengers").CaptureBitmap "edit_4.bmp" b. Check Method Browser("Mercury Tours").Page("Find Flights").WebCheckBox("roundtrip").Check CheckPoint("Roundtrip Ticket") c. CheckProperty Method Browser("Nested Lists").Page("Page").WebEdit("Name").CheckProperty "value", "Mercury" d. ChildObjects Method Set oDesc = Description.Create() oDesc("micclass").Value = "WebList" Set Lists = Browser("Mercury Interactive").Page("Mercury Interactive").ChildObjects(oDesc) NumberOfLists = Lists.Count() For i = 0 To NumberOfLists - 1 Lists(i).Select i + 1 Next e. Click Method Browser("New Page").Page("New Page").WebElement("html tag:=Form").Click f. Drag Method,Drop Method Browser("Microsoft Outlook Web").Page("Microsoft Outlook Web_2").Frame("viewer").WebElement("MyEmail").Drag , micNoCoordinate, micNoCoordinate, micCtrl Browser("Microsoft Outlook Web").Page("Microsoft Outlook Web_2").Frame("navbar").Link("Junk E-mail").Drop g. FireEvent Method Browser("New Page").Page("New Page").WebElement("html tag:=Form").FireEvent "onpropertychange" h. GetROProperty Method Val = Browser("Mercury Tours").Page("Find Flights").WebCheckBox("roundtrip").GetROProperty("Value") i. GetTOProperties Method Set LinkObject = Browser("Index").Page("index").Link("All kind of") Set Props = LinkObject.GetTOProperties PropsCount = Props.Count For i = 0 To PropsCount - 1 PropName = Props(i).Name PropValue = Props(i).Value MsgBox PropName & " = " & PropValue Next j. GetTOProperty Method TableTag = Browser("Mercury Tours").Page("Method of Payment").WebTable("FirstName").GetTOProperty("HtmlTag") 或 ListSize = Browser("ListOfStudentNames").Page("ListOfStudentNames").WebList("ListOfStudentNames").GetTOProperty("items count") Dim NamesArray()
ReDim NamesArray(ListSize - 1)
For i = 1 To ListSize NamesArray(i - 1) = Browser("ListOfStudentNames").Page("ListOfStudentNames").WebList("ListOfStudentNames").GetItem(i) Next
SendGradesByStudentName (NameArray) k. Output Method Browser("Mercury Technologies").Page("Mercury Technologies").Link("Jobs").Output CheckPoint("Jobs") l.RefreshObject Method Set MyLink = Browser("HP United States - Computers,").Page("HP United States - Computers,").Link("Contact HP") MyLink.Click Browser("HP United States - Computers,").Sync MyLink.RefreshObject MyLink.Click m.SetTOProperty Method Browser("New Page").Page("New Page").WebElement("just text").SetTOProperty "Style", "'color:blue'" n. Submit Method Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Submit o. ToString Method ObjectString = Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("who-to-do-it-with").ToString
Reporter.ReportEvent micDone, "We are selecting the fourth item in the " & ObjectString, "" Browser("Fill-Out Form Example").Page("Fill-Out Form Example").WebList("who-to-do-it-with").Select "#4" p. WaitProperty Method If Browser("index").Page("index").Link("All kind of").WaitProperty("attribute/readyState", "complete", 4000) Then Browser("index").Page("index").Link("All kind of").Click End If
5.Web Table a. ColumnCount Method NumColumns = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").ColumnCount(1) MsgBox "The number of columns in the row is " & NumColumns b. ChildItem Method Set WebEditObj = Browser("Mercury Tours").Page("Method of Payment").WebTable("FirstName").ChildItem(8, 2, "WebEdit", 0) WebEditObj.Set "Example" c. ChildItemCount Method Dim NumEdit NumEdit = Browser("Mercury Tours").Page("Method of Payment").WebTable("FirstName").ChildItemCount(1, 2, "WebEdit") d. GetCellData Method Set Text = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlight").GetCellData(1, 1) MsgBox "text contains" & Text e. GetRowWithCellText Method row = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").GetRowWithCellText("Price", 2, 2) Set Link = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").ChildItem(row, 2, "Link", 0) Link.Click f. RowCount Method NumRows = Browser("Mercury Tours").Page("Search Results").WebTable("OutboundFlights").RowCount
DataTable.Export ("C:\flights.xls")
DataTable.ExportSheet "C:\name.xls" ,1 '(save the first sheet of the run-time Data Table to the name.xls file.) d. GetCurrentRow Method
row = DataTable.GetCurrentRow
Reporter.ReportEvent 1, "Row Number", row e.GetRowCount Method
rowcount = DataTable.GetSheet("MySheet").GetRowCount
Reporter.ReportEvent 2, "There are " &rowcount, "rows in the data sheet."
The example below performs the equivalent of renaming a column (parameter) in the DataTable by copying the data from one column to a new column with a new name, and then deleting the old column.
'add a new column DataTable.GetSheet("dtGlobalSheet").AddParameter "NewColumn","Row1Value" 'copy the cells from the old column into the new column cnt=DataTable.GetRowCount For i=1 to cnt DataTable.SetCurrentRow(i) OldVal=DataTable.Value("OldColumn","dtGlobalSheet") DataTable.Value("NewColumn","dtGlobalSheet")=OldVal Next 'delete the old column DataTable.GetSheet("dtGlobalSheet").DeleteParameter("OldColumn") f. GetSheet Method
MyParam=DataTable.GetSheet ("MySheet").AddParameter("Time", "8:00")
You can also use this to add a parameter to the "MySheet" local sheet (note that no value is returned).
DataTable.GetSheet ("MySheet").AddParameter "Time", "8:00" g. GetSheetCount Method
sheetcount = DataTable.GetSheetCount
Reporter.ReportEvent 0, "Sheet number", "There are " & sheetcount & " sheets in the Data Table." h. Import Method
DataTable.Import ("C:\flights.xls") i. ImportSheet Method
DataTable.ImportSheet "C:\name.xls" ,1 ,"name" j. SetCurrentRow Method
DataTable.SetCurrentRow (2)
The example below performs the equivalent of renaming a column (parameter) in the DataTable by copying the data from one column to a new column with a new name, and then deleting the old column.
'add a new column DataTable.GetSheet("dtGlobalSheet").AddParameter "NewColumn","Row1Value" 'copy the cells from the old column into the new column cnt=DataTable.GetRowCount For i=1 to cnt DataTable.SetCurrentRow(i) OldVal=DataTable.Value("OldColumn","dtGlobalSheet") DataTable.Value("NewColumn","dtGlobalSheet")=OldVal Next 'delete the old column DataTable.GetSheet("dtGlobalSheet").DeleteParameter("OldColumn") k. SetNextRow Method
DataTable.SetNextRow l. SetPrevRow Method
DataTable.SetPrevRow m. GlobalSheet Property
ParamValue=DataTable.GlobalSheet.AddParameter("Time", "5:45")
You can also use this method to add a parameter to the global sheet (note that no value is returned).
DataTable.GlobalSheet.AddParameter "Time", "5:45" n. LocalSheet Property
MyParam=DataTable.LocalSheet.AddParameter("Time", "5:45")
o. RawValue Property
FormulaVal=DataTable.RawValue ("Date", "ActionA") p.Value Property
DataTable.Value ("Destination", "ActionA")="New York"
The following example uses the Value property to set the value in the current row of the second parameter (column) in the third sheet.
DataTable.Value (2,3)="New York"
Note: You could omit the word Value in the statements above, because Value is the default property for the DataTable object.
The following example uses the default property to set the value in the current row of the Destination parameter (column) in the current (active) local sheet.
DataTable("Destination", dtlocalSheet)="New York"
The example below performs the equivalent of renaming a column (parameter) in the DataTable by copying the data from one column to a new column with a new name, and then deleting the old column.
'add a new column DataTable.GetSheet("dtGlobalSheet").AddParameter "NewColumn","Row1Value"
'copy the cells from the old column into the new column cnt=DataTable.GetRowCount For i=1 to cnt DataTable.SetCurrentRow(i) OldVal=DataTable.Value("OldColumn","dtGlobalSheet") DataTable.Value("NewColumn","dtGlobalSheet")=OldVal Next
'delete the old column DataTable.GetSheet("dtGlobalSheet").DeleteParameter("OldColumn")
|