无法在 WebdriverSampler 中使用 groovy Webdriver 脚本

Unable to use groovy Webdriver scripts in WebdriverSampler

我正在尝试使用 groovy 脚本通过 webdriver 采样器启动我的客户端,但它不起作用,因为 expected.only JavaScript 正在使用以下代码

var pkg = JavaImporter(org.openqa.selenium); //WebDriver classes
var support_ui = JavaImporter(org.openqa.selenium.support.ui.WebDriverWait); //WebDriver classes
var wait = new support_ui.WebDriverWait(WDS.browser, 5000);

WDS.sampleResult.sampleStart(); //captures sampler's start time
WDS.sampleResult.getLatency();
WDS.log.info("Sample started");

WDS.browser.get('https://google.com/'); 
  1. Groovy syntax differ from JavaScript, i.e. there is no JavaImporter there, you should use import keyword 而不是
  2. Groovy/Java 中没有 var 关键字(除非您使用 Java 10), you need to change it to def keyword
  3. 假设以上所有你需要修改你的代码如下:

    import org.openqa.selenium.support.ui.WebDriverWait
    
    def wait = new WebDriverWait(WDS.browser,5000);
    
    WDS.sampleResult.sampleStart(); //captures sampler's start time
    WDS.sampleResult.getLatency();
    WDS.log.info("Sample started");
    
    WDS.browser.get('https://google.com/');
    

    演示:

查看 Apache Groovy - Why and How You Should Use It 文章以开始使用 JMeter 中的 Groovy 脚本