1

我正在使用 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();

**

4

1 回答 1

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)
    

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

    在此处输入图像描述

查看您应该与 Groovy 一起使用的 8 个 JMeter Java 类,以了解有关这些varsSampleResult速记的更多信息

于 2020-10-07T07:23:13.933 回答