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.

la1
  • 519
  • 1
  • 8
  • 30
  • which version of jmeter and webdriver plugin are you using ? Can you also show content of lib folder ? Thanks – UBIK LOAD PACK Jan 28 '19 at 21:10
  • Thanks for the reply, Jmeter version 5, From Jmeter Plugin Manager installed Selenium Webdriver. – la1 Jan 29 '19 at 02:47

2 Answers2

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

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
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)

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • 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] – la1 Jan 29 '19 at 02:42
  • I fail to see this function in [ChromeDriver JavaDoc](https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeDriver.html), it seems to be applicable only for [HtmlUnitDriver](https://github.com/SeleniumHQ/selenium/wiki/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. – Dmitri T Jan 29 '19 at 02:51
  • Thanks a lot, after commenting this able to launch the browser and navigate to the url. – la1 Jan 29 '19 at 02:52