当前位置:网站首页>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 !!!!
边栏推荐
- Unity and webgl love each other
- Cause analysis and solution of too laggy page of [test interview questions]
- What is ADC sampling rate (Hz) and how to calculate it
- 肠道里的微生物和皮肤上的一样吗?
- LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]
- PCL . VTK files and Mutual conversion of PCD
- Debezium series: binlogreader for source code reading
- This time, let's clear up: synchronous, asynchronous, blocking, non blocking
- QT graphicsview graphical view usage summary with flow chart development case prototype
- 定位到最底部[通俗易懂]
猜你喜欢

Transform XL translation

GBU1510-ASEMI电源专用15A整流桥GBU1510

Knowledge drop - PCB manufacturing process flow

Ligne - raisonnement graphique - 4 - classe de lettres

iNFTnews | NFT技术的广泛应用及其存在的问题
![Leetcode interview question 02.07 Linked list intersection [double pointer]](/img/a5/58b4735cd0e47f1417ac151a1bcca4.jpg)
Leetcode interview question 02.07 Linked list intersection [double pointer]
![LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]](/img/5e/e442c8649b9123a9d9df7c0d61a564.jpg)
LeetCode142. Circular linked list II [two pointers, two methods for judging links in the linked list and finding ring points]

今日创见|企业促进创新的5大关键要素

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

十三、系统优化
随机推荐
Cause analysis and solution of too laggy page of [test interview questions]
Software test classification
行測-圖形推理-4-字母類
[language programming] exe virus code example
Unity与WebGL的相爱相杀
开发那些事儿:Go加C.free释放内存,编译报错是什么原因?
2022 words for yourself
定位到最底部[通俗易懂]
Quelles sont les similitudes et les différences entre les communautés intelligentes et les villes intelligentes?
GEE(四):计算两个变量(影像)之间的相关性并绘制散点图
微信论坛交流小程序系统毕业设计毕设(6)开题答辩PPT
It's no exaggeration to say that this is the most user-friendly basic tutorial of pytest I've ever seen
Guessing game (read data from file)
Byte hexadecimal binary understanding
iNFTnews | Web5 vs Web3:未来是一个过程,而不是目的地
Use JfreeChart to generate curves, histograms, pie charts, and distribution charts and display them to jsp-2
Unity 动态合并网格纹理
今日创见|企业促进创新的5大关键要素
微信论坛交流小程序系统毕业设计毕设(3)后台功能
Why is network i/o blocked?