la1
la1

Reputation: 539

JSR233 sampler with Java to work with Selenium Webdriver (javax.script.ScriptException: In file: inline evaluation)

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.

Upvotes: 4

Views: 2252

Answers (2)

UBIK LOAD PACK
UBIK LOAD PACK

Reputation: 34566

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

Upvotes: 4

Dmitri T
Dmitri T

Reputation: 168157

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)

Upvotes: 1

Related Questions