标题: qtp 脚本控制js控件dtree,实现随机展开和选取dtree中的node [打印本页] 作者: ccgulan 时间: 2011-10-25 17:41 标题: qtp 脚本控制js控件dtree,实现随机展开和选取dtree中的node js 脚本代码:
function expSelNod(sl){
if (deptTree.aNodes[sl]._hc) {deptTree.o(sl,deptTree.aNodes[sl].id);}
else{selNod();}
};
function selNod(){
var l=deptTree.aNodes.length;
var zsl=parseInt(Math.random()*l);
var el=zsl;
for(var i=0;i<l-1;i++ ) {
if(qtpOp.title.indexOf(deptTree.aNodes.pid)>=0 || qtpOp.title.indexOf(deptTree.aNodes.id)>=0){
el=i;
if( i>=zsl){break ;}
}
};
select(deptTree.aNodes[el].id,deptTree.aNodes[el].name);
qtpOp.title="end"
};
function expAChildNod(sl){
var l=deptTree.aNodes.length;
var zsl=parseInt(Math.random()*l);
var el=zsl;
for(var i=0;i<l-1;i++ ) {
if(deptTree.aNodes.pid==deptTree.aNodes[sl].id){
el=i;
if(i>=zsl)break ;
}
};
qtpOp.title=qtpOp.title+","+el;expSelNod(el);
};
function mFun(){
debugger;
if (qtpOp.title==''){
var tl=deptTree.aNodes.length;
var tzsl=parseInt(Math.random()*tl);
if(tzsl==0)tzsl=1;qtpOp.title=tzsl;
expSelNod(tzsl);
};
else {
var pid=qtpOp.title.split(',');
if (pid.length>0) expAChildNod(pid[pid.length-1]);
}
};
document.getElementById('qtpOpt').onclick=mFun
将以上代码存入文本文件中
qtp脚本代码:
'该函数完成对dtree js控件的随机展开和随机点取
'pageObj:dtree 控件所在的页面
'dreeObjStr:dtree控件名称
Public Function selRamDtreeNode(pageObj, dreeObjStr)
Dim newObjDiv
Dim adobj
Dim selNode
Dim objHead,objScript
Dim tmpstr
tmpstr=readTxt("E:\qtp script\selNode.txt")
set newObjDiv = pageObj.Object.createElement("<div id='qpttest'>")
pageObj.Object.body.appendChild newObjDiv
Set adobj= pageObj.Object.getElementById("qpttest")
jsStr = "<input name=qtpOp id=qtpOpt type=button value=qtpOp "
jsStr = jsStr & ">"
adobj.innerhtml = jsStr
Set objHead = pageObj.Object.getElementsByTagName("HEAD").item(0)
Set objScript = pageObj.Object.createElement( "script" )
objScript.language = "javascript"
objScript.type = "text/javascript"
objScript.id = "sIdTest"
objScript.defer = true
objScript.text =tmpstr
objHead.appendChild( objScript )
a=pageObj.WebButton("name:=qtpOp").object.title
do while a<>"end"
pageObj.WebButton("name:=qtpOp").click
a=pageObj.WebButton("name:=qtpOp").object.title
wait 1
Loop
End Function
'读取文件
Public Function readTxt(filePath)
Dim fso,openfile
Dim str,restr
Set fso = CreateObject("Scripting.FileSystemObject")
Set openfile=fso.OpenTextFile(filePath,1,true)
Do Until openfile.AtEndOfStream
str=openfile.ReadLine
restr=restr&str
Loop
openfile.Close
Set fso=Nothing
readTxt=restr
End Function
action 调用
Dim dtreeobj
Set dtreeobj= Browser("**").Page("**").Frame("dept_frame")