Bala
Bala

Reputation: 204

SampleResult is not working as Expected with JSR223 sampler in JMeter

I have been working with JSR223 sampler with Java as chosen Language. My problem is that when I use SampleResult functions say SampleResult.sampleStart/End(), the error SampleResult: sampleStart called twice is displayed.

The script:

/*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*/
    }

It is working properly when it is given in outside of 'IF' condition.

Below are the things I tried so far:

Create a constructor for SampleResult and call its methods: This didn't give me the ERROR, instead, time is recorded from the beginning of the thread.

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

How to start recording the time immediately after 'driver.get()' is called?

How to use SampleResult inside 'IF' condition?

Upvotes: 3

Views: 2480

Answers (1)

Dmitri T
Dmitri T

Reputation: 168002

You don't need to create a constructor for SampleResult, it is already exposed as a pre-defined variable. JMeter SampleResult

You neither need to call sampleStart() and sampleEnd() functions, they are calculated automatically depending on your Sampler duration so you need to amend your code like:

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

More information: Apache Groovy - Why and How You Should Use It

Upvotes: 4

Related Questions