简体   繁体   English

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

[英]WDS.sampleResult.sampleStart() for JSR223 Sampler

I am using JSR223 Sampler and I want to start calculating time after url load so my code as below :我正在使用 JSR223 Sampler,我想在 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();

** **

JSR223 Sampler automatically calculates its duration depending on your script contents so if you want to measure the time required to find an input you have to options: JSR223 Sampler 会根据您的脚本内容自动计算其持续时间,因此如果您想测量查找输入所需的时间,您必须选择:

  1. Create another JSR223 Sampler which will open the required page and store the WebDriver instance into JMeterVariables like:创建另一个 JSR223 采样器,它将打开所需的页面并将 WebDriver 实例存储到JMeterVariables 中,例如:

    • First JSR223 Sampler:第一个 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)
    • Second JSR223 Sampler:第二个 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. Use SampleResult.addSubResult() function to create a "child" sample result which will measure the time required to locate the element:使用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)

    in this case you will get something like:在这种情况下,你会得到类似的东西:

    在此处输入图片说明

Check out Top 8 JMeter Java Classes You Should Be Using with Groovy to learn more about these vars and SampleResult shorthands查看您应该与 Groovy一起使用的前 8 个 JMeter Java 类,以了解有关这些varsSampleResult速记的更多SampleResult

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM