问题
I am getting the above Json exception after I updated my TestNG to 6.14.3 and Selenium to 3.13.0. I am using ChromeDriver() for my automation. Here's a brief idea about my code:
@BeforeSuite:
System.setProperty("webdriver.chrome.driver", "path to chrome driver");
@BeforeMethod:
driver = new ChromeDriver();
@AfterMethod:
driver.quit();
My previous Versions were 6.11.0 and 3.7.1 respectively for TestNG and Selenium.
The exception is coming when the code tries to start a WebDriver session.
The full exception is:
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)
回答1:
This error message:
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
...implies that java.lang.NoSuchMethodError was raised while JVM was trying to escape some characters.
It's pretty tough to analyze the error in absence of the following information pertaining to your Test Environment:
- selenium-server-standalone-3.13.0.jar or selenium-java-3.13.0 client.
- JARs or Maven or Gradle
But this error is coming out of the following source code:
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");
}
}
Reason
The possible reason can be one of the following:
- Presence of multiple Selenium related jars through selenium-server-standalone-3.13.0.jar and selenium-java-3.13.0 client jars.
- Presence of multiple Selenium related jars through JARs and Maven and/or Gradle.
Presence of gson-2.8.2.jar as the Release Notes of Selenium v3.13.0 clearly mentions:
* Introduced our own JSON parser and outputter, allowing GSON to be removed from our dependencies.
Solution
- If using manually downloaded JAR files, remove all the Selenium related External JARs and add the newly downloaded one.
- If using Maven or Gradle, perform the following steps:
- Through CLI:
mvn clean
(to flush out previous dependencies) - Through CLI:
mvn install
(to install the necessary dependencies) - Through CLI:
mvn test
(to execute testsuite)
- Through CLI:
回答2:
I fix it use follow config
<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>
来源:https://stackoverflow.com/questions/51084484/org-openqa-selenium-json-jsonoutput-writeljava-lang-objectljava-lang-reflect-t