不谋万世者,不足谋一时;不谋全局者,不足谋一域。君子敏于事而慎于言 新浪微薄:土司阿哈

Qtp代码总结(二)(收集于网路)

上一篇 / 下一篇  2008-04-16 21:22:24 / 个人分类:qtp

1208328177
  • 自动化测试:
  • 测试文档:

Qtp代码收藏(源于网路)51Testing软件测试网z+N.h*h/Ok{u f

1.测试中我们使用QTP调试脚本的时候一般就是DEBUG或者MSGBOX察看一些信息,其实有时候也可以使用print来实现批量的察看信息但是不影响程序运行.51Testing软件测试网.YC!Z7Pd*W
运行脚本:

P}4r h aI*W0

代码:51Testing软件测试网%o4HN[|9{fQ.i)[ S/u

a="100"
~ j{Y(@,D0
print a51Testing软件测试网Y f1qa#G7MH6e

~~~~~~~~~~~~~~~~~~~~~~~~~51Testing软件测试网F/[GD{"Ru2h Vs*w
2.
datatable特定行的数据可以这样使用
%r8UmJ&P3s^ekc0
运行脚本:51Testing软件测试网$M!Ac yU0E

代码:51Testing软件测试网0QTqM}O

DataTable.GetSheet("Action1").GetParameter("test\").ValueByRow(4)51Testing软件测试网H K0{(m%H p$p:d9u j|

~~~~~~~~~~~~~~~~~~51Testing软件测试网)]tS&t0|[ Nrw
3.Wait Seconds [, Milliseconds]
可以精确到毫秒.51Testing软件测试网U#{Z|s8JMh&b
~~~~~~~~~~~~~~~~~~
-bL1Jqh\i04.
在自定义的function里面数组作为返回值.
B(E(Z{?8t5T$g]5\0
运行脚本:51Testing软件测试网Gl*D$I'g [2F|C

代码:

pb x)N,Z'T.]0

circuit = "399937"51Testing软件测试网9h%K:X9L5V/W
Function trimString(circuit)
IO ?p.mD0
Dim holdArray(5)51Testing软件测试网 t4Q7F*NvM v
holdArray(0) = Left(circuit, 2)
3x+x?/vC xHBV0
holdArray(1) = Right(circuit, 2)
^*o;_Bk:TO0
msgbox holdArray(0) 'showed 3951Testing软件测试网#r/u8q e5e9yz5Z
trimString = holdArray' I get an out of range error here
@,O)UrJa0
End Function
Os&dfvJ;L:Gnh0
dim myArray51Testing软件测试网;qJh1}f*N8H
'here I want to assign the return array to another array51Testing软件测试网`h$av1P:e)np,~E:r*_
myArray = trimString(circuit)
#HQ\S5a9r2A)S0
' and then call one element from it51Testing软件测试网-G8}d D\.C&d F6fVE
msgbox myArray(1)

S,f ncmE8EZ:d,Z0

~~~~~~~~~~~~~~~51Testing软件测试网0`w&A$cO~6F
5.
计算一个操作的时间.51Testing软件测试网3g^5TWGj&YR$x
运行脚本:51Testing软件测试网rK}6D*w)[*DX}&yu

代码:51Testing软件测试网1J.n:BB%yA ["YSe

Browser("Browser").Page("Page").Image("getRates").Click
P#T/ndt'x i0
var_StartTime = Timer51Testing软件测试网G!BE ?W'mf:k*h m
Browser("Browser").Page("Page").Sync51Testing软件测试网?p LJ3\L!eu&{^
Browser("Browser").Page("Page").Check CheckPoint("Check1")
wuOqFOt!P0
var_EndTime = Timer
R-qkr~/a,e0
intRespTime = round ((var_EndTime - var_StartTime), 2 )51Testing软件测试网dK!X8b1{(\
msgbox (intRespTime)

0?;V]I7II z0

~~~~~~~~~~~~~~~
mo)g a{06.
取得指定sheet(datatable)的行数和列数(也可以理解为参数个数)

F;h8C)m4b/@})P"i0

代码:51Testing软件测试网}9hzG!XMs:P

paramcount = DataTable.GetSheet("Action1").GetParameterCount51Testing软件测试网2P+]3Sn8K0v&\2h
msgbox "There are " &paramcount&"columns in the data sheet."
'N)_R-i^%Hhzx0
rowcount = DataTable.GetSheet("Action1").GetRowCount51Testing软件测试网0oA XU |g0x+\
msgbox "There are " &rowcount&"rows in the data sheet."51Testing软件测试网#TjpOO'U#sv

~~~~~~~~~~
b5kl#\3x K0RS%E7Fn07.
一种设置全局变量的方法GlobalDictionary
,@1q#Q/VFD~"q@7L0'Part 1.
4fKD7MBWd0'**********************************************************************************************************

3xCP^ nC:`6Z%^0

代码:51Testing软件测试网M Q![(Bq9T l

Dim WshShell
!Dq_0w.Zj0
Set WshShell =CreateObject("Wscrīpt.Shell")
"`S-_0C)m2PQ c5S1\0
WshShell.RegWrite "HKCU\Software\Mercury Interactive\QuickTest Professional\MicTest\ReservedObjects\GlobalDictionary\ProgID", "scrīpting.Dictionary","REG_SZ"
/IS+p-D#e1ZO,r3vvW4l0
Set WshShell = Nothing51Testing软件测试网6W5_6a ?C3ZpO&?

'*****************************************************************************************************51Testing软件测试网UllZ b/b!_%n
'Part 2.51Testing软件测试网 ^,Z*f5q#R
'*****************************************************************************************
51Testing软件测试网&be,qVH8W2B'Xs

复制内容到剪贴板51Testing软件测试网 k1cU*w1`'I#Xz+La

代码:51Testing软件测试网U|_8gP

GlobalDictionary.Add "ParamName", "ParamValue"
B7eV8i#m!h zY0
Msgbox GlobalDictionary.Item("ParamName")
&L:J@;V2z(F3go0
GlobalDictionary.Item("ParamName")="***********"51Testing软件测试网 Y-c8p d!j`,VJz
Msgbox GlobalDictionary.Item("ParamName")
F`"Chn!\I&@Y0
Msgbox GlobalDictionary.Exists("ParamName")
g}1V;YeKxn KR0
GlobalDictionary.Remove("ParamName")51Testing软件测试网EB2TX){7[
Msgbox GlobalDictionary.Exists("ParamName")

f9|c-['D h0

'*********************************************************************************************51Testing软件测试网XQ8tY]yA:`,D

gl"Q_W.]VCG1J h08.
关掉多余的IE窗口51Testing软件测试网z*xc:~L

复制内容到剪贴板51Testing软件测试网5N!D.M8[#l#N+v(I

代码:51Testing软件测试网,G}F:gDO(L

SystemUtil.CloseProcessByWndTitle "51testing", True51Testing软件测试网ZjD1_:P4Q|6c

~~~~~~~~~~~~~~~~~~
y.B,yj)W n Fy09.Execute
的用法,这个用法在一些特殊时候很有用的.51Testing软件测试网F"Z`(}1xm

复制内容到剪贴板

XI7gA gv^,q7nya&k0

代码:

,kD'\ k2W1^_F["v^0

x="4"
Tfn(C |3M0
Execute "Dim A_" & x
!SaZS0aQN0
Execute "A_" & x &"=99"51Testing软件测试网.T%|/p,C&j'mH
Msgbox Eval("A_" & x)  
4uh*LNb8w-]E?%g/F0
~~~~~~~~~~~~~~~~~~~

U/U ]r2Z+gkL;E0

10.GetLastError的介绍,看字面就是取运行最后一个错误

`#v-B U6{5jj4Pk0

代码:51Testing软件测试网t*l1Y1t N4`v(\

x = GetLastError51Testing软件测试网"@ K?\ i
msgbox(DescribeResult(x))

mC%^:]H uC m0

这样可以在程序里面判断程序运行时候是否出错了.
7\7Qq%{$Hyp4r0~~~~~~~~~~~~~~~~~
6ro6rOv Pl011.
weblist抓图下来51Testing软件测试网"GQ`D7]j-t2Aw

代码:

f$`D a~q2e5F~'z0

Setting.WebPackage("ReplayType") = 2 'Default is 1
3`0Br a^7f yH0
Browser(".").Page(".").WebList(".").Click51Testing软件测试网r2R!um#@
Desktop.CaptureBitmap "C:\Test.bmp",True51Testing软件测试网#Mpp a3P~+}3y
Setting.WebPackage("ReplayType") = 151Testing软件测试网 jn8st D

~~~~~~~~~~~~~~~~~~~~~~~~~~
U o/Mi,{C$Y*{5l]012.Reporter.ReportEvent
的新用法51Testing软件测试网!tbT&^T
transNumber = 12345

$B4fK+Q"` K0

代码:51Testing软件测试网 R fIUgN

Reporter.ReportEvent micPass, "TransactionNumber", "&transNumber&"

8JE}T3G {0

~~~~~~~~~~~~~~~~~~~~~~~~~~51Testing软件测试网%U ~d+L^s4CG
13.For
循环中step的用法51Testing软件测试网7B;pu6s*{ _gBk

复制内容到剪贴板

a#W.{5Z$n1i9Z0

代码:51Testing软件测试网!? }q:xv-}

For K = 1 To 10 step 2
D#X-R#Qf&|3rj0
          msgbox k
]e _[t$d0
Next

h%[WW*h `{V0

~~~~~~~~~~~~~~~~~~~~~~~

X/g[$Qp|0

TAG: QTP qtp

 

评分:0

我来说两句

Open Toolbar