Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

background

To build an interface automation platform, it is debugged under Windows and finally run in Linux environment, so there are differences in saving the result path, so in order to run in both sides of the environment, the path slash needs to be parameterized; Java supports calling system functions, getting the system properties currently being executed, and deciding which path variables to use. Beanshell scripting, similar to Python, allows you to define functions directly, using the system’s own class methods.

Beanshell coding

Open JMeter to create a thread group and add a Beanshell handler

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

// Java scripts can be decorated without class classes and can write methods directly
//public class AppendFile {
// Writes the content to the specified file fileName
public static void appendFile(String fileName, String content) throws IOException{
 FileWriter writer = null;
try {
 writer = new FileWriter(fileName, true); //true to append records to newlines,false to replace each time
 content = content + "\n";
 writer.write(content);
 } catch(IOException e){
 e.printStackTrace();
 }finally{
 if(writer ! =null){ writer.close(); }}}/ /}
// Check whether the current system is Linux
public static boolean isLinux(a) {
return System.getProperties().getProperty("os.name").toLowerCase().indexOf("linux") > =0;
}
// system.out. println("****** write file start*******"); // system.out. println("****** write file start*******");
//xxx,userTypr,nickName,account,passwd
String getdata1 = vars.get("caseid");
String getdata2 = vars.get("title"); // Username is the variable name obtained from sample, which can be modified according to actual conditions.
String getdata3 = vars.get("params");
String getdata4 = vars.get("requri");
//String getdata5 = vars.get("A");
//String getdata6 = vars.get("B");
//String getdata7 = vars.get("C");
String expected=vars.get("expect");
String res=prev.getResponseDataAsString();
if(! res.contains(expected)){ Failure=true;
		FailureMessage = "Response result Don't contains keywords:"+expected+", assertion failed! Expected results are inconsistent with reality!";
		if (isLinux()){
		String filePath = "/var/lib/jenkins/workspace/Hcp_Api_Auto/errorData.csv";
		// Want to write the result of each assertion failure to the CSV table
		appendFile(filePath,getdata1+', '+getdata2+', '+getdata3+', '+getdata4+', '+FailureMessage);
		}else{
		String filePath = "D:/errorData.csv";
		// Want to write the result of each assertion failure to the CSV table
		appendFile(filePath,getdata1+', '+getdata2+', '+getdata3+', '+getdata4+', '+FailureMessage); }}else{
		FailureMessage = prev.getResponseDataAsString() +"contains keywords: "+expected+"Declare success! Expected results are in line with actual results!";
	}
//log.info(FailureMessage);
Println ("****** write file end*******"); // system.out. println("****** write file end*******");
Copy the code

There is a problem with scripting. When wrapping methods in IDE tools, some exceptions are thrown directly without a try... If the jar is imported into the JMeter environment, you will need to try the beanshell script... The catch?

guess

In IDE tools, try-catch is required to handle the exception when JMeter introduces the call. If throws an exception, no processing is required when the encapsulated method is called.