【问题标题】:SampleResult is not working as Expected with JSR223 sampler in JMeterSampleResult 与 JMeter 中的 JSR223 采样器没有按预期工作
【发布时间】:2018-09-12 16:34:24
【问题描述】:

我一直在使用 JSR223 采样器,使用 Java 作为选择的语言。我的问题是,当我使用 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?

【问题讨论】:

    标签: java jmeter performance-testing jsr223 blazemeter


    【解决方案1】:

    您不需要为SampleResult 创建构造函数,它已经作为预定义变量公开。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2015-10-20
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多