工作背景
性能测试工程师,主要测试工具--loadrunner,主要是接口测试。
实现功能
loadrunner对报文格式的转换存在问题,部分报文无法转换,故使用Python编写脚本自动将soap协议报文转换为loadrunner默认的格式。
转换步骤
soap协议原报文如下:
1 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="URL"> 2 <soapenv:Header/> 3 <soapenv:Body> 4 <ser:queryRiskPolicyCaseInfo> 5 <!--Zero or more repetitions:--> 6 <arg0> 7 <!--Optional:--> 8 <clientInfo> 9 <!--Optional:--> 10 <batchNo>?</batchNo> 11 <!--Optional:--> 12 <size>?</size> 13 </clientInfo> 14 <!--Optional:--> 15 <riskPolicyCaseSubDto> 16 <!--Optional:--> 17 <riskPolicyCaseRequestInfo> 18 <!--Optional:--> 19 <companyCode>?</companyCode> 20 <!--Optional:--> 21 <currentPage>?</currentPage> 22 <!--Optional:--> 23 <districtCode>?</districtCode> 24 <!--Optional:--> 25 <districtLevel>?</districtLevel> 26 <!--Optional:--> 27 <endDate>?</endDate> 28 <!--Optional:--> 29 <pageSize>?</pageSize> 30 <!--Optional一页多少条:--> 31 <riskCode>?</riskCode> 32 <!--Optional风险等级:--> 33 <riskRuleCode>?</riskRuleCode> 34 <!--Optional:--> 35 <ruleType>?</ruleType> 36 <!--Optional:--> 37 <startDate>?</startDate> 38 </riskPolicyCaseRequestInfo> 39 </riskPolicyCaseSubDto> 40 </arg0> 41 </ser:queryRiskPolicyCaseInfo> 42 </soapenv:Body> 43 </soapenv:Envelope>
代码如下:
1 import re 2 """ 3 将soap格式的报文修改为loadrunner格式 4 """ 5 file = r"C:\\Users\\zg\\Desktop\\报文转换\\报文.txt" 6 file_out = r"C:\\Users\\zg\\Desktop\\报文转换\\转换后的报文.txt" 7 #报文头部 8 header = 'soap_request("StepName=google", \n"ExpectedResponse=AnySoap", \n'+'"URL=www.baidu.com'+'"'\ 9 +', \n"SOAPEnvelope= "\n"<?xml version=\\"1.0\\" encoding=\\"utf-8\\"?>"' 10 #报文尾部 11 last = '"Snapshot=t1.inf",\n"ResponseParam=result",\nLAST );' 12 with open(file_out,'w') as f_clear: 13 #写文件前清空文件 14 f_clear.truncate() 15 f_clear.write(header+'\n') 16 #确定line_num 17 line_num = 1 18 #确定文件的行数 19 numbers = len(open(file,'r').readlines()) 20 #编辑原报文 21 with open(file) as f_in: 22 for line in f_in.readlines(): 23 #判断是否为空行 24 if len(line.strip()) > 0: 25 # 找出每行第一个非空字符的位置(正则表达式) 26 num = re.search(r'\S', line).span()[0] 27 #判断是否为最后一行 28 if line_num < numbers: 29 # 对每行进行拼接 30 line = line[:num] + '"' + line[num:].replace('"', r'\"').rstrip() + '"' 31 line_num += 1 32 else: 33 line = line[:num] + '"' + line[num:].replace('"', r'\"').rstrip() + '"' + ',' 34 #在最后一行行尾添加逗号‘,’ 35 else: 36 line = '\r\t\n' 37 line_num += 1 38 #将修改后的内容写入新的文件 39 with open(file_out,'a') as f_out: 40 f_out.write(line + "\n") 41 #追加报文尾部 42 with open(file_out,'a') as f_out: 43 f_out.write(last)
运行代码,转换后的报文如下:

1 soap_request("StepName=google", 2 "ExpectedResponse=AnySoap", 3 "URL=www.baidu.com", 4 "SOAPEnvelope= " 5 "<?xml version=\"1.0\" encoding=\"utf-8\"?>" 6 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"URL\">" 7 "<soapenv:Header/>" 8 "<soapenv:Body>" 9 "<ser:queryRiskPolicyCaseInfo>" 10 "<!--Zero or more repetitions:-->" 11 "<arg0>" 12 "<!--Optional:-->" 13 "<clientInfo>" 14 "<!--Optional:-->" 15 "<batchNo>?</batchNo>" 16 "<!--Optional:-->" 17 "<size>?</size>" 18 "</clientInfo>" 19 "<!--Optional:-->" 20 "<riskPolicyCaseSubDto>" 21 "<!--Optional:-->" 22 "<riskPolicyCaseRequestInfo>" 23 "<!--Optional:-->" 24 "<companyCode>?</companyCode>" 25 "<!--Optional:-->" 26 "<currentPage>?</currentPage>" 27 "<!--Optional:-->" 28 "<districtCode>?</districtCode>" 29 "<!--Optional:-->" 30 "<districtLevel>?</districtLevel>" 31 "<!--Optional:-->" 32 "<endDate>?</endDate>" 33 "<!--Optional:-->" 34 "<pageSize>?</pageSize>" 35 "<!--Optional一页多少条:-->" 36 "<riskCode>?</riskCode>" 37 "<!--Optional风险等级:-->" 38 "<riskRuleCode>?</riskRuleCode>" 39 "<!--Optional:-->" 40 "<ruleType>?</ruleType>" 41 "<!--Optional:-->" 42 "<startDate>?</startDate>" 43 "</riskPolicyCaseRequestInfo>" 44 "</riskPolicyCaseSubDto>" 45 "</arg0>" 46 "</ser:queryRiskPolicyCaseInfo>" 47 "</soapenv:Body>" 48 "</soapenv:Envelope>", 49 "Snapshot=t1.inf", 50 "ResponseParam=result", 51 LAST );
后期会使用Tkinter模块实现其图形界面化,代码中用到正则表达式模块re,以后会有专门的文章对其进行描述,本人为Python入门菜鸟,大神请绕过,不喜勿喷!