背景:作为一个测试,特别是性能测试,尤其在活动的测试,时间紧,有很多要测的,我们的LR11因为浏览器兼容问题全录制不了脚本了,用浏览器加代理或手机加代理录制,我感觉好麻烦 ,所以就想如果能用脚本把所有的测试请求,全部自动生成脚本拿来直接用,岂不是爽歪歪。
最近我老大,非要我用python。他有个特点,每当我们用哪个东西刚刚上手的时候,他就要求我们再扩展。
所以我们这里的人的特质就是:懂的很多,但是都不精通。
我的urls.txt
我的脚本就是逐行读取URL,然后把action_name\web_url名称、url给取出来,拼出LR脚本的格式,存储在C文件中。
# -*- coding:utf-8 -*- ‘‘‘ @Author :ll @Version : v1.0 @File :ToLR.py @CreateTime :2019-11-28 下午 5:07 ‘‘‘ import os,sys #一批URL过来,生成简单的LR脚本 class ToLr: def ToGetScript(self,fromfile,tofile): for url in open(fromfile,‘r‘): str = ‘‘ action_name = ‘‘ web_url =‘‘ if ‘?‘ in url:#带参数 name = (url.split(‘/‘)[-1]).split(‘?‘)[0] web_url=url.split(‘?‘)[0] if ‘.‘ in name: action_name = name.split(‘.‘)[0] else: action_name = name filename = tofile + ‘\\‘ + action_name + ".c" str = ToLr().samecontent(action_name,web_url) print(str) else: #没有? name=url.split(‘/‘)[-1] if ‘.‘ in name: action_name = url.split(‘.‘)[0] else: action_name = name filename = tofile + ‘\\‘ + action_name + ".c" str = ToLr().samecontent(action_name, url) print(str) with open("%s" % filename, "a+")as f: f.write(str) def samecontent(self,action_name,web_url): str = ‘‘ str = str + action_name + ‘(){‘ + "\n" str = str + ‘\t‘ + ‘web_url("‘ + action_name + ‘",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"URL=‘ + web_url + ‘",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"Resource=0",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"RecContentType=text/html",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"Referer=",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"Snapshot=t2.inf",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"Mode=HTTP",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘"Mode=HTTP",‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘LAST);‘ + ‘\n‘ str = str + ‘\t\t‘ + ‘return 0;‘ + ‘\n‘ str = str + ‘}‘ print(str) return str def ToPostScript(self, fromfile, tofile): #第1种,application/text form表单提交 for url in open(fromfile, ‘r‘): params={} name = (url.split(‘/‘)[-1]).split(‘?‘)[0] action_name = ‘‘ if ‘.‘ in name: action_name = name.split(‘.‘)[0] else: action_name = name filename = tofile + ‘\\‘ + action_name + ".c" uri = url.split(‘?‘)[0] lists = url.split(‘?‘)[1].split(‘&‘) for line in lists: params[line.split(‘=‘)[0]]=line.split(‘=‘)[1] str="" str = str + action_name + ‘(){‘ + "\n" str = str +‘\t‘+‘web_submit_data("‘+name+‘",‘+‘\n‘ str = str +‘\t\t‘+‘"Action=‘+uri+‘",‘+‘\n‘ str = str +‘\t\t‘+‘"Method=POST",\n‘ str = str +‘\t\t‘+ ‘"RecContentType=text/html",\n‘ str = str +‘\t\t‘+ ‘"Snapshot=t7.inf",\n‘ str = str +‘\t\t‘+ ‘"Mode=HTML",\n‘ str = str +‘\t\t‘+ ‘ITEMDATA,\n‘ for param in params: str = str +‘\t\t‘+ ‘"Name=‘+param+‘", "Value=‘+params[param]+‘", ENDITEM,\n‘ str = str +‘\t\t‘+ ‘LAST);\n‘ str = str + ‘\t\t‘ + ‘return 0;‘ + ‘\n‘ str = str + ‘}‘ with open("%s" % filename, "a+") as f: f.write(str) if __name__ == ‘__main__‘: a = ToLr() fromtxt = input("请输入urls存放的完整路径:") totxt = input("请输入脚本输出存放文件夹:") getorpost = input(‘你要生成的是get请求还是post请求:get输入1,post输入2‘) print(getorpost) if getorpost==‘1‘: a.ToGetScript(fromtxt, totxt) elif getorpost==‘2‘: a.ToPostScript(fromtxt, totxt) else: print(‘输入不正确,你到底想干什么!‘) print(str)
以上代码覆盖几个点:
1、get请求,包括带参和无参2种形式
2、post请求,只包括application/text类型的Form表单提交,application/json类型不支持
执行后生成C文件:
LR修改内容:
1、进.usr文件增加左树action显示
2、进.usp修改增加action
新增RunLogicRunRoot标签内容,修改名字2处
增加以下几处的action名称,以,隔开
原文地址:https://www.cnblogs.com/sincoolvip/p/11972439.html
时间: 2024-10-09 09:50:05