import os
import sys
def jmeterHelper():
print('请将此脚本放于jemter的bin目录下\n'
'一、请查看user.properties文件中是否有\n'
'jmeter.save.saveservice.output_format=xml\n'
'jmeter.save.saveservice.response_data=true\n'
'jmeter.save.saveservice.samplerData=true\n'
'jmeter.save.saveservice.requesteaders=true\n'
'jmeter.save.saveservice.url=true\n'
'jmeter.save.saveservice.responseHeaders=true\n'
'没有请添加到文件中末尾\n')
def changeJmeter():
try:
with open("./user.properties") as ufd:
ureadlines = ufd.readlines()
with open("./user.properties", "w+") as ufw:
# print(ureadlines)
if '#jmeter.save.saveservice.samplerData=true\n' in ureadlines or 'jmeter.save.saveservice.samplerData=true\n' in ureadlines:
print("*************")
if '#jmeter.save.saveservice.output_format=xml\n' in ureadlines:
print("目前未开启保存接口返回数据,即将开启。。。")
for line in ureadlines:
if ".save.saveservice.output_format=xml" in line:
n1 = "jmeter.save.saveservice.output_format=xml\n"
ufw.write(n1)
elif ".save.saveservice.response_data=true" in line:
n2 = "jmeter.save.saveservice.response_data=true\n"
ufw.write(n2)
elif ".save.saveservice.samplerData=true" in line:
n3 = "jmeter.save.saveservice.samplerData=true\n"
ufw.write(n3)
elif ".save.saveservice.requestHeaders=true" in line:
n4 = "jmeter.save.saveservice.requestHeaders=true\n"
ufw.write(n4)
elif ".save.saveservice.url=true" in line:
n5 = "jmeter.save.saveservice.url=true\n"
ufw.write(n5)
elif ".save.saveservice.responseHeaders=true" in line:
n6 = "jmeter.save.saveservice.responseHeaders=true\n"
ufw.write(n6)
else:
ufw.write(line)
print("user.pro off->no修改完毕")
with open("./jmeter.properties") as jfd:
jreadlines = jfd.readlines()
# print(jfd.read())
with open("./jmeter.properties", 'w+') as jfw:
for i in jreadlines:
if ".save.saveservice.response_data=" in i:
# print("11")
jn1 = "jmeter.save.saveservice.response_data=true\n"
jfw.write(jn1)
elif ".save.saveservice.samplerData=" in i:
jn2 = "jmeter.save.saveservice.samplerData=true\n"
jfw.write(jn2)
elif ".save.saveservice.output_format=" in i:
jn3 = "#jmeter.save.saveservice.output_format=csv\n"
jfw.write(jn3)
else:
jfw.write(i)
print("jmeter.pro off->on修改完毕")
print("接口返回数据保存已经打开")
else:
print("目前已经开启保存接口返回数据,即将关闭。。。")
for line in ureadlines:
if ".save.saveservice.output_format=xml" in line:
n1 = "#jmeter.save.saveservice.output_format=xml\n"
ufw.write(n1)
elif ".save.saveservice.response_data=true"in line:
n2 = "#jmeter.save.saveservice.response_data=true\n"
ufw.write(n2)
elif ".save.saveservice.samplerData=true" in line:
n3 = "#jmeter.save.saveservice.samplerData=true\n"
ufw.write(n3)
elif ".save.saveservice.requestHeaders=true" in line:
n4 = "#jmeter.save.saveservice.requestHeaders=true\n"
ufw.write(n4)
elif ".save.saveservice.url=true" in line:
n5 = "#jmeter.save.saveservice.url=true\n"
ufw.write(n5)
elif ".save.saveservice.responseHeaders=true" in line:
n6 = "#jmeter.save.saveservice.responseHeaders=true\n"
ufw.write(n6)
else:
ufw.write(line)
print("user.pro on->off修改完毕")
with open("./jmeter.properties") as jfd:
jreadlines = jfd.readlines()
with open("./jmeter.properties", 'w+') as jfw:
for i in jreadlines:
if ".save.saveservice.response_data=" in i:
jn1 = "#jmeter.save.saveservice.response_data=true\n"
jfw.write(jn1)
elif ".save.saveservice.samplerData=" in i:
jn2 = "#jmeter.save.saveservice.samplerData=true\n"
jfw.write(jn2)
elif ".save.saveservice.output_format=csv" in i:
jn3 = "jmeter.save.saveservice.output_format=csv\n"
jfw.write(jn3)
else:
jfw.write(i)
print("jmeter.pro on->off修改完毕")
print("接口返回数据保存已经关闭")
else:
jmeterHelper()
except Exception as e:
print(e)
if __name__ == "__main__":
changeJmeter()