LoadRunner使用Java代码编写脚本

在录制脚本时可以选择Java Vuser方式来编写代码实现,有几个注意点:
在脚本编辑界面按F4的Run-time Settings,可以设置JRE和ClassPath路径;
Actions类中使用的class包括Run-time Settings中设置的类路径、JRE下的类以及HP\LoadRunner\classes下的lr相关类;对于初始化的信息需要在默认构造器上做,不能使用static静态块;


/*
* LoadRunner Java script. (Build: _build_number_)

* Script Description: 
*                     
*/
import lrapi.lr;

public class Actions
{

        private JmsSender send = new JmsSender();

        public Actions(){
                send.init();
        }

        public int init() throws Throwable {
                return 0;
        }//end of init


        public int action() throws Throwable {
                lr.start_transaction("s");
                lr.output_message("开始发送消息");
                try {
                  send.sendMessage("消息内容");
                  lr.end_transaction("s", lr.PASS);
                } catch (Exception e) {
                  lr.end_transaction("s", lr.FAIL);
                }
                lr.output_message("发送消息成功");
                return 0;
        }//end of action


        public int end() throws Throwable {
                return 0;
        }//end of end


}


 

你可能感兴趣的:(loadrunner)