自动生成LR脚本且运行
Posted sincoolvip
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了自动生成LR脚本且运行相关的知识,希望对你有一定的参考价值。
背景:作为一个测试,特别是性能测试,尤其在活动的测试,时间紧,有很多要测的,我们的LR11因为浏览器兼容问题全录制不了脚本了,用浏览器加代理或手机加代理录制,我感觉好麻烦 ,所以就想如果能用脚本把所有的测试请求,全部自动生成脚本拿来直接用,岂不是爽歪歪。
最近我老大,非要我用python。他有个特点,每当我们用哪个东西刚刚上手的时候,他就要求我们再扩展。
所以我们这里的人的特质就是:懂的很多,但是都不精通。
我的urls.txt
我的脚本就是逐行读取URL,然后把action_nameweb_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 + ‘(){‘ + " " str = str + ‘ ‘ + ‘web_url("‘ + action_name + ‘",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"URL=‘ + web_url + ‘",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"Resource=0",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"RecContentType=text/html",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"Referer=",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"Snapshot=t2.inf",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"Mode=HTTP",‘ + ‘ ‘ str = str + ‘ ‘ + ‘"Mode=HTTP",‘ + ‘ ‘ str = str + ‘ ‘ + ‘LAST);‘ + ‘ ‘ str = str + ‘ ‘ + ‘return 0;‘ + ‘ ‘ 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 + ‘(){‘ + " " str = str +‘ ‘+‘web_submit_data("‘+name+‘",‘+‘ ‘ str = str +‘ ‘+‘"Action=‘+uri+‘",‘+‘ ‘ str = str +‘ ‘+‘"Method=POST", ‘ str = str +‘ ‘+ ‘"RecContentType=text/html", ‘ str = str +‘ ‘+ ‘"Snapshot=t7.inf", ‘ str = str +‘ ‘+ ‘"Mode=HTML", ‘ str = str +‘ ‘+ ‘ITEMDATA, ‘ for param in params: str = str +‘ ‘+ ‘"Name=‘+param+‘", "Value=‘+params[param]+‘", ENDITEM, ‘ str = str +‘ ‘+ ‘LAST); ‘ str = str + ‘ ‘ + ‘return 0;‘ + ‘ ‘ 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名称,以,隔开
以上是关于自动生成LR脚本且运行的主要内容,如果未能解决你的问题,请参考以下文章