带有Java的JSR233采样器可与Selenium Webdriver一起使用(javax.script.ScriptException:在文件中:内联评估)

时间:2019-01-28 18:17:43

标签: selenium-webdriver jmeter

尝试使用JSR233采样器在Jmeter中运行Selenium Webdriver脚本。该脚本在Eclipse IDE中运行良好,但是在Jmeter中则遇到以下错误。

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] 

下面是试图执行的脚本:

    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/");

我已经阅读了以下参考资料以获得以上脚本:

我们可以使用带有JavaScript的Selenium Webdriver config sampler启动浏览器并执行操作,但是由于我们无法使用WDS设置功能,因此我们试图在JSR233中实现相同功能。

2 个答案:

答案 0 :(得分:4)

从stacktrace看来,您正在将JSR223与Beanshell或Java(将是Beanshell)一起使用。

因为它是Beanshell,所以它不理解泛型(钻石运算符),因此此行:

  

HashMap chromePrefs = new HashMap ();

因此,您只需将语言切换为Groovy即可解决问题:

JSR223 Sampler

答案 1 :(得分:1)

如果您真的想继续使用Beanshell,则Beanshell不支持the diamond operator-更改此行:

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

对此

HashMap chromePrefs = new HashMap();

请注意,starting from JMeter version 3.1 it's recommended to use JSR223 Test Elements and Groovy language for scripting的原因是:

因此考虑迁移到Groovy,我的期望是不需要进行任何更改(如果有的话,您可能需要将lambda重写为闭包,但是开销很小)