当前位置:网站首页>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 !!!!

原网站

版权声明
本文为[Test Architect Bei fan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207072001386675.html