当前位置:网站首页>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 !!!!
边栏推荐
- Microbial Health Network, How to restore Microbial Communities
- kubernetes的简单化数据存储StorageClass(建立和删除以及初步使用)
- 网络安全-对操作系统进行信息查询
- 开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
- Early childhood education industry of "screwing bar": trillion market, difficult to be a giant
- The wonderful relationship between message queue and express cabinet
- Digital collections accelerated out of the circle, and marsnft helped diversify the culture and tourism economy!
- The author of LinkedList said he didn't use LinkedList himself
- Years of summary, some core suggestions for learning programming
- About idea cannot find or load the main class
猜你喜欢

Lecture 30 linear algebra Lecture 5 eigenvalues and eigenvectors

安踏DTC | 安踏转型,构建不只有FILA的增长飞轮

Cases of agile innovation and transformation of consumer goods enterprises

PCL .vtk文件与.pcd的相互转换

GEE(四):计算两个变量(影像)之间的相关性并绘制散点图

Line test - graphic reasoning - 6 - similar graphic classes

Sword finger offer 55 - I. depth of binary tree

The author of LinkedList said he didn't use LinkedList himself

面试百问:如何测试App性能?

十三、系统优化
随机推荐
V20变频器手自动切换(就地远程切换)的具体方法示例
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
Line test - graphic reasoning - 2 - black and white lattice class
每日一题——PAT乙级1002题
网络安全-联合查询注入
Develop those things: go plus c.free to free memory, and what are the reasons for compilation errors?
知识点滴 - PCB制造工艺流程
Advantages and disadvantages of rest ful API
Years of summary, some core suggestions for learning programming
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
iNFTnews | NFT技术的广泛应用及其存在的问题
Class implementation of linear stack and linear queue (another binary tree pointer version)
GEE(三):计算两个波段间的相关系数与相应的p值
GBU1510-ASEMI电源专用15A整流桥GBU1510
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
Anta DTC | Anta transformation, building a growth flywheel that is not only FILA
This time, let's clear up: synchronous, asynchronous, blocking, non blocking
聊聊 Dart 的空安全 (null safety) 特性
Mitsubishi PLC SLmP (MC) protocol
Line measurement - graphic reasoning -9- line problem class