jmeter downloads the PDF to the local and clears the test data

 

  1. 【Execution test】>>>>Get download PDF address
  2. Use a JSON extractor to extract the URL in the response data.
  3. Intercept the URL suffix path. (BeanShell post-processing program)
    import org.apache.commons.lang.StringUtils;
    String ss=StringUtils.substringBeforeLast("${downloadurl}", "/") ;
    String sss=StringUtils.substringAfterLast("${downloadurl}", "/") ;
    String ssss=StringUtils.substringAfter(ss, "//") ;
    vars.put("downurl",ssss);
    vars.put("downurlpath",sss);
    System.out.println(ss);
    System.out.println(sss);
    System.out.println(ssss);

     

  4.  【Execute test】>>>Download PDF and verify

  5.  

    Write download content into PDF file (BeanShell post-processing program)

    import java.io.*;
    byte[] result = prev.getResponseData();
    String file_name = "C:/Users/Administrator/Downloads/test/Invitation_Parent_${test_centername}${__time(yyyy-MM-dd)}en.pdf";
    File file = new File(file_name);
    FileOutputStream out = new FileOutputStream(file);
    out.write(result);
    out.close();

     

  6. Assert whether the downloaded file exists, exist, delete the file (BeanShell post-processing program)

    import java.io.File; 
    import java.io.FileNotFoundException; 
    import java.io.FileWriter; 
    import java.io.IOException; 
    import java.io.RandomAccessFile; 
    
         StringBuffer fileBuf=new StringBuffer(); 
                String filePar = "C:/Users/Administrator/Downloads/test/Invitation_Parent_${test_centername}${__time(yyyy-MM-dd)}en.pdf";
                File myPath = new File( filePar ); 
                if(!myPath.exists())
                  {
                      	Failure=true;
                      	FailureMessage = "error, check";
                   }
                   else
                  {
    				Failure=false;
                      	FailureMessage = "ok!";
    				myPath.delete();
                          }

     

 

Guess you like

Origin blog.csdn.net/cheerlh2018/article/details/107081712