带有Java的JSR233采样器可与SeleniumWebdriver一起使用(javax.script.ScriptException:在文件中:内联评估)
问题内容:
尝试使用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/");
我通过以下参考资料来获得上述脚本:
- 使用JMeter运行Selenium脚本
我们可以使用带有JavaScript的Selenium Webdriver config
sampler启动浏览器并执行操作,但是由于我们无法使用WDS设置功能,因此我们试图在JSR233中实现相同功能。
问题答案:
如果您真的想继续使用Beanshell,则Beanshell不支持Diamond
运算符 -更改此行:
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
到这个
HashMap chromePrefs = new HashMap();
请注意,从JMeter
3.1版开始,建议使用JSR223测试元素和Groovy语言进行脚本编写,原因如下:
- Groovy支持所有现代Java语言功能
- Groovy对标准Java SDK进行了很多增强
- Groovy提供了比Beanshell更好的性能
因此,考虑迁移到Groovy,我的期望是不需要进行任何更改(如果有的话,您可能需要将lambda重写为闭包,但是开销很小)。