【问题标题】:WDS.sampleResult.sampleStart() for JSR223 SamplerJSR223 采样器的 WDS.sampleResult.sampleStart()
【发布时间】:2020-10-07 03:01:31
【问题描述】:

我正在使用 JSR223 采样器,我想在 url 加载后开始计算时间,所以我的代码如下:

**

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
System.setProperty("webdriver.gecko.driver","/Users/geckodriver");
FirefoxOptions options = new FirefoxOptions().setAcceptInsecureCerts(true);
options.addArguments("--headless");
WebDriver driver = new FirefoxDriver(options);
def wait = new WebDriverWait(driver, 20);
driver.get('https://google.com/');
WDS.sampleResult.sampleStart();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@name='q']")));
WDS.sampleResult.sampleEnd();

**

【问题讨论】:

  • 我基本上需要在 driver.get() 之后开始计算时间。我使用了函数 sampleStart();/sampleEnd();最终出现以下错误:2020-10-07 16:08:11,096 错误 o.a.j.p.j.s.JSR223Sampler:JSR223 脚本 JSR223 采样器中的问题,消息:javax.script.ScriptException:groovy.lang.MissingMethodException:没有方法签名:org.codehaus。 groovy.jsr223.GroovyScriptEngineImpl.sampleStart() 适用于参数类型:() 值:[]

标签: selenium selenium-webdriver jmeter jmeter-plugins jsr233


【解决方案1】:

JSR223 Sampler 会根据您的脚本内容自动计算其持续时间,因此如果您想测量查找输入所需的时间,您必须选择:

  1. 创建另一个 JSR223 Sampler,它将打开所需的页面并将 WebDriver 实例存储到 JMeterVariables 中,例如:

    • 第一个 JSR223 采样器:

      import org.openqa.selenium.WebDriver;
      import org.openqa.selenium.firefox.FirefoxDriver;
      import org.openqa.selenium.firefox.FirefoxOptions;
      import org.openqa.selenium.support.ui.WebDriverWait;
      System.setProperty("webdriver.gecko.driver","/Users/geckodriver");
      FirefoxOptions options = new FirefoxOptions().setAcceptInsecureCerts(true);
      options.addArguments("--headless");
      WebDriver driver = new FirefoxDriver(options);
      def wait = new WebDriverWait(driver, 20);
      driver.get('https://google.com/');
      
      vars.putObject('driver', driver)
      vars.putObject('wait', wait)
      
    • 第二个 JSR223 采样器:

       import org.openqa.selenium.By;
       import org.openqa.selenium.support.ui.ExpectedConditions;
      
       def driver = vars.getObject('driver')
       def wait = vars.getObject('wait')
       wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@name='q']")));
      
  2. 使用SampleResult.addSubResult() 函数创建一个“子”样本结果,该结果将测量定位元素所需的时间:

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxOptions;
    import org.openqa.selenium.By;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    System.setProperty("webdriver.gecko.driver","/Users/geckodriver");
    FirefoxOptions options = new FirefoxOptions().setAcceptInsecureCerts(true);
    options.addArguments("--headless");
    WebDriver driver = new FirefoxDriver(options);
    def wait = new WebDriverWait(driver, 20);
    driver.get('https://google.com/');
    
    def myResult = new org.apache.jmeter.samplers.SampleResult()
    myResult.setSampleLabel('Locating element')
    myResult.sampleStart()
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//input[@name='q']")));
    myResult.setResponseCodeOK()
    myResult.setSuccessful(true)
    myResult.sampleEnd()
    
    SampleResult.addSubResult(myResult,false)
    

    在这种情况下,你会得到类似的东西:

查看Top 8 JMeter Java Classes You Should Be Using with Groovy 了解更多关于这些varsSampleResult 速记的信息

【讨论】:

  • 谢谢,@Dmitri,我相信选项 2 更适合我目前的要求,但它再次抛出同样的错误。
  • 2020-10-07 18:58:21,636 INFO o.a.j.e.StandardJMeterEngine:所有线程组已启动 2020-10-07 18:58:21,636 INFO o.a.j.t.JMeterThread:线程已启动:FirefoxeBrowser 1-1 2020 -10-07 18:58:25,427 错误 o.a.j.s.SampleResult:sampleEnd 调用了两次 java.lang.Throwable:org.apache.jmeter.samplers.SampleResult.sampleEnd 的调用序列无效(SampleResult.java:1145)[ApacheJMeter_core.jar:5.3 ] 在 org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:82)
  • 我复制了上面的脚本并运行了它。
  • 我切换到 Optioned 2 ,感谢您的帮助,它解决了我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多