Appium无法在真实iPad设备上启动新会话求助
在真实iPad设备上执行iOS应用自动化测试,此前代码运行正常,今日突然报错。未升级Xcode、Node.js、Java或Appium,也未修改任何代码。报错发生在driver = new IOSDriver(new URL("http://127.0.0.1:4723/"),options);这一行。
实现代码
public AppiumDriver runOnRealDevice() { try { XCUITestOptions options = new XCUITestOptions(); options.setDeviceName("my iPad"); options.setPlatformVersion("16.5.1"); options.setWdaLaunchTimeout(Duration.ofSeconds(20)); options.setCapability("xcodeOrgId","......"); options.setCapability("xcodeSigningId","iPhone Developer"); options.setCapability("udid","......"); driver = new IOSDriver(new URL("http://127.0.0.1:4723/"),options); driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(15)); Map<String,String> params = new HashMap<String,String>(); params.put("bundleId", "com.myapplebundleidentfier"); driver.executeScript("mobile:launchApp",params); }catch (Exception e) { System.out.println("Here12 "+e.getLocalizedMessage()); e.printStackTrace(); } return driver; }
完整错误信息
StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console... Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure. Host info: host: '-MacBook-Air.local', ip: 'fe80:0:0:0:1044:a4fe:6300:fc53%en0' Build info: version: '4.11.0', revision: '040bc5406b' System info: os.name: 'Mac OS X', os.arch: 'aarch64', os.version: '13.4.1', java.version: '17.0.6' Driver info: io.appium.java_client.ios.IOSDriver Command: [null, newSession {capabilities=[{appium:udid=myudid, appium:xcodeOrgId=myxoceorgid, appium:wdaLaunchTimeout=20000, appium:xcodeSigningId=iPhone Developer, appium:deviceName=iPad, platformName=IOS, appium:automationName=XCuiTest, appium:platformVersion=16.5.1}], desiredCapabilities=Capabilities {appium:automationName: XCuiTest, appium:deviceName: Pritish’s iPad, appium:platformVersion: 16.5.1, appium:udid:dsssddsdssd, appium:wdaLaunchTimeout: 20000, appium:xcodeOrgId: dsdsdsdsdssd, appium:xcodeSigningId: iPhone Developer, platformName: IOS}}] Capabilities {appium:automationName: XCuiTest, appium:deviceName: my iPad, appium:platformVersion: 16.5.1, appium:udid: sddsdsdsds, appium:wdaLaunchTimeout: 20000, appium:xcodeOrgId: ddsds, appium:xcodeSigningId: iPhone Developer, platformName: IOS} at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:536) at io.appium.java_client.AppiumDriver.startSession(AppiumDriver.java:229) at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:159) at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:80) at io.appium.java_client.AppiumDriver.<init>(AppiumDriver.java:92) at io.appium.java_client.ios.IOSDriver.<init>(IOSDriver.java:106) at WebPortal.IOSBaseTest.runOnRealDevice(IOSBaseTest.java:66) at WebPortal.IOSBaseTest.setup(IOSBaseTest.java:32) at WebPortal.IOSBaseTest.getDriver(IOSBaseTest.java:25) at stepDefinitions.TA124_ComprehensiveCI_StepDefinitions.<init>(TA124_ComprehensiveCI_StepDefinitions.java:36) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) at io.cucumber.core.runtime.ObjectFactoryServiceLoader$DefaultJavaObjectFactory.cacheNewInstance(ObjectFactoryServiceLoader.java:141) at io.cucumber.core.runtime.ObjectFactoryServiceLoader$DefaultJavaObjectFactory.getInstance(ObjectFactoryServiceLoader.java:133) at io.cucumber.java.AbstractGlueDefinition.invokeMethod(AbstractGlueDefinition.java:47) at io.cucumber.java.JavaStepDefinition.execute(JavaStepDefinition.java:29) at io.cucumber.core.runner.CoreStepDefinition.execute(CoreStepDefinition.java:66) at io.cucumber.core.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:63) at io.cucumber.core.runner.ExecutionMode$1.execute(ExecutionMode.java:10) at io.cucumber.core.runner.TestStep.executeStep(TestStep.java:92) at io.cucumber.core.runner.TestStep.run(TestStep.java:64) at io.cucumber.core.runner.PickleStepTestStep.run(PickleStepTestStep.java:51) at io.cucumber.core.runner.TestCase.run(TestCase.java:104) at io.cucumber.core.runner.Runner.runPickle(Runner.java:73) at io.cucumber.core.runtime.Runtime.lambda$execute$5(Runtime.java:110) at io.cucumber.core.runtime.CucumberExecutionContext.runTestCase(CucumberExecutionContext.java:117) at io.cucumber.core.runtime.Runtime.lambda$execute$6(Runtime.java:110) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264) at io.cucumber.core.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:233) at java.base/java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:123) at io.cucumber.core.runtime.Runtime.lambda$run$2(Runtime.java:86) at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197) at java.base/java.util.stream.SliceOps$1$1.accept(SliceOps.java:200) at java.base/java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1602) at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129) at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527) at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513) at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499) at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:921) at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) at java.base/java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:682) at io.cucumber.core.runtime.Runtime.run(Runtime.java:87) at io.cucumber.core.cli.Main.run(Main.java:92) at cucumber.api.cli.Main.run(Main.java:30) at cucumber.api.cli.Main.main(Main.java:15) Caused by: java.lang.NoSuchMethodError: 'java.lang.String org.openqa.selenium.json.Json.toJson(java.lang.Object, int)' at org.openqa.selenium.remote.NewSessionPayload.create(NewSessionPayload.java:126) at org.openqa.selenium.remote.NewSessionPayload.create(NewSessionPayload.java:111) at org.openqa.selenium.remote.NewSessionPayload.create(NewSessionPayload.java:105) at io.appium.java_client.remote.AppiumProtocolHandshake.createSession(AppiumProtocolHandshake.java:101) at io.appium.java_client.remote.AppiumCommandExecutor.createSession(AppiumCommandExecutor.java:194) at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:262) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:518) ... 48 more
报错根源
java.lang.NoSuchMethodError: 'java.lang.String org.openqa.selenium.json.Json.toJson(java.lang.Object, int)'
这个错误是典型的依赖版本冲突问题,核心是Selenium与Appium Java Client版本不兼容,导致找不到指定方法。即使未手动升级依赖,也可能是构建工具自动拉取了不兼容版本,或本地依赖缓存损坏。
对齐Selenium与Appium Java Client版本
Appium Java Client对Selenium有严格版本依赖,比如Appium Java Client 8.x对应Selenium 4.x,需确保版本匹配。可直接使用Appium提供的兼容版本,让构建工具自动处理依赖,避免手动指定Selenium版本。清理并重新下载依赖
本地依赖缓存可能损坏,执行以下操作:- Maven:
mvn clean install -U(强制更新依赖并清理缓存) - Gradle:
gradle clean build --refresh-dependencies
- Maven:
检查依赖树并排除冲突
用构建工具查看依赖树,找出是否有其他依赖引入了不兼容的Selenium版本:- Maven:
mvn dependency:tree - Gradle:
gradle dependencies
找到冲突依赖后,添加排除规则,例如Maven中:
<dependency> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <version>xxx</version> <exclusions> <exclusion> <groupId>org.seleniumhq.selenium</groupId> <artifactId>selenium-java</artifactId> </exclusion> </exclusions> </dependency>- Maven:
验证Appium服务兼容性
重启Appium服务,运行appium --version查看版本,确保与Java Client版本匹配(如Appium 2.x对应Java Client 8.x+)。
内容的提问来源于stack exchange,提问作者BraveEvidence

