Selenium webdriver 使用Java的JSR233采样器与Selenium Webdriver配合使用(javax.script.ScriptException:在文件中:内联求值)

Selenium webdriver 使用Java的JSR233采样器与Selenium Webdriver配合使用(javax.script.ScriptException:在文件中:内联求值),selenium-webdriver,jmeter,Selenium Webdriver,Jmeter,尝试使用JSR233采样器在Jmeter中运行SeleniumWebDriver脚本。该脚本在EclipseIDE中运行良好,但在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.HashMa

尝试使用JSR233采样器在Jmeter中运行SeleniumWebDriver脚本。该脚本在EclipseIDE中运行良好,但在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/");
import java.util.HashMap;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.chrome.ChromeOptions;
导入org.openqa.selenium.remote.CapabilityType;
导入org.openqa.selenium.remote.DesiredCapabilities;
导入org.openqa.selenium.By;
导入org.openqa.selenium.support.ui.WebDriverWait;
导入org.openqa.selenium;
System.setProperty(“webdriver.chrome.driver”,“D:\\chromedriver.exe”);
String downloadFilepath=“D:/MyDeskDownload”;
HashMap chromePrefs=新HashMap();
//chromePrefs.put(“profile.default\u content\u settings.popups”,0);
//chromePrefs.put(“download.default\u目录”,downloadFilepath);
//chromePrefs.put(“safebrowsing.enabled”、“true”);
ChromeOptions选项1=新的ChromeOptions();
选项1.设置实验选项(“prefs”,chromePrefs);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
cap.setCapability(色度选项.CAPABILITY,选项1);
WebDriver=新的ChromeDriver(cap);
driver.setJavaScriptEnabled(true);
驱动程序。获取(“http://google.com/");
我已经阅读了以下参考资料以获得上述脚本:


我们可以通过JavaScript启动浏览器并使用Selenium Webdriver config sampler执行操作,但是由于我们无法使用WDS设置功能,我们正在尝试在JSR233中实现同样的功能。

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

因为它是Beanshell,所以它不理解泛型(菱形运算符),所以这一行:

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

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

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

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

请注意,原因在于:

  • Groovy支持所有现代Java语言特性

<> >考虑迁移到Groovy,我的期望是不需要改变(如果需要的话,您可能需要将lambdas改写为闭包,但是开销会很小)

您使用的是JME和WebDelphi插件的哪种版本?您还可以显示lib文件夹的内容吗?感谢Jmeter插件管理器安装的Selenium Webdriver的回复,Jmeter版本5。感谢回复。我把launguage改为Groovy,结果出错了。错误o.a.j.p.j.s.JSR223Sampler:JSR223脚本JSR223 Sampler中存在问题,消息:javax.script.ScriptException:groovy.lang.MissingMethodException:没有方法签名:org.openqa.selenium.chrome.ChromeDriver.setJavaScriptEnabled()适用于参数类型:(java.lang.Boolean)值:[]javax.script.ScriptException:groovy.lang.MissingMethodException:org.openqa.selenium.chrome.ChromeDriver.setJavaScriptEnabled()的签名不适用于参数类型:(java.lang.Boolean)值:[true]我在中没有看到此函数,它似乎只适用于。默认情况下,JavaScript应该在Chrome中启用,您不需要做任何额外的事情。向前看,把这个问题的答案发给你复制和粘贴的人。非常感谢,在评论了这篇文章之后,你可以启动浏览器并导航到URL。谢谢你的建议,它现在正在工作,改为Groovy。我很高兴我帮助了你。