(Jmeter notes) Beanshell grammar and common built-in objects

Reprinted https://blog.csdn.net/hujyhfwfh2/article/details/80862134

1: Beanshell basic syntax 1.1: Beanshell For loop the set A = String "AAA"; int B = 111;
List List new new = the ArrayList (); // save the only set of type String List list.add (a); list.add (b.toString ()); // common wording for loop for (int i = 0; i <list.size (); i ++) {log.info ( ">>>>>>>>> >> "); log.info (list.get ( i));} // written for for reinforcing loop (object: list) {log.info ( " ========== "); log.info (object);} 1.2: writing the array Beanshell String [] str1 = new String [ ] { "aa", "bb", "cc"}; // new a String [] a one-dimensional array
String [ ] [] str2 = new String [ ] [] {{ "AA", "BB"}, { "CC", "DD"}, { "EE", "FF"}}; // str1 traversing the one-dimensional array for (int i = 0; i <str1.length; i ++) {log.info (str1 [i]);} // second 2D array traversal for (String [] strings: str2) {
for (int 0 = I; I <strings.length; I ++) {
log.info (strings [I]);}}. 1.3: Beanshell // definition of a function call and define a void method addList public (List List) {
List.add ( "AAA"); List.add ( "BBB"); List.add ( "CCC");}
List list = new ArrayList (); // call the method addlist (list); // set traversal list for (int i = 0; i <list.size (); i ++) {log.info (list.get (i) );} 2: Beanshell commonly used built-in objects 2.1: bsh.args object to obtain an array of parameters



2.2: vars object built with a: example of defining a user-defined variables


B: vars.get (String key) Get String variable 
   vars.put (String key, Value) is placed into a Map VARS 


2.3: VARS built-in objects put and get Object If the object is placed or need to obtain a non-data need to use the putObject type String () and the getObject ()
// BeanShell vars.putObject () vars.getObject () = new new use of the ArrayList List List ();
List.add ( "AAA"); List.add ( "BBB"); // the putObject () Object type objects can be placed vars.putObject ( "list of objects in the vars ", list); // =========================== // Get Object list Object Object Value = vars.getObject (" list " ); // log.info only output variable of type String log.info (value.toString ());



 . 3: the conventional built-in objects -cxt3.1 Beanshell: cxt built-in objects getPreviousResult () method invocation request / response data
cxt built-in objects from JMeterContext See Jmeter API found the following method to obtain the get request / response
// SampleResult import the necessary objects org.apache.jmeter.samplers.SampleResult import;
SampleResult ctx.getPreviousResult Result = (); // getRequestHeaders ( ) method returns a string string string requestHeaders = result.getRequestHeaders (); // getResponseHeaders () returns the response headersString ResponseHeaders = result.getResponseHeaders () // getResponseCode () returns the response status code string string responseCode = result.getResponseCode (); // getURL () returns the requested URL object URL url = result.getURL (); log.info (requestHeaders); log.info (requestHeaders); 4: prev prev built-in object is built-in objects corresponding to the post-processor Beanshell ctx .getPreviousResult (); ---------------- Disclaimer: This article is CSDN blogger "AnndyTuo 'original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement. Original link: https: //blog.csdn.net/hujyhfwfh2/article/details/80862134

Guess you like

Origin www.cnblogs.com/worldbugMsg/p/12148807.html