如何在SoapUI的Groovy脚本中运行Selenium脚本并解决NoSuchMethodError
解决SoapUI Groovy脚本中Selenium的NoSuchMethodError问题
问题描述
我在SoapUI的Groovy脚本测试步骤中编写了如下Selenium脚本:
openBrowser() // Invoke the method from groovy script public void openBrowser() { System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://stackoverflow.com"); System.out.println("Application opened"); System.out.println("Page Title is : "+driver.getTitle()); driver.quit(); }
运行该脚本时,立即弹出错误提示:
java.lang.NoSuchMethodError: org.openqa.selenium.os.CommandLine.findExecutable(Ljava/lang/String;)Ljava/lang/String; error at line: 19
相关版本信息:
- Chrome浏览器版本:66+
- Selenium独立服务器:3.12.0
- ChromeDriver版本:2.38
- SoapUI工具版本:5.3.0
该脚本在Eclipse IDE中添加selenium-standalone-server 3.12.0 jar包后可正常运行,请问如何解决SoapUI中的这个问题?
问题原因
这个报错的核心原因是SoapUI自带了旧版本的Selenium依赖jar包,和你手动引入的3.12.0版本产生了依赖冲突。旧版本的Selenium jar里并没有org.openqa.selenium.os.CommandLine.findExecutable这个方法,导致运行时JVM找不到该方法,抛出NoSuchMethodError。
解决办法
方法1:替换SoapUI全局的Selenium依赖
这是最直接的解决方案,把SoapUI自带的旧Selenium jar换成你需要的版本:
- 找到SoapUI的安装目录,进入
bin\ext文件夹(这是SoapUI存放外部依赖的默认路径) - 删掉或者重命名该文件夹下所有带
selenium前缀的jar文件(比如selenium-api-x.x.x.jar、selenium-server-x.x.x.jar这类) - 将你在Eclipse中使用的
selenium-standalone-server-3.12.0.jar复制到bin\ext文件夹中 - 重启SoapUI,再运行你的Groovy脚本应该就正常了
方法2:给单个项目配置优先加载的依赖
如果不想修改SoapUI全局的依赖,可以给当前测试项目单独指定依赖优先级:
- 在SoapUI中右键点击你的测试项目,选择Properties(属性)
- 在弹出的窗口中切换到Libraries(库)标签页
- 点击Add按钮,选择你的
selenium-standalone-server-3.12.0.jar添加到列表中 - 用窗口右侧的上下箭头,把这个jar调整到列表的最顶部——这样脚本会优先加载这个新版本的jar,避免和旧版本冲突
- 保存项目设置后,重新运行Groovy脚本即可
额外提醒
- 你的Chrome浏览器(66+)和ChromeDriver(2.38)版本是匹配的(2.38支持Chrome 65-67),这部分不用调整
- 如果你使用的是SoapUI Pro版本,操作逻辑和社区版一致,只是依赖存放路径可能略有差异,找到对应的依赖文件夹替换或添加jar即可
内容的提问来源于stack exchange,提问作者Karunagara
相关产品推荐
相关产品推荐

