我一直在使用Java作为选定语言的JSR223采样器。我的问题是,当我使用SampleResult函数说SampleResult.sampleStart/End()时,显示错误SampleResult: sampleStart called twice

剧本:

/*All import statements*/
setStrictJava(false);

public static DesiredCapabilities desCapabilities;
public static WebDriver driver = null;
public static String IEDriver = "Chrome";

if (IEDriver.equalsIgnoreCase("Chrome")) {
    desCapabilities = DesiredCapabilities.chrome();
    desCapabilities.setPlatform(Platform.ANY);
    desCapabilities.setBrowserName("Chrome");
    ChromeOptions options = new ChromeOptions();
    options.addArguments(new String[] {"--start-maximized"});
    System.setProperty("webdriver.chrome.driver","D:/Bala/Automation/Chrome/chromedriver.exe");
    desCapabilities.setCapability("chromeOptions", options);
    driver = new ChromeDriver(desCapabilities);
    Thread.sleep(20000);
    driver.get("http://google.com");
    SampleResult.setSampleLabel("Login Page Load Time");
    SampleResult.setSuccessful(true);
    SampleResult.sampleStart();
    JavascriptExecutor js = (JavascriptExecutor)driver;
    while(true){
       log.info("Started while loop...");
       Boolean isJSLoaded = js.executeScript("return document.readyState;", new Object[] {}).toString().equals("complete");
       Boolean isAjaxLoaded = js.executeScript("return window.jQuery != undefined && jQuery.active == 0 && window.$.active", new Object[] {}).equals("0");
       if(!(isJSLoaded) && !(isAjaxLoaded)){
            break;
       }
    }
    log.info("Ended while loop...");
    (new WebDriverWait(driver, 60)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='googleHome']")));
    SampleResult.sampleEnd();
    return driver.getWindowHandle();
    } else if {
        /*code for IE browser*/
    }


在“ IF”条件之外使用时,它可以正常工作。

以下是到目前为止我尝试过的事情:

为SampleResult创建一个构造函数并调用其方法:这并没有给我错误,而是从线程的开始记录时间。

SampleResult result = new SampleResult();
result.setSampleLabel("Login Page Load Time2");
result.setSuccessful(true);
result.sampleStart();
Thread.sleep(3000);
result.sampleEnd();


如何在调用“ driver.get()”后立即开始记录时间?

如何在“ IF”条件下使用SampleResult?

最佳答案

您不需要为SampleResult创建构造函数,它已经作为预定义变量公开。
java - SampleResult无法与JMeter中的JSR223采样器配合使用-LMLPHP

您无需调用sampleStart()sampleEnd()函数,它们会根据您的采样器持续时间自动计算,因此您需要修改代码,例如:

SampleResult.setSampleLabel("Login Page Load Time2");
SampleResult.setSuccessful(true);
Thread.sleep(3000);


更多信息:Apache Groovy - Why and How You Should Use It

08-28 18:52