在JMeter中,SampleResult与JSR223采样器的预期无法正常工作

时间:2018-04-03 11:08:43

标签: java jmeter performance-testing jsr223 blazemeter

我一直在使用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' IF'以外的地方给出它是正常的。条件。

以下是我到目前为止所做的事情:

为SampleResult创建一个构造函数并调用它的方法:这并没有给出ERROR,而是从线程的开头记录时间。

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

如何在' driver.get()'之后立即开始记录时间被称为?

如何使用SampleResult' IF'条件?

1 个答案:

答案 0 :(得分:0)

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

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

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

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