JSR223采样器的WDS.sampleResult.sampleStart()

时间:2020-10-07 03:01:31

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

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

**

1 个答案:

答案 0 :(得分:0)

JSR223 Sampler根据脚本内容自动计算持续时间,因此,如果要测量查找输入所需的时间,则必须选择以下选项:

  1. 创建另一个JSR223采样器,它将打开所需的页面并将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)
    

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

    enter image description here

查看Top 8 JMeter Java Classes You Should Be Using with Groovy,以详细了解这些varsSampleResult的速记

相关问题