问题描述
我正在使用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();
**
解决方法
JSR223 Sampler根据脚本内容自动计算持续时间,因此,如果要测量查找输入所需的时间,则必须选择以下选项:
-
创建另一个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']")));
-
-
使用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",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,以详细了解这些vars
和SampleResult
的速记