方法1:
function regexselectqtp(object, spattern)
dim oregexp, arrallitems, ix
'create regexp object
set oregexp = new regexp
oregexp.ignorecase = false
oregexp.pattern = spattern
'split object's all_items property
arrallitems = split(object.getroproperty("all items"), ";")
for ix = lbound(arrallitems) to ubound(arrallitems)
'if regexp pattern matches list item, we're done!
if oregexp.test(arrallitems(ix)) then
object.select "#" & ix
set oregexp = nothing
exit function
end if
next
'select item #1 by default
object.select "#0"
end function
registeruserfunc "weblist", "regexselectqtp", "regexselectqtp"
使用的例子:
browser("").page("").weblist("").regexselectqtp "london - heathrow" 'select london-heathrow
browser("").page("").weblist("").regexselectqtp "london - heath.*"'select london-heathrow
browser("").page("").weblist("").regexselectqtp "london - /d+"
'select london-heathrow方法2
:function regexselectdom(object, spattern)
dim oregexp, ooptions, ix
'create regexp object
set oregexp = new regexp
oregexp.ignorecase = false
oregexp.pattern = spattern
'dom options
set ooptions = object.object.options
for ix = 0 to ooptions.length - 1
'if regexp pattern matches list item, we're done!
if oregexp.test(ooptions(ix).text) then
object.select "#" & ix
set oregexp = nothing
exit function
end if
next
'select item #1 by default
object.select "#0"
end function
registeruserfunc "weblist", "regexselectdom", "regexselectdom"
使用的例子:
方法3:
這種方法並沒有使用正規表示式,而是使用了
vbs中的
instr
和mid
方法public function vbsselect(object, sstring)
dim sallitems, varlocation, varend, varbeginning
'retrieve object's all_items property
sallitems = object.getroproperty("all items")
'verify if the supplied string is found in list's all_items property
varlocation = instr(1, sallitems, sstring)
'if found:
if varlocation > 0 then
varend = instr(varlocation, sallitems, ";")
if varend = 0 then varend = len(sallitems) + 1
varbeginning = instrrev(sallitems, ";", varlocation)
object.select "" & mid(sallitems, varbeginning + 1, varend - varbeginning - 1)
exit function
end if
'select item #1 by default
object.select "#0"
end function
registeruserfunc "weblist", "vbsselect", "vbsselect"
使用的例子:
browser("").page("").weblist("").vbsselect "london - heathrow" 'select london-heathrow
browser("").page("").weblist("").vbsselect "london - heath"'select london-heathrow
browser("").page("").weblist("").vbsselect "london - "'select london-heathrow
三種方法的執行效率比較:
run mode
normal
fast
regexselectqtp
0.44 s
0.38 s
regexselectdom
0.45 s
0.40 s
vbsselect
0.39 s
0.35 s
參考:http://relevantcodes.com/regular-expressions-with-select-method-listbox/
在QTP的Select方法中使用正規表示式
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!方法1 function regexselectqtp object,spattern dim oregexp,arrallitems,ix create regexp object set oregexp new regexp oregexp.i...
在QTP中使用RegisterUserFunc
registeruserfunc在qtp的幫助文件中可以看到,作用是用來自定義qtp物件下的方法。在實際的工作中這個方法是經常用到的,因為它能幫我們節省不少的時間。比如qtp的 set 這是個用來向輸入框中填入數值的方法,object.set 這個方法在實際應用中可能會帶來不便,比如實現修改操作的測...
在QTP中使用Dictionary物件
vbscript的dictionary 物件與perl的關聯陣列 associative array 類似,其中儲存的內容可以是任何資料,每乙個儲存的資料都與乙個唯一的鍵 key 關聯,鍵用於獲取每一項資料,鍵通常是整型或字串型別。在qtp中,可以把dictionary 當成陣列來用,而且可以隨時新...