Jmeter interface automated test reading case

1: Environment preparation

1. Download the jar package jxl.jar

2. After downloading, place it in the lib directory under the installation path of Jmeter.

3. The role of jxl.jar: complete reading, writing and modifying operations on Excel

Analysis on how to use jmter to operate excel:

1. Excel generally has three most important elements: workbook, sheet, and cell.
2. If you want to store the results in a specified cell, you must locate them according to these three elements.
3. First get the file name of excel
4, get the sheet name
5, get the coordinates of the cell
6, get the result, and write it to the corresponding cell
7. You need to use beanshell to write java code to get the corresponding data and write it to Excel. Go inside.

At the same time, I have also prepared a software testing video tutorial for everyone (including interviews, interfaces, automation, performance testing, etc.), which is below. If you need it, you can watch it directly, or you can directly click on the small card at the end of the article to get the information document for free.

Where to watch software testing video tutorials:

Byte boss teaches you how to master automated testing (interface automation/APP automation/Web automation/performance testing) within 15 days, including practical project practice

2: Preparation of test data structure

The data prepared is as follows:

(1) The test case file is named user.csv

(2) The test data file is named num.csv

Note: Create a new txt file first, and then change the file extension to csv. Do not create a new xls file and then change it to csv, otherwise the file will not be read.

1. Create a test case file, import it into CSV Data Set Config, name it test_case, and set related attributes (note the circled part)

2. Create a test data file and import it into CSV Data Set Config, name it test_data, and write the variables named tel and pwd

 

three

(1) Create a new thread group, create an http login request, and pass in the relevant parameters

(2) Create a new regular expression extractor to obtain the results returned by the http request

Four: Code preparation. After writing the code, export the jar package and name it CWResultFile.jar. Place the exported jar package into the lib--ext directory under the Jmeter installation directory. Then remember to restart Jmeter, otherwise it will not work. 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;
 
/**导入jxl.jar;*后续扩充功能,sheet2增加测试报告展现;------待实现;*/
public class CWOutputFile {
    
    public static void main(String[] args) throws RowsExceededException, WriteException, BiffException, IOException{
        CWOutputFile t=new CWOutputFile();
        String File=t.cOutputFile("测试");
    }
    
    /** wOutputFile方法写结果文件* wOutputFile(文件路径,案例编号,测试验证点,预期结果,实际结果,错误码,状态码,响应结果)*/
    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); // 根据文件创建一个操作对象
                WritableSheet readsheet = wbook.getSheet(0);
                //int rsColumns = readsheet.getColumns(); //获取Sheet表中所包含的总列数
                int rsRows = readsheet.getRows(); 
                // 获取Sheet表中所包含的总行数
                /********************************字体样式设置 ****************************/
                WritableFont font = new WritableFont(WritableFont.createFont("宋体"), 10,WritableFont.NO_BOLD);// 字体样式
                WritableCellFormat wcf = new WritableCellFormat(font);
                /***********************************************************************/
                Cell cell1 = readsheet.getCell(0, rsRows);
                if (cell1.getContents().equals("")) {
                    Label labetest1 = new Label(0, rsRows, caseNo);// 第1列--案例编号;
                    Label labetest2 = new Label(1, rsRows, testPoint); // 第2列--验证测试点;
                    Label labetest3 = new Label(2, rsRows, preResult); // 第3列--预期结果;
                    Label labetest4 = new Label(3, rsRows, fresult);// 第4列--实际结果;
                    Label labetest5 = new Label(4, rsRows, errCode);// 第5列--错误码;
                    if (preResult == fresult) {
                        result = "通过";wcf.setBackground(Colour.BRIGHT_GREEN); // 通过案例标注绿色
                        } 
                    else {result = "不通过";wcf.setBackground(Colour.RED);// 不通过案例标注红色
                    }
                    Label labetest6 = new Label(5, rsRows, result, wcf); // 第6列--执行结果;
                    Label labetest7 = new Label(6, rsRows, status); // 第7列--状态码
                    Label labetest8 = new Label(7, rsRows, respond);// 第8列--响应结果
                    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方法创建输出文件,传入参数为交易类型,如开户等;* cOutputFile方法返回文件路径,作为wOutputFile的入参;*/
    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); // 获取时间戳// 相对路径默认为 apache-jmeter-3.1\bin
                String filepath = "D:\\\\"+tradeType+"_output_" + "_" + temp_str + ".xls"; // 以时间戳命名结果文件,确保唯一
                File output = new File(filepath);
                if (!output.isFile()) {
                    output.createNewFile(); // 如果指定文件不存在,则新建该文件
                    WritableWorkbook writeBook = Workbook.createWorkbook(output);
                    WritableSheet Sheet = writeBook.createSheet("输出结果", 0); // createSheet(sheet名称,第几个sheet)
                    WritableFont headfont = new WritableFont(WritableFont.createFont("宋体"), 11, WritableFont.BOLD); // 字体样式
                    WritableCellFormat headwcf = new WritableCellFormat(headfont);
                    headwcf.setBackground(Colour.GRAY_25); // 灰色颜色
                    Sheet.setColumnView(0, 11); // 设置列宽度setColumnView(列号,宽度)
                    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); // 设置文字居中对齐方式;
                    headwcf.setVerticalAlignment(VerticalAlignment.CENTRE); // 设置垂直居中;
                    Label labe00 = new Label(0, 0, "案例编号", headwcf); // Label(列号,行号, 内容)
                    Label labe10 = new Label(1, 0, "验证测试点", headwcf);
                    Label labe20 = new Label(2, 0, "预期结果", headwcf);
                    Label labe30 = new Label(3, 0, "实际结果", headwcf);
                    Label labe40 = new Label(4, 0, "错误码", headwcf);
                    Label labe50 = new Label(5, 0, "执行结果", headwcf);
                    Label labe60 = new Label(6, 0, "返回状态", headwcf);
                    Label labe70 = new Label(7, 0, "响应结果", 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;
                }
    }

Five: Add a beanshell sampler calling code and use the controller only once (because only one Excel file needs to be exported)

t=new CWOutputFile();
String filepath=t.cOutputFile("测试");
vars.put("filepath",filepath);//转为jMeter变量,方便后期获取。


Six: Create another beanshell sampler calling code and write the data into the Excel file

s=new CWOutputFile();
String testData="{"+"\"mobilephone\":\""+"${tel}\","+"\"pwd\":\""+"${pwd}\""+"}";
String preResult=vars.get("preResult");//用get方法可以确保获取到的是字符串,里面传递的是变量名,不需要用${变量名}这种方式咯!
String fresult=vars.get("fresult");
s.wOutputFile("${filepath}", "${caseNo}","${testPoint}",testData,preResult,fresult);


Seven: Click Run and find the test result file in the D drive.

The test result file is as follows

A little help

PS: Here is a collection of self-study tutorials for software testing. It should be very helpful for those who are developing in the testing industry. In addition to basic introductory resources, bloggers also collect a lot of advanced automation resources. From theory to practice, only by integrating knowledge and action can you truly master it. The full set of content has been packaged on the network disk, and the total content is close to 500 G.

☑ 240 episodes - a complete set of video courses from zero to mastery
☑ [Courseware + Source Code] - complete supporting tutorials
☑ 18 sets - source code of practical testing projects
☑ 37 sets - testing tool software package
☑ 268 - real interview questions
☑ 200 templates - Interview resume template, test plan template, software test report template, test analysis template, test plan template, performance test report, performance test report, performance test script case template (complete information)

These materials should be the most comprehensive and complete preparation warehouse for friends who do [software testing]. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you! Everything must be done early, especially in the technical industry, where technical skills must be improved.

 

Guess you like

Origin blog.csdn.net/huace3852/article/details/132919673