You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何定义可被JMeter WebDriver Sampler调用的JavaScript全局函数

全局定义WebDriver Sampler通用JavaScript函数的方法

问题背景

我正在使用WebDriver Sampler在JMeter中测试Web应用的客户端性能。
注意:因Groovy无法正常工作,目前使用JavaScript,后续需切换至Groovy。
当前一切运行正常,页面加载时间可在监听器中显示。多个WebDriver Sampler会用到一些通用函数,想知道是否可以全局定义这些JavaScript函数,以便在每个WebDriver Sampler中调用?

当前代码示例:

var pkg = JavaImporter(org.openqa.selenium)
var time = JavaImporter(java.time)
var support_ui = JavaImporter(org.openqa.selenium.support.ui);
var wait = new support_ui.WebDriverWait(WDS.browser, time.Duration.ofSeconds(10));


WDS.sampleResult.sampleStart();
    WDS.log.info("Start navigating to new bus incident creation page");
    waitAndClick(pkg.By.cssSelector("#New"));
    waitAndClick(pkg.By.cssSelector("#Bus_3"));
    waitAndClick(pkg.By.cssSelector("#Bus_3_0"));
    waitUntilLoadingIsCompleted();
    WDS.log.info("Accessed the new bus incident creation page");
WDS.sampleResult.sampleEnd();



function waitUntilLoadingIsCompleted() {
    wait.until(support_ui.ExpectedConditions.presenceOfElementLocated(pkg.By.cssSelector(".k-loading-text")));
    wait.until(support_ui.ExpectedConditions.invisibilityOfElementLocated(pkg.By.cssSelector(".k-loading-text")));
}


function waitAndClick(element){
    wait.until(support_ui.ExpectedConditions.elementToBeClickable(element));
    WDS.browser.findElement(element).click()
}

解决方案

可以通过Test Setup线程组实现全局函数定义,具体步骤如下:

  1. 添加Test Setup线程组
    在测试计划中创建一个Test Setup线程组(该线程组会在所有测试线程启动前执行一次),然后在其中添加一个JSR223 Sampler,语言选择JavaScript,将通用函数挂载到WDS全局对象上:
var pkg = JavaImporter(org.openqa.selenium)
var time = JavaImporter(java.time)
var support_ui = JavaImporter(org.openqa.selenium.support.ui);

// 全局等待实例
WDS.wait = new support_ui.WebDriverWait(WDS.browser, time.Duration.ofSeconds(10));

// 全局函数:等待加载完成
WDS.waitUntilLoadingIsCompleted = function() {
    WDS.wait.until(support_ui.ExpectedConditions.presenceOfElementLocated(pkg.By.cssSelector(".k-loading-text")));
    WDS.wait.until(support_ui.ExpectedConditions.invisibilityOfElementLocated(pkg.By.cssSelector(".k-loading-text")));
}

// 全局函数:等待并点击元素
WDS.waitAndClick = function(element){
    WDS.wait.until(support_ui.ExpectedConditions.elementToBeClickable(element));
    WDS.browser.findElement(element).click()
}
  1. 在WebDriver Sampler中调用全局函数
    之后每个WebDriver Sampler只需导入必要的Java包,直接调用全局函数即可,无需重复定义:
var pkg = JavaImporter(org.openqa.selenium)
var time = JavaImporter(java.time)
var support_ui = JavaImporter(org.openqa.selenium.support.ui);

WDS.sampleResult.sampleStart();
    WDS.log.info("Start navigating to new bus incident creation page");
    WDS.waitAndClick(pkg.By.cssSelector("#New"));
    WDS.waitAndClick(pkg.By.cssSelector("#Bus_3"));
    WDS.waitAndClick(pkg.By.cssSelector("#Bus_3_0"));
    WDS.waitUntilLoadingIsCompleted();
    WDS.log.info("Accessed the new bus incident creation page");
WDS.sampleResult.sampleEnd();

后续切换Groovy的提示

如果后续切换到Groovy,逻辑类似:在Test Setup的JSR223 Sampler中定义全局函数,可通过props对象绑定,或者直接将函数定义为全局变量,在其他Sampler中直接调用即可。


内容的提问来源于stack exchange,提问作者Janesh Kodikara

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 07:50:25