1
votes

I want to measure performance of a Web page in real time. So, got the 1st option of JMeter with Selenium.

I am able to do some navigations and click operations using JSR223 sampler in Java.

But, while waiting for all the JQuery and DOM ready state, I am failed to do in Javascript Executor(org.openqa.selenium.JavascriptExecutor). Below is my code of Wait statements.

setStrictJava(false); 
/*
---some block of code---
*/
while(true){ 
     Boolean isAjaxCompletes = (Boolean) ((JavascriptExecutor)driver).executeScript("return jQuery.active == 0;"); 
     Boolean isJsLoaded = ((JavascriptExecutor)driver).executeScript("return document.readyState;").toString().equals("complete"); 
     if(isAjaxCompletes && isJsLoaded){ 
           break; 
     } 
}

I am getting the below error:

2018-04-02 18:39:33,794 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; impor . . . '' : Error in method invocation: Static method executeScript( java.lang.String ) not found in class'org.openqa.selenium.JavascriptExecutor' : at Line: 36 :  
 in file: inline evaluation of: ``import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; impor . . . '' : JavascriptExecutor .executeScript ( "return jQuery.active == 0;" ) 
in inline evaluation of: ``import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; impor . . . '' at line number 36
javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; impor . . . '' : Error in method invocation: Static method executeScript( java.lang.String ) not found in class'org.openqa.selenium.JavascriptExecutor' : at Line: 36 : in file: inline evaluation of: ``import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; impor . . . '' : JavascriptExecutor .executeScript ( "return jQuery.active == 0;" ) 
in inline evaluation of: ``import org.openqa.selenium.Platform; import org.openqa.selenium.WebDriver; impor . . . '' at line number 36
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
at javax.script.AbstractScriptEngine.eval(Unknown Source) ~[?:1.8.0_151]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_151]

Kindly help me to solve this.

JMeter version: 4.0
Plugins auto downloaded selenium version: 2.52.0
Java: 1.8, update 151

3
Yes, I added already. - Bala

3 Answers

1
votes

You are calling the method with static reference which isn't exists. you should create JavascriptExecutor object and call executeScript method:

JavascriptExecutor js = (JavascriptExecutor) driver;  
Boolean isJsLoaded = js.executeScript("return document.readyState;").toString().equals("complete"); 
0
votes

You need to use the method equals() as follows :

import org.openqa.selenium.JavascriptExecutor;

Boolean isAjaxCompletes = (Boolean) ((JavascriptExecutor) webdriver).executeScript("return jQuery.active").equals("0")); 
Boolean isJsLoaded = ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete"));
0
votes
  1. First of all switch to Groovy language, since JMeter 3.1 it is recommended to use Groovy for any form of scripting
  2. Second, you can use normal ExplicitWait in the WebDriver Sampler as follows:

    import org.openqa.selenium.WebDriver
    import org.openqa.selenium.support.ui.ExpectedCondition
    import org.openqa.selenium.support.ui.WebDriverWait
    
    WDS.sampleResult.sampleStart()
    WDS.browser.get('http://jmeter-plugins.org')
    WebDriverWait wait = new WebDriverWait(WDS.browser, 5)
    
    ExpectedCondition<Boolean> documentReady = new ExpectedCondition<Boolean>() {
        @Override
        public Boolean apply(WebDriver driver) {
            return WDS.browser.executeScript("return document.readyState")
                    .toString().equalsIgnoreCase("COMPLETE");
        }
    };
    wait.until(documentReady)
    WDS.sampleResult.sampleEnd()