移动设备请求在 JMeter 中使用 JSR223 进行仿真 - 没有这样的 属性:class 的驱动程序

mobile devices requests emulation using JSR223 in JMeter - No such property: driver for class

场景:

  1. 打开主页并单击“接受所有 Cookie”(Once Only 控制器中的 JSR223 Sampler1);
  2. 从一组参数化 urls(另一个控制器中的 JSR223 Sampler2)打开页面。

JSR223 Sampler1 主页代码:

import org.apache.jmeter.samplers.SampleResult; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.WebDriver; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.WebDriverWait; import java.util.concurrent.TimeUnit;

System.setProperty("webdriver.chrome.driver", "vars.get("webdriver_path")");

Map<String, Object> mobileEmulation = new HashMap<>(); mobileEmulation.put("userAgent", "vars.get("userAgent")"); Map<String, Object> chromeOptions = new HashMap<>(); chromeOptions.put("mobileEmulation", mobileEmulation); ChromeOptions options = new ChromeOptions(); options.setExperimentalOption("mobileEmulation", mobileEmulation); ChromeDriver driver = new ChromeDriver(options);

driver.get("https://vars.get("main_page")"); WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("xpath"))); driver.findElement(By.xpath("xpath")).click(); log.info(driver.getTitle());

JSR223 Sampler2 代码,用于 url 集合中的任何页面:

driver.get("https://${url}");

错误信息: 响应 message:javax.script.ScriptException: groovy.lang.MissingPropertyException: 没有这样的 属性: class

的驱动程序

问题: 如果我只是将所有代码从 JSR223 Sampler1 复制到 JSR223 Sampler2 并更改目标 url,urls 正在打开,但方式不正确 - 每次启动新的浏览器实例时,我都无法做出现实的回应时间(仅适用于 driver.get(url")),因为结果提供了 Sampler 工作的时间,包括驱动程序初始化、新浏览器实例启动,这需要几秒钟...

能否请您提出任何想法,如何解决这个问题?要在 1 个浏览器实例中获取所有请求并为 JSR223 Sampler2 中的所有请求提供真实的响应时间,仅适用于 browser.get("url")? 将不胜感激。

  1. 在第一个 JSR223 采样器中,您需要将 driver 实例存储到 JMeter Variables 中,例如:

    vars.putObject("driver", driver)
    

    它应该是你脚本的最后一行

  2. 在第二个 JSR223 采样器中,您需要从 JMeter 变量中获取 driver 实例,例如:

    driver = vars.getObject("driver")
    

    它应该是你脚本的第一行

vars 是 shorthand for JMeterVariables class instance, see the JavaDoc for all available functions and Top 8 JMeter Java Classes You Should Be Using with Groovy 文章以获取有关 JMeter 的更多信息 API shorthand 可用于 JSR223 测试元素

P.S。在执行 driver.get() 函数时,您应该遵循与 vars 相同的方法,例如:

driver.get("https://" + vars.get("url"))