当前位置:网站首页>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 !!!!
边栏推荐
- Debezium series: support the use of variables in the Kill Command
- Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors
- Txt file virus
- Understand the session, cookie and token at one time, and the interview questions are all finalized
- 嵌入式音频开发中的两种曲线
- GBU1510-ASEMI电源专用15A整流桥GBU1510
- Circumvention Technology: Registry
- DTC社群运营怎么做?
- Txt file virus
- Sword finger offer 63 Maximum profit of stock
猜你喜欢

Brush question 4

Software test classification

数据库每日一题---第22天:最后一次登录

What does the model number of asemi rectifier bridge kbpc1510 represent

Line test - graphic reasoning - 4 - alphabetic class

U盘拷贝东西时,报错卷错误,请运行chkdsk

Anta DTC | Anta transformation, building a growth flywheel that is not only FILA

PMP项目管理考试过关口诀-1

Sword finger offer 55 - I. depth of binary tree

Introduction to redis and jedis and redis things
随机推荐
智慧社區和智慧城市之間有什麼异同
Leetcode94. Middle order traversal of binary trees
Basic knowledge of binary tree
U盘拷贝东西时,报错卷错误,请运行chkdsk
聊聊 Dart 的空安全 (null safety) 特性
数据库每日一题---第22天:最后一次登录
De la famille debezium: SET ROLE statements supportant mysql8
JMeter-接口自动化测试读取用例,执行并结果回写
Personal statement of testers from Shuangfei large factory: is education important for testers?
每日一题——PAT乙级1002题
消息队列与快递柜之间妙不可言的关系
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades-KDD2020
三菱PLC slmp(mc)协议
oc 可变參数传递
[untitled] reprint melting ice - track icedid server with a few simple steps
This time, let's clear up: synchronous, asynchronous, blocking, non blocking
Understand the session, cookie and token at one time, and the interview questions are all finalized
Line test - graphic reasoning - 4 - alphabetic class
Why is network i/o blocked?
The author of LinkedList said he didn't use LinkedList himself