使用Java从API创建Jmeter脚本时,如何在网站的搜索框中传递值/关键字/参数?

时间:2018-10-03 11:21:42

标签: jmeter

我已经使用Java通过API创建了Jmeter脚本。
我想模拟在电子商务网站的搜索框中搜索值/关键字/参数的操作。

以下脚本由以下采样器组成:

  1. 加载网址(调用示例电子商务网址)
  2. 登录(使用AuthManager来使用登录凭据)
  3. 搜索(在示例网站的搜索框中搜索值/关键字/参数)

这是我的代码段:

package com.blazemeter.demo;

import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.protocol.http.control.AuthManager;
import org.apache.jmeter.protocol.http.control.Authorization;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
import org.apache.jmeter.protocol.http.gui.AuthPanel;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
import org.apache.jmeter.reporters.ResultCollector;
import org.apache.jmeter.reporters.Summariser;
import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jmeter.testelement.TestPlan;
import org.apache.jmeter.threads.ThreadGroup;
import org.apache.jmeter.threads.gui.ThreadGroupGui;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jorphan.collections.HashTree;
import java.io.FileOutputStream;


public class JMeterFromAPI {

  public static void main(String[] args) throws Exception {

      String jmeterHome = "Path of Jmeter Home";
      StandardJMeterEngine jmeter = new StandardJMeterEngine();

      //JMeter initialization (properties, log levels, locale, etc)
      JMeterUtils.setJMeterHome(jmeterHome);
      JMeterUtils.loadJMeterProperties("Path of Jmeter Properties Files");
      JMeterUtils.initLocale();

      // JMeter Test Plan, basically JOrphan HashTree
      HashTree testPlanTree = new HashTree();

      // HTTP Sampler - Load URL
      HTTPSamplerProxy LoadUrl = new HTTPSamplerProxy();
      LoadUrl.setDomain("www.samplewebsite.com");
      LoadUrl.setPort(80);
      LoadUrl.setPath("/");
      LoadUrl.setMethod("GET");
      LoadUrl.setName("Load Url");
      LoadUrl.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
      LoadUrl.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

      // Create HTTP Authorization Manager
      AuthManager manager = new AuthManager();
      Authorization authorization = new Authorization();
      authorization.setURL("https://www.samplewebsite.com");
      authorization.setUser("sampleusername");
      authorization.setPass("samepassword");
      manager.addAuth(authorization);
      manager.setName(JMeterUtils.getResString("auth_manager_title")); // $NON-NLS-1$
      manager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName());
      manager.setProperty(TestElement.GUI_CLASS, AuthPanel.class.getName());

      // HTTP Sampler - Login
      HTTPSamplerProxy Login = new HTTPSamplerProxy();
      Login.setDomain("www.samplewebsite.com");
      Login.setPort(80);
      Login.setPath("/account/login");
      Login.setMethod("GET");
      Login.setName("Login");
      Login.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
      Login.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

      // HTTP Sampler - Search
      HTTPSamplerProxy Search = new HTTPSamplerProxy();
      Search.setDomain("www.samplewebsite.com"");
      Search.setPort(80);
      Search.setPath("/search?q=mobiles");
      Search.setMethod("GET");
      Search.setName("Search");
      Search.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
      Search.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());

      // Loop Controller
      LoopController loopController = new LoopController();
      loopController.setLoops(1);
      loopController.setFirst(true);
      loopController.setProperty(TestElement.TEST_CLASS, LoopController.class.getName());
      loopController.setProperty(TestElement.GUI_CLASS, LoopControlPanel.class.getName());
      loopController.initialize();

      // Thread Group
      ThreadGroup threadGroup = new ThreadGroup();
      threadGroup.setName("Thread Group");
      threadGroup.setNumThreads(3);
      threadGroup.setRampUp(1);
      threadGroup.setSamplerController(loopController);
      threadGroup.setProperty(TestElement.TEST_CLASS, ThreadGroup.class.getName());
      threadGroup.setProperty(TestElement.GUI_CLASS, ThreadGroupGui.class.getName());

      // Test Plan
      TestPlan testPlan = new TestPlan("Ecommerce Website");
      testPlan.setProperty(TestElement.TEST_CLASS, TestPlan.class.getName());
      testPlan.setProperty(TestElement.GUI_CLASS, TestPlanGui.class.getName());
      testPlan.setUserDefinedVariables((Arguments) new ArgumentsPanel().createTestElement());

      // HTTP Request Sampler and Header Manager
      HashTree httpRequestTree = new HashTree();
      httpRequestTree.add(Login, manager);

      // Construct Test Plan from previously initialized elements
      testPlanTree.add(testPlan);
      HashTree threadGroupHashTree = testPlanTree.add(testPlan, threadGroup);
      threadGroupHashTree.add(LoadUrl);
      threadGroupHashTree.add(httpRequestTree);
      threadGroupHashTree.add(Search);

      SaveService.saveTree(testPlanTree, new FileOutputStream(jmeterHome + "/bin/test.jmx"));

      //Add Summarizer output
      Summariser summer = null;
      String summariserName = JMeterUtils.getPropDefault("summariser.name", "summary");
      if (summariserName.length() > 0) {
          summer = new Summariser(summariserName);
      }

      // Store Execution Results into a .jtl file
      String logFile = jmeterHome + "/bin/result.jtl";
      ResultCollector logger = new ResultCollector(summer);
      logger.setFilename(logFile);
      testPlanTree.add(testPlanTree.getArray()[0], logger);

      // Run Test Plan
      jmeter.configure(testPlanTree);
      jmeter.run();

      System.exit(0);
  }
}

因此,我想使用搜索采样器为示例网站上提供的搜索框搜索值/关键字/参数。

1 个答案:

答案 0 :(得分:0)

  1. 您很有可能无法使用HTTP Authorization Manager登录,因为它旨在处理外部身份验证系统,例如Basic,{{3} }和NTLM。如果您需要在Kerberos中输入凭据,则很可能需要使用HTML Form采样器并发送带有凭据的HTTP Request请求。

    例如此代码:

    HTTPSamplerProxy loginSampler = new HTTPSamplerProxy();
    loginSampler.setDomain("example.com");
    loginSampler.setPort(80);
    loginSampler.setPath("/login");
    loginSampler.setMethod("POST");
    loginSampler.setName("Perform Login");
    loginSampler.addArgument("username", "johndoe");
    loginSampler.addArgument("password", "secret");
    loginSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
    loginSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
    

    将导致以下配置:

    POST

  2. 关于搜索词-我的期望是您需要坚持相同的方法。

通常不建议/不支持使用API​​创建JMeter测试计划,因此我建议采用以下替代方法:

  1. 使用JMeter的JMeter POST request from API
  2. 记录您的测试计划框架
  3. 根据需要执行HTTP(S) Test Script Recorder
  4. 使用correlation侦听器检查请求/响应,以检查您的测试是否正在执行预期的操作
  5. 准备好.jmx脚本后,请使用JMeter API以编程方式生成完全相同的测试计划。

P.S。在没有GUI的情况下创建JMeter脚本的一种更简便的方法是使用View Results Tree工具,在该工具中,您可以使用基于声明式YAML的语法来开发测试计划。