4

Trying to run Selenium Webdriver script in Jmeter using JSR233 sampler. The script works fine in Eclipse IDE, however in Jmeter facing below error.

ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, 
message: javax.script.ScriptException: In file: inline evaluation of: 
``import java.util.HashMap; import org.openqa.selenium.WebDriver; import 
org.openq . . . '' Encountered "," at line 28, column 25.
in inline evaluation of: ``import java.util.HashMap; import 
org.openqa.selenium.WebDriver; import org.openq . . . '' at line number 28
javax.script.ScriptException: In file: inline evaluation of: ``import 
java.util.HashMap; import org.openqa.selenium.WebDriver; import org.openq . 
. . '' Encountered "," at line 28, column 25.
in inline evaluation of: ``import java.util.HashMap; import 
org.openqa.selenium.WebDriver; import org.openq . . . '' at line number 28
at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:82) ~[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_181] 

Below is the script trying to execute:

    import java.util.HashMap;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.By;
    import org.openqa.selenium.support.ui.WebDriverWait;
    import org.openqa.selenium;
    System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
    String downloadFilepath = "D:/MyDeskDownload";
    HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
   // chromePrefs.put("profile.default_content_settings.popups", 0);
   // chromePrefs.put("download.default_directory", downloadFilepath);
   // chromePrefs.put("safebrowsing.enabled", "true"); 
    ChromeOptions options1 = new ChromeOptions();
    options1.setExperimentalOption("prefs", chromePrefs);
    DesiredCapabilities cap = DesiredCapabilities.chrome();
    cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
    cap.setCapability(ChromeOptions.CAPABILITY, options1);
    WebDriver driver = new ChromeDriver(cap);
    driver.setJavaScriptEnabled(true);
    driver.get("http://google.com/");

I have gone through below references to get above script:

We could achieve launching a browser and performing actions with Selenium Webdriver config sampler with JavaScript however since we are unable to set capabilities with WDS, we are trying to achieve the same in JSR233.

2
  • which version of jmeter and webdriver plugin are you using ? Can you also show content of lib folder ? Thanks Commented Jan 28, 2019 at 21:10
  • Thanks for the reply, Jmeter version 5, From Jmeter Plugin Manager installed Selenium Webdriver. Commented Jan 29, 2019 at 2:47

2 Answers 2

4

From stacktrace it looks you are using JSR223 with Beanshell or Java (which will be beanshell).

Since it's Beanshell, it doesn't understands generics (diamond operator) so this line:

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();

So you need to just switch the language to Groovy to fix the problem:

JSR223 Sampler

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for the suggestion, it is working now, changed to Groovy.
Happy I helped you
1

Beanshell doesn't support the diamond operator to if you really want to continue wiht Beanshell - change this line:

HashMap<String, Object> chromePrefs = new HashMap<String, Object>();

to this one

HashMap chromePrefs = new HashMap();

Be aware that starting from JMeter version 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting, the reasons are in:

So consider migrating to Groovy, my expectation is that no changes will be required (you might need rewriting lambdas into closures if any, however the overhead will be minimal)

3 Comments

Thanks for the reply. I changed launguage to Groovy, getting error. ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.openqa.selenium.chrome.ChromeDriver.setJavaScriptEnabled() is applicable for argument types: (java.lang.Boolean) values: [true] javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: org.openqa.selenium.chrome.ChromeDriver.setJavaScriptEnabled() is applicable for argument types: (java.lang.Boolean) values: [true]
I fail to see this function in ChromeDriver JavaDoc, it seems to be applicable only for HtmlUnitDriver. JavaScript should be enabled in Chrome by default, you don't need to do anything extra. Going forward consider addressing this form of questions to the person whose code you copied and pasted.
Thanks a lot, after commenting this able to launch the browser and navigate to the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.