beanshell practice --- import external jar package
step:
1.Eclipse ready to test the next class, as follows
Test Package; public class TestBeanShell { public static String testHello (username String, String password) { IF (! username.equals ( "Clover")) { return "user name is incorrect"; } the else IF (password.equals ( "456! ')) { return "wrong password"; } the else { return "Congratulations, successful landing";! } } }
2. Export jar package, right -export-JAR-test.jar
================================================== = Jmeter related =============================================== ================================
3. Under the derived jmeter bag on the jar lib \ ext: E: \ WorkSpace \ Jmeter \ apache-jmeter-4.0 \ lib \ ext
4. Open jmeter, introduced in test plans test.jar inside, and at the same time add user-defined variables, beanshell sample, see the number of results
The variable definition defines the user-defined user passwd
6. Thread group - Right-added -beanshell sample
7. beanshell write code to get a user name and password, and passed to the advance packaged class, and return results after login
Import Test *. ; // static method call does not require striking instantiated // Note Note semicolon semicolon semicolon Note A = vars.get ( "User" ); B = vars.get ( "Pass" ); String C = TestBeanShell.testHello (A, B); log.info (C);
8. Run success
Summarizes the problems encountered and the
1.beanshell write code that did not add a semicolon; ============= error has led to unfamiliar syntax for JAVA
2.beanshell common functions
- vars.get ( "user") as the value of the above acquisition variable user-defined variables user, user wants to obtain a value corresponding clover, used as such: vars.get ( "user") result clover
- vars.put ( "v1", "$ {user}") results $ {user} is clover, this sentence is to assign clover i.e. v1 v1 = "clover"
- log.info ( "==========") print log
3. The following three methods can be taken to various values of the user name and password
//String result = TestBeanShell.testHello("${user}", "${pass}"); //vars.put("v1",bsh.args[0]) vars.put("v1",bsh.args[1]); 在beanshhell 定义变量的时候要用空格隔开 ${user} ${pass} 空格隔开空格隔开空格隔开
//vars.ger("user") vars.get("pass")
4.进阶版
import test.*
vars.put("v1","${user}"); vars.put("v2","${pass}"); vars.put("v3",Parameters); String result = TestBeanShell.testHello(vars.get("v1"), vars.get("v2")); log.info(result); String name = vars.get("v1"); String pass = vars.get("v2"); String params = vars.get("v3"); log.info("输入用户名=" + name); log.info("输入密码=" + pass); log.info("params=" + params); //log.info("bsh.args="+ Arrays.toString(bsh.args)); int bshArgs = bsh.args.length; log.info("bsh.args数组长度="+Integer.toString(bshArgs)); for(int i = 0 ; i< bshArgs ; i++){ log.info("bsh.args第"+i+"个元素=" + bsh.args[i]); }
5.多实践多实践多实践!!!