webtable的2个很有用的函数-代码及用法

上一篇 / 下一篇  2010-12-04 12:23:02 / 个人分类:QTP/UFT

ObjectsByMicClass:返回WebTable中某一类型的所有对象集合51Testing软件测试网 k,|ABN&R"a*l

ItemByKeyColumn:查找对象返回同一行中另一列的某类型的对象51Testing软件测试网\0|"^ l$]G/[]

‘-------------------------------------------------------------------------

qa$zPF(H,ae0

' Function: ObjectsByMicClass51Testing软件测试网8a&~k"O)nWy:c
' Descrīption:
返回webtable中某一类型的所有对象集合

pp$wdS0' Return Value:
对象集合
Z@ n E)Akq/FTQa0' Arguments:51Testing软件测试网&]Xm/lc
' Obj : 测试对象 (WebTable)
DoM*F;OWLV0' micClass :
对象的micclass属性

/B T}"P E&N [ ^5]0

‘---------------------------------------------------------------------------51Testing软件测试网hh2t4[g-R cb+Pz l

 Function ObjectsByMicClass(Obj, micClass)

fZu-J u)Ua3uS0

           Set Table = Obj

7oCe#I4n6R g8?0

           ' Create a collection object to hold the items

$W)@.Y1n9k0

          Set bjCollection = CreateObject("scripting.Dictionary")

&z\*X7_x0G0

           ' Go over all the cells in the table, and look for objects with the specified micClass

&\G _Nzr&`!},a5q0

           For row=1 to Table.RowCount51Testing软件测试网z)S @|6`n \n

              ColumnCount=Table.ColumnCount(row)51Testing软件测试网M{}.D.B2hd:l.G

              For col=1 to ColumnCount51Testing软件测试网7|#EF9G*g ^ e(Q0p

                   For ItemIndex=0 to Table.ChildItemCount(row, col, micClass)-151Testing软件测试网:p(d6Q2Kt

                       Set childItem=Nothing

i[+Aj0d{-d&[0

                       Set childItem = Table.ChildItem(row, col, micClass, ItemIndex)

XW&S5_!RP0

                       If Not childItem is Nothing Then

B5Ma8} WyS6|0

                            ' If the cell contains a micClass object, add it to the collection51Testing软件测试网l {,elf-Kg+D H

                            ItemKey = objCollection.Count + 1

j K"v"q9s0

                            objCollection.Add ItemKey, childItem51Testing软件测试网Q6A8}s)Qa,b

                       End if51Testing软件测试网I3Bg(?.zl;J A{

                   Next51Testing软件测试网-OF'{ D#FV

               Next51Testing软件测试网)Eal~/Y3{Ei

         Next51Testing软件测试网hX.g1W%k Gp|2C`

           Set bjectsbyMicClass = objCollection

'g/R+ak"R9E0

      End Function

2y6CN-W(j|0

 

&n%x F3E/D{ r Q6V0

用法:注册用户定义的函数

;q8Eaf9wk;C0

RegisterUserFunc "WebTable", "ObjectsByMicClass", "ObjectsByMicClass"

C8M Z qJ"e9[0

 

pB:oiY3JI"l0

   Setcollection=Browser("**").Page("**").Frame("**").WebTable("**").ObjectsByMicClass("WebCheckBox")

tR i:A4@LV)O)C0

       For i=1 to collection.count

l8wW*W8g7Z G2I0

          If collection(i).GetROProperty("checked") Then

1Ls@6p(K*m0

               collection(i).Set "OFF"51Testing软件测试网Pw5WQb|!h^)Y*f

           Else

Pw$}Si0

              collection(i).Set "ON"

G%M5M'mL[M/p#C0

          End If

Wlhy0K0

      Next

`-i`,]'~V4IF0

 51Testing软件测试网5t(U{ uR

‘--------------------------------------------------------------------------------------

)q!|7wnfU&Jox0

' Function: ItemByKeyColumn
0L.N6gVX.w0' Descrīption: Returns an item from a column, based on the value of a
7jQ!d|KyT/x)Z0' key column
z&Iw+\q;J0c0v0' Return Value: Object51Testing软件测试网gl2s*Tdw%C
' Arguments:51Testing软件测试网*I;U;\$~?e"c
' Obj - Test Object (WebTable)51Testing软件测试网1j m&m+UT-j qg R
' KeyColumnIndex - Index of the KeyColumn
7?E{SERxg3u)o(cZ0' KeyColumnValue - Value to search for in the key column51Testing软件测试网vb!v!?%kY8r/[
' KeyItemIndex - Index of the value in the key column (if there is
"I-Kel9o-Y Q0'                        more than one). If 0, the first item will be used.51Testing软件测试网1Xd p&\sl`#_k
' TargetColumnIndex - Column from which to retrieve the target item
,}5{r:o7U.p'S$K&W0' micClass - The micClass of the target item
?XM4Z,eV Lj.c6@Bd0' TargetItemIndex - Index of the target item to retrieve (if there is
s4dG5Z2q-T0'                           more than one). If 0, the first item will be used.
S(D;Gsl&E0ed'Qd0' ------------------------------------------------------------------------------------------------------------------------------------
$L%t[+l1}f%v|\Co0Function ItemByKeyColumn(Obj, KeyColumnIndex, KeyColumnValue, KeyItemIndex, TargetColumnIndex, micClass, TargetItemIndex)51Testing软件测试网W'y&y0\T(e(F%jz9V!n
    Table = Obj
&i*O,`j/K'V9f0    rowCount = Table.RowCount51Testing软件测试网2z/a6i'I[IK N

2iFb)b-l*}N0    ' if TargetItemIndex was not specified, use 1 as deafult51Testing软件测试网x/@*H)N9X
    If TargetItemIndex < 1 Then
5qI `cP0        TargetItemIndex = 1
4[\}#n{$Qg0    End If51Testing软件测试网$r,H\w4mF
    ' if KeyColumnIndex was not specified, use 1 as default
StiZA7xY'q0    If KeyItemIndex < 1 Then
uad H#DC~q0        KeyItemIndex = 151Testing软件测试网P;KT9z3a&q
    End If
3j-l,uIP1O2I051Testing软件测试网\?BeMJ8OZTx
    ' look for KeyColumnValue in the key column to determine which51Testing软件测试网Z;AfwA5t'M+f!rP
    ' row to retrieve the targe item from51Testing软件测试网 IVf5d7k/{
    Row = 051Testing软件测试网*}UT7b-TMz
    foundIndex = 0
%?C;Ky#U/yG5c0    While Row <= RowCount And foundIndex < KeyItemIndex51Testing软件测试网qB!J&gk:V
        Row = Row + 151Testing软件测试网$BTT,P\f
        CellData = Table.GetCellData(Row, KeyColumnIndex)
CuP2Ag0        If CellData = KeyColumnValue Then51Testing软件测试网3E0C!w lx"h\8\QQ"v1HH
           foundIndex = foundIndex + 151Testing软件测试网5WR[|1R,v$F
        End If
v k(oy}0    Wend
l1ms i'f*s UM"c0    If foundIndex < KeyItemIndex Then
#VU3F p D4d1]0        Exit Function51Testing软件测试网$~/x5O;jP2J.A@
    End If
"NP%Sn3Pdm0
;o}Db]2G$|P rK0    ' Now that we know the row, retrieve the item (according to its micClass)51Testing软件测试网)t,d3J NW/K*a0^;f9W
    ' from the target column.51Testing软件测试网as7yt;F:DTh'{M
    ChildItemsCount = Table.ChildItemCount(Row, TargetColumnIndex, micClass)
OJ"C4uXF0    If ChildItemsCount > =1 And ChildItemsCount >= TargetItemIndex Then
/T3mqh[:T0c@+p!P0         Set GetItemByKeyColumn = Table.ChildItem(Row, TargetColumnIndex, micClass, TargetItemIndex-1)51Testing软件测试网9ssziK_
    End If
]`rEV.Q2OSc2}0End Function
51Testing软件测试网}(W#V)TjQ5o'x6Io^

 51Testing软件测试网y C}IyZ

 51Testing软件测试网.z;u&T.E:`M3Py#Hm%L

这个是我自己修改后的

0i;{|0]5d0

‘-----------------------------------------------------------------------------------------

}od,id8C0

' Function: ItemByKeyColumn
m2K~w.QSE0' Descrīption:
根据某行中的对象返回webtable中同一行中另一列的某类型的对象

$xR0A%?yK2Z|+q U-[#A1C0' Return Value: Object
u_;@FC$s,x]0' Arguments:51Testing软件测试网"}Yv+i o,F~
' Obj - Test Object (WebTable)
b'{ j3kN8Dn/D]D0' KeyColumnIndex –
待查找的列数
Hm aYg+`.h3n2tWt:?0' KeyColumnValue –
待查找的列的值,即getcelldata的值
/C$j)` Zd wI0' TargetColumnIndex –
需要查找的列的列数
y,F7AL'h9MYj.?0' micClass –
需要查找的对象类型51Testing软件测试网]?2bg9Zi-cT
' TargetItemIndex –
查找对象的索引,从0开始

Y4Tg0UG:r4M` k0

‘--------------------------------------------------------------------------------------------51Testing软件测试网HO9v-Dh r,@` vVG

Function ItemByKeyColumn(Obj, KeyColumnIndex, KeyColumnValue, TargetColumnIndex, micClass, TargetItemIndex)51Testing软件测试网2\t9b@J@ X3E

  SET   Table = Obj

#js}*my-CC0

  rowCount = Table.RowCount51Testing软件测试网hD9{1{'Wp*p5p

For i=2 to rowcount ‘一般webtable都有标题栏,所以从第二行开始51Testing软件测试网'Y.Wzc&M

      celldata=table.getcelldata(i,KeyColumnIndex)

+P PnnS]F Bf0

      If celldata=KeyColumnValue Then51Testing软件测试网-o;Z[V0tv\#u*R

If Table.ChildItemCount(I,TargetColumnIndex, micClass)>0 then51Testing软件测试网r.f]8Rq F#G

如果该单元格的该对象个数不为0,则查找

#hYB I9h&J5zM0

   Set ItemByKeyColumn = Table.ChildItem(i, TargetColumnIndex, micClass, TargetItemIndex)

*]L%j:U%y+s0

             Exit for

v m:`0nt0

     Else

8C(ZlW(Sm.c `0

      ExitFuncion

2pu^QQ_ mIL*d0

    reporter.reporterevent fail,”查找对象”,”找不到”&micclass&”类型的对象51Testing软件测试网r lf fr

End If51Testing软件测试网*D(O/|IqU Shz g

     

0\GE8h5_0

      End If

.r9q uzrK0

 

'H c6?-u*l_?0

Next

#V|x)k?T vN0

End Function51Testing软件测试网 _"h2{(p nxm)CZ

   

c2dGx p2ybF0

 

#t1qH/ps@)xH[0

RegisterUserFunc "WebTable", "ItemByKeyColumn", "ItemByKeyColumn"51Testing软件测试网&Ssa0m2Y%Aq1K0t*y)O

 51Testing软件测试网i7@6U R Qf } m

  ‘查找webtable中第一列中value值为test的行的第四列的第一个链接,并点击

(_.q$X)R5` ~G0

    Setobj=Browser("**").Page("**").Frame("main").WebTable("**").ItemByKeyColumn(1,"test",4,"LINK",0)51Testing软件测试网k&a-dX0pR

 obj.click()51Testing软件测试网:n*R;a"P.q

 

2?:] v O$K9yKF$Y0

TAG:

 

评分:0

我来说两句

Open Toolbar