在使用接口功能测试工具时,都需先通过用户登录接口进行一次登录,然后在测试用户基本资料查询、密码修改、好友信息查询、游戏经验值查询等接口时,无需用户输入userID和webkey,将会通过javascript脚本将cookie中登录生成的userID和webkey取出,并自动填充被测试接口的userID和webkey相应项。
登录成功写入userID和webkey到cookie中的代码可见:用户登录接口的消息封装脚本;自动调用userID和webkey的脚本代码为:
1 function getCookieByName(name){ 2 //获取cookie字符串 3 var strCookie=document.cookie; 4 //将多cookie切割为多个名/值对 5 var arrCookie=strCookie.split("; "); 6 //遍历cookie数组,处理每个cookie对 7 for(var i=0;i<arrCookie.length;i++){ 8 var arr=arrCookie[i].split("="); 9 10 //找到名称为userId的cookie,并返回它的值 11 if(name==arr[0]){ 12 if( typeof arr[1]=='undefined')return ''; 13 return arr[1]; 14 } 15 } 16 return ''; 17 } 18 19 function showUseIdAndWebKey(){ 20 21 setElementVal('webKey'); 22 setElementVal('userID'); 23 24 } 25 26 function setElementVal(elementName){ 27 var webkeys=document.getElementsByName(elementName); 28 if( typeof webkeys=='undefined')return ''; 29 if(webkeys&&webkeys.length>0){ 30 var webkey=getCookieByName(elementName); 31 webkeys[0].value=webkey; 32 } 33 34 35 } 36 window.onload=showUseIdAndWebKey; |
在用户基本资料查询、密码修改、好友信息查询、游戏经验值查询等需要自动填充userID和webkey的接口页面加入调用上述脚本的语句即可。
1 <script type="text/javascript" src="showWebKey.js"></script> |