当前位置:网站首页>JMeter interface automated test read case, execute and write back result
JMeter interface automated test read case, execute and write back result
2022-07-07 23:10:00 【Test Architect Bei fan】
Preface :
【 At the end of the article, a lot of benefits are prepared for you 】
One : Environmental preparation
1. download jxl.jar This jar package
2. Once you download it , Put it in Jmeter Under the installation path of lib Under the table of contents
3.jxl.jar The role of : Finish right Excel Read, write, and modify operations
How to use it jmter operation excel Analysis of the thinking of :
1,Excell There are generally three most important elements :workbook,sheet,cell
2, Want to save the results to the specified cell , Then it must be located according to these three elements .
3, First get excel File name for
4, Get form name
5, Get the coordinates of the cell
6, To get the results , Write to the corresponding cell
7, Need to use beanshell Write java Code , Get the corresponding data and write it to Excel Go inside .
Two : Test data structure preparation
The data prepared are as follows :(1) The test case file is named user.csv (2) The test data file is named num.csv
Be careful : First new txt file , Then change the file extension to csv, Don't create new xls Change it to csv, Otherwise, the file cannot be read
1. Create a test case file , And import to CSV Data Set Config, Name it test_case, And set the relevant properties ( Pay attention to the circled part )
2. Create test data file , And import to CSV Data Set Config, Name it test_data, And write a variable named tel,pwd
3、 ... and :(1) New thread group , Create a login request http, And pass in the relevant parameters
(2) Create a new regular expression extractor , obtain http Results returned by request
Four : Code preparation , After writing the code , export jar package , Name it CWResultFile.jar, Will export jar Put the bag in Jmeter Under the installation path directory lib--ext Under the table of contents , Then remember to restart Jmeter, Otherwise, it will not take effect
import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.Date; import jxl.Cell; import jxl.Workbook; import jxl.format.Alignment; import jxl.format.Colour; import jxl.format.VerticalAlignment; import jxl.read.biff.BiffException; import jxl.write.Label; import jxl.write.WritableCellFormat; import jxl.write.WritableFont; import jxl.write.WritableSheet; import jxl.write.WritableWorkbook; import jxl.write.WriteException; import jxl.write.biff.RowsExceededException; /** Import jxl.jar;* Subsequent expansion functions ,sheet2 Add test report display ;------ To be realized ;*/ public class CWOutputFile { public static void main(String[] args) throws RowsExceededException, WriteException, BiffException, IOException{ CWOutputFile t=new CWOutputFile(); String File=t.cOutputFile(" test "); } /** wOutputFile Method to write the result file * wOutputFile( File path , The case number , Test verification point , Expected results , The actual result , Error code , Status code , In response to the results )*/ public void wOutputFile(String filepath, String caseNo,String testPoint, String preResult, String fresult, String errCode,String status, String respond) throws IOException,RowsExceededException, WriteException, BiffException { File output = new File(filepath); String result = ""; InputStream instream = new FileInputStream(filepath); Workbook readwb = Workbook.getWorkbook(instream); WritableWorkbook wbook = Workbook.createWorkbook(output, readwb); // Create an operation object from the file WritableSheet readsheet = wbook.getSheet(0); //int rsColumns = readsheet.getColumns(); // obtain Sheet The total number of columns contained in the table int rsRows = readsheet.getRows(); // obtain Sheet The total number of rows contained in the table /******************************** Font style settings ****************************/ WritableFont font = new WritableFont(WritableFont.createFont(" Song style "), 10,WritableFont.NO_BOLD);// Font style WritableCellFormat wcf = new WritableCellFormat(font); /***********************************************************************/ Cell cell1 = readsheet.getCell(0, rsRows); if (cell1.getContents().equals("")) { Label labetest1 = new Label(0, rsRows, caseNo);// The first 1 Column -- The case number ; Label labetest2 = new Label(1, rsRows, testPoint); // The first 2 Column -- Verify test points ; Label labetest3 = new Label(2, rsRows, preResult); // The first 3 Column -- Expected results ; Label labetest4 = new Label(3, rsRows, fresult);// The first 4 Column -- The actual result ; Label labetest5 = new Label(4, rsRows, errCode);// The first 5 Column -- Error code ; if (preResult == fresult) { result = " adopt ";wcf.setBackground(Colour.BRIGHT_GREEN); // Mark Green through the case } else {result = " Not through ";wcf.setBackground(Colour.RED);// Do not mark red through the case } Label labetest6 = new Label(5, rsRows, result, wcf); // The first 6 Column -- Execution results ; Label labetest7 = new Label(6, rsRows, status); // The first 7 Column -- Status code Label labetest8 = new Label(7, rsRows, respond);// The first 8 Column -- In response to the results readsheet.addCell(labetest1); readsheet.addCell(labetest2); readsheet.addCell(labetest3); readsheet.addCell(labetest4); readsheet.addCell(labetest5); readsheet.addCell(labetest6); readsheet.addCell(labetest7); readsheet.addCell(labetest8); } wbook.write(); wbook.close(); } /** cOutputFile Method to create an output file , The incoming parameter is transaction type , Such as opening an account, etc ;* cOutputFile Method returns the file path , As wOutputFile Input ;*/ public String cOutputFile(String tradeType) throws IOException, WriteException { String temp_str = ""; Date dt = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); temp_str = sdf.format(dt); // Get the timestamp // The relative path defaults to apache-jmeter-3.1\bin String filepath = "D:\\\\"+tradeType+"_output_" + "_" + temp_str + ".xls"; // Name the result file with a timestamp , Ensure uniqueness File output = new File(filepath); if (!output.isFile()) { output.createNewFile(); // If the specified file does not exist , Then create a new file WritableWorkbook writeBook = Workbook.createWorkbook(output); WritableSheet Sheet = writeBook.createSheet(" Output results ", 0); // createSheet(sheet name , Number one sheet) WritableFont headfont = new WritableFont(WritableFont.createFont(" Song style "), 11, WritableFont.BOLD); // Font style WritableCellFormat headwcf = new WritableCellFormat(headfont); headwcf.setBackground(Colour.GRAY_25); // Gray color Sheet.setColumnView(0, 11); // Set column width setColumnView( Column number , Width ) Sheet.setColumnView(1, 30); Sheet.setColumnView(2, 35); Sheet.setColumnView(3, 35); Sheet.setColumnView(4, 18); Sheet.setColumnView(5, 11); Sheet.setColumnView(6, 11); Sheet.setColumnView(7, 50); headwcf.setAlignment(Alignment.CENTRE); // Set text center alignment ; headwcf.setVerticalAlignment(VerticalAlignment.CENTRE); // Set vertical center ; Label labe00 = new Label(0, 0, " The case number ", headwcf); // Label( Column number , Line number , Content ) Label labe10 = new Label(1, 0, " Verify test points ", headwcf); Label labe20 = new Label(2, 0, " Expected results ", headwcf); Label labe30 = new Label(3, 0, " The actual result ", headwcf); Label labe40 = new Label(4, 0, " Error code ", headwcf); Label labe50 = new Label(5, 0, " Execution results ", headwcf); Label labe60 = new Label(6, 0, " Return to status ", headwcf); Label labe70 = new Label(7, 0, " In response to the results ", headwcf); Sheet.addCell(labe00); Sheet.addCell(labe10); Sheet.addCell(labe20); Sheet.addCell(labe30); Sheet.addCell(labe40); Sheet.addCell(labe50); Sheet.addCell(labe60); Sheet.addCell(labe70); writeBook.write(); writeBook.close(); } return filepath; } }
5、 ... and : Add one beanshell The sampler calls the code , And use the controller only once ( Because you only need to export one Excel file )
t=new CWOutputFile(); String filepath=t.cOutputFile(" test "); vars.put("filepath",filepath);// To jMeter Variable , Convenient for later access .
6、 ... and : Create another beanshell The sampler calls the code , Write data to Excel In file
s=new CWOutputFile(); String testData="{"+"\"mobilephone\":\""+"${tel}\","+"\"pwd\":\""+"${pwd}\""+"}"; String preResult=vars.get("preResult");// use get Method can ensure that the obtained string is , What is passed in is the variable name , Don't need to use ${ Variable name } This way ! String fresult=vars.get("fresult"); s.wOutputFile("${filepath}", "${caseNo}","${testPoint}",testData,preResult,fresult);
7、 ... and : Click on the run , stay D Find the test result file on the disk
The test result file is as follows
a key : Learning materials of course, learning is inseparable from materials , Of course, here is also prepared for you 600G Learning materials
Required first Focus on Then private my keyword 【000】 Get it for free Note that the keywords are :000
doubt : Why pay attention first ? return : Because if you don't pay attention, you can't see the private letter
Project practice
app project , Bank Project , Medical Project , Online retailers , Finance
Large scale e-commerce projects
Full set of software test automation test teaching video
300G Download tutorial materials 【 Video tutorial +PPT+ Project source code 】
A complete set of software testing automation testing factory has been
python automated testing ++ A complete set of templates + Performance testing
It's said that the iron juice who has paid attention to me for three consecutive years has been promoted, raised and made a fortune !!!!
边栏推荐
- Installing vmtools is gray
- 肠道里的微生物和皮肤上的一样吗?
- LeetCode206. Reverse linked list [double pointer and recursion]
- Some parameters of Haikang IPC
- Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
- 行测-图形推理-1-汉字类
- 智慧社區和智慧城市之間有什麼异同
- Line test graph reasoning graph group class
- Years of summary, some core suggestions for learning programming
- Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
猜你喜欢
Line test - graphic reasoning - 2 - black and white lattice class
十四、数据库的导出和导入的两种方法
微生物健康網,如何恢複微生物群落
What is fake sharing after filling the previous hole?
LeetCode707. Design linked list
十三、系统优化
数据库每日一题---第22天:最后一次登录
Sword finger offer 28 Symmetric binary tree
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
【刷题记录】3. 无重复字符的最长子串
随机推荐
Mitsubishi PLC SLmP (MC) protocol
Debezium series: introducing support for the final operator
One question per day - pat grade B 1002 questions
GBU1510-ASEMI电源专用15A整流桥GBU1510
网络安全-钓鱼
Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
Ligne - raisonnement graphique - 4 - classe de lettres
DTC社群运营怎么做?
网格(Grid)
Database daily question --- day 22: last login
Leetcode interview question 02.07 Linked list intersection [double pointer]
What does the model number of asemi rectifier bridge kbpc1510 represent
Cause analysis and solution of too laggy page of [test interview questions]
微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
Transparent i/o model from beginning to end
网络安全-对操作系统进行信息查询
V20变频器手自动切换(就地远程切换)的具体方法示例
线上面试,该如何更好的表现自己?这样做,提高50%通过率~
PMP project management exam pass Formula-1
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2