Python simple operations on text files (1 ),
Background
The performance testing engineer, the main testing tool loadrunner, is mainly interface testing.
Implement Functions
Loadrunner has a problem with the conversion of message formats, and some packets cannot be converted. Therefore, you can use Python to write a script to automatically convert soap packets to the default loadrunner format.
Conversion steps
The original soap packet is as follows:
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 risk level: --> 33 <riskRuleCode>? </RiskRuleCode> 34 <! -- Optional: --> 35 <ruleType>? </RuleType> 36 <! -- Optional: --> 37 <startDate>? </StartDate> 38 </riskPolicyCaseRequestInfo> 39 </strong> 40 </arg0> 41 </ser: queryRiskPolicyCaseInfo> 42 </soapenv: Body> 43 </soapenv: Envelope>
The Code is as follows:
1 import re 2 "" 3 modify the soap format packet to loadrunner format 4 "5 file = r" C: \ Users \ zg \ Desktop \ packet conversion \ 文.txt "6 file_out = r" C: \ Users \ zg \ Desktop \ packet conversion \ 文.txt "7 # packet header 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 # Clear the file 14 f_clear.truncate () 15 f_clear.write (header + '\ n') before writing the file ') 16 # determine line_num17 line_num = 118 # determine the number of lines of the file 19 numbers = len (open (file, 'R '). readlines () 20 # edit the original packet 21 with open (file) as f_in: 22 for line in f_in.readlines (): 23 # determine whether the row is empty 24 if len (line. strip ()> 0: 25 # locate the position of the first non-null character in each line (Regular Expression) 26 num = re. search (R' \ s', line ). span () [0] 27 # determine whether the last line is 28 if line_num <numbers: 29 # concatenate 30 line = line [: num] + '"' + line [num:]. replace ('"', R '\"'). rstrip () + '"'31 line_num + = 132 else: 33 line = line [: num] +'" '+ line [num:]. replace ('"', R '\"'). rstrip () + '"' + ', '34 # Add a comma', '35 else at the end of the last line: 36 line = '\ r \ t \ n' 37 line_num + = 138 # Write the modified content to the new file 39 with open (file_out, 'A') as f_out: 40 f_out.write (line + "\ n") 41 # append message tail 42 with open (file_out, 'A') as f_out: 43 f_out.write (last)
Run the code. The converted message is as follows:
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 = \ "external" 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 risk level: --> "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 );
View Code
The Tkinter module will be used later to implement its graphical interface. The regular expression module re will be used in the Code. In the future, there will be a special article to describe it. I am a beginner in Python. Please bypass it, don't like it, don't spray it!