在我将 TestNG 更新到 6.14.3 并将硒更新到 3.13.0 后,我遇到了上述 Json 异常。我正在使用ChromeDriver((进行自动化。以下是关于我的代码的简要想法:
@BeforeSuite:
System.setProperty("webdriver.chrome.driver", "path to chrome driver");
@BeforeMethod:
driver = new ChromeDriver();
@AfterMethod:
driver.quit();
我以前的版本分别是 TestNG 和 Selenium 的 6.11.0 和 3.7.1。
当代码尝试启动 Web 驱动程序会话时,会出现异常。
完全例外是:
java.lang.NoSuchMethodError: org.openqa.selenium.json.JsonOutput.write(Ljava/lang/Object;Ljava/lang/reflect/Type;)Lorg/openqa/selenium/json/JsonOutput;
at org.openqa.selenium.remote.NewSessionPayload.writeTo(NewSessionPayload.java:247)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:69)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:548)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:212)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:130)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:59)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:458)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:222)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:523)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Suppressed: org.openqa.selenium.json.JsonException: Attempting to close incomplete json stream
Build info: version: '3.13.0', revision: '2f0d292', time: '2018-06-25T15:24:21.231Z'
Driver info: driver.version: ChromeDriver
at org.openqa.selenium.json.JsonOutput.close(JsonOutput.java:263)
at org.openqa.selenium.remote.NewSessionPayload.writeTo(NewSessionPayload.java:270)
此错误消息:
java.lang.NoSuchMethodError: org.openqa.selenium.json.JsonOutput.write(Ljava/lang/Object;Ljava/lang/reflect/Type;)Lorg/openqa/selenium/json/JsonOutput;
.
Suppressed: org.openqa.selenium.json.JsonException: Attempting to close incomplete json stream
。暗示java.lang.NoSuchMethodError是在JVM试图转义某些字符时引发的。
如果没有与测试环境相关的以下信息,则很难分析错误:
- Selenium-server-standalone-3.13.0.jar或Selenium-java-3.13.0客户端。
- JARs或Maven或Gradle
但是此错误来自以下源代码:
public void close() {
if (appendable instanceof Closeable) {
try {
((Closeable) appendable).close();
} catch (IOException e) {
throw new JsonException(e);
}
}
if (!(stack.getFirst() instanceof Empty)) {
throw new JsonException("Attempting to close incomplete json stream");
}
}
原因
可能的原因可能是以下原因之一:
- 通过Selenium-server-standalone-3.13.0.jar和Selenium-java-3.13.0客户端jar存在多个与Selenium相关的jar。
- 通过JAR和Maven和/或Gradle存在多个与硒相关的罐子。
gson-2.8.2 的存在.jar正如 Selenium v3.13.0 的发行说明中明确提到的那样:
* Introduced our own JSON parser and outputter, allowing GSON to be removed from our dependencies.
溶液
- 如果使用手动下载的JAR文件,请删除所有与Selenium相关的外部JAR并添加新下载的JAR文件。
- 如果使用Maven或Gradle,请执行以下步骤:
- 通过 CLI:
mvn clean
(清除以前的依赖项( - 通过 CLI:
mvn install
(安装必要的依赖项( - 通过 CLI:
mvn test
(执行测试套件(
- 通过 CLI:
我使用以下配置修复它
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-chrome-driver</artifactId>
<version>3.141.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.5</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-remote-driver</artifactId>
<version>3.141.5</version>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.1.0</version>
</dependency>
只需使用以下依赖项,它应该可以解决您的问题。
不要忘记重建。
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>6.1.0</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server</artifactId>
<version>3.141.5</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.14.0</version>
</dependency>