Appium 请教:appium1.6.5 启动 app 的时候报错 A session is either terminated or not started

小小测试 · 2017年09月07日 · 最后由 一条虫 回复于 2019年01月08日 · 5237 次阅读

在测试类 A 中启动 app 完成测试后(driver 写在一个专门写参数的模块里,没有放在 setup()中),再 teardown 中 driver.quit()关闭 app,之后继续运行测试类 B,此时报错 A session is either terminated or not started 。

直接在测试类 B 的 setup()中写上 self.driver = driver 或者不写都一样报上面的错误

appium 日志:

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Finding '//android.widget.Button[@text='账户退出']' using 'XPATH' with the contextId: '' multiple: false

[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"value":"Could not find an element using supplied strategy. ","status":7}
[AndroidBootstrap] Received command result from bootstrap
[HTTP] <-- POST /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9/element 500 19 ms - 164
[HTTP] --> DELETE /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9 {}
[MJSONWP] Calling AppiumDriver.deleteSession() with args: ["37c57813-ef00-4ae3-aa3d-82e7497a88b9"]
[BaseDriver] Event 'quitSessionRequested' logged at 1504771567658 (16:06:07 GMT+0800 (中国标准时间))
[AndroidDriver] Shutting down Android driver
[AndroidDriver] Stopping chromedriver for context WEBVIEW_com.changhong.cloudtrip
[Chromedriver] Changed state to 'stopping'
[JSONWP Proxy] Proxying [DELETE /] to [DELETE http://127.0.0.1:8000/wd/hub/session/7265d1da25b7e58a0f1adfe64b33c6c4] with no body
[JSONWP Proxy] Got response with status 200: "{\"sessionId\":\"7265d1da25b7e58a0f1adfe64b33c6c4\",\"status\":0,\"value\":null}"
[Chromedriver] Changed state to 'stopped'
[AndroidDriver] Resetting IME to io.appium.android.ime/.UnicodeIME
[ADB] Getting connected devices...
[ADB] 1 device(s) connected
[ADB] Running 'D:\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","127.0.0.1:62001","shell","ime","set","io.appium.android.ime/.UnicodeIME"]
[ADB] Getting connected devices...
[ADB] 1 device(s) connected
[ADB] Running 'D:\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","127.0.0.1:62001","shell","am","force-stop","com.changhong.cloudtrip"]
[ADB] Pressing the HOME button
[ADB] Getting connected devices...
[ADB] 1 device(s) connected
[ADB] Running 'D:\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","127.0.0.1:62001","shell","input","keyevent",3]
[AndroidBootstrap] Sending command to android: {"cmd":"shutdown"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got data from client: {"cmd":"shutdown"}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Got command of type SHUTDOWN
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Returning result: {"value":"OK, shutting down","status":0}
[AndroidBootstrap] [BOOTSTRAP LOG] [debug] Closed client connection
[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: numtests=1

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=.

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: test=testRunServer

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: current=1

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS_CODE: 0

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS: stream=

[AndroidBootstrap] [UIAUTO STDOUT] Test results for WatcherResultPrinter=.

[AndroidBootstrap] [UIAUTO STDOUT] Time: 607.044

[AndroidBootstrap] [UIAUTO STDOUT] OK (1 test)

[AndroidBootstrap] [UIAUTO STDOUT] INSTRUMENTATION_STATUS_CODE: -1

[AndroidBootstrap] Received command result from bootstrap
[UiAutomator] Shutting down UiAutomator
[UiAutomator] Moving to state 'stopping'
[UiAutomator] UiAutomator shut down normally
[UiAutomator] Moving to state 'stopped'
[ADB] Attempting to kill all uiautomator processes
[ADB] Getting all processes with uiautomator
[ADB] Getting connected devices...
[ADB] 1 device(s) connected
[ADB] Running 'D:\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","127.0.0.1:62001","shell","ps"]
[ADB] No uiautomator process found to kill, continuing...
[UiAutomator] Moving to state 'stopped'
[Logcat] Stopping logcat capture
[ADB] Getting connected devices...
[ADB] 1 device(s) connected
[ADB] Running 'D:\android-sdk\platform-tools\adb.exe' with args: ["-P",5037,"-s","127.0.0.1:62001","shell","am","force-stop","io.appium.unlock"]
[AndroidDriver] Not cleaning generated files. Add clearSystemFiles capability if wanted.
[Appium] Removing session 37c57813-ef00-4ae3-aa3d-82e7497a88b9 from our master session list
[BaseDriver] Event 'quitSessionFinished' logged at 1504771574330 (16:06:14 GMT+0800 (中国标准时间))
[MJSONWP] Received response: null
[MJSONWP] But deleting session, so not returning
[MJSONWP] Responding to client with driver.deleteSession() result: null
[HTTP] <-- DELETE /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9 200 6674 ms - 76
[HTTP] --> GET /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9/window/current/size {}
[HTTP] <-- GET /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9/window/current/size 404 2 ms - 131
[HTTP] --> DELETE /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9 {}
[HTTP] <-- DELETE /wd/hub/session/37c57813-ef00-4ae3-aa3d-82e7497a88b9 404 2 ms - 131

最佳回复
luckyhey 回复

你怎么解决的啊?我也遇到了

共收到 31 条回复 时间 点赞

求助求助,哪位大神来看看啊,

Could not find an element using supplied strategy.
找不到控件,这不是已经告诉你了么

edsion 回复

但根本原因是会话没有开启或者根本就没有启动会话,所以才找不到控件,我是想问为什么会出现不能启动会话的错误;我每个测试类拿出来单独跑就可以,连续一起跑就只能第一个可以,到第二个的时候就报会话不能能启动的错误

应该是因为你跑跑完第一个脚本之后,执行了 tearDown 里的退出会话操作,导致后边的脚本运行的时候报错吧~

于归 回复

不知道,我第一个 teardown 中写的 driver.quit(),这没错吧

tearDown() 里已经把 driver 结束掉了。下一条 case 里要重新初始化一个 driver(self.driver = driver 不行,因为只是给变量赋值)

小小测试 回复

你可以尝试加一个装饰器@classmethod,之前有看过文章说好像可以解决你这个问题,但是我还没尝试过

edsion 回复

那怎么重新初始化呢,在测试类下面写一个init方法,然后放入 self.driver = driver 吗?

于归 回复

这个在第一个 case 的 setup 上加还是第二个 case 上加

edsion 回复

谢谢,我试了一下只写

不写

也是可以的

每个 case 添加一个装饰器,装饰器首先执行 start 的初始操作,每个 case 执行结束后执行 teardown 里的清理操作

笑哼 回复

谢谢,问题已经解决了,之前是因为我有几个用例是连着跑的,中间不让它关闭,所以我把 driver 写到测试类外面的(公用的)。然后在关闭后,运行其他脚本时再启动的时候直接在 setup 里写的 self.driver=driver,所以会报错;重新初始化一下就可以了

小小测试 回复

可以分享一下你解决后的代码吗?

小小测试 回复

最后怎么解决的呢?我也遇到了这个问题。

luckyhey 回复

你怎么解决的啊?我也遇到了

仅楼主可见

楼主怎么解决的?我也遇到这个问题了,解决了就要把方案发出来共享一下撒..

不好意思,因为自动化是学习阶段,加上工作比较忙,所以没有一直做自动化。很久没登录了。
现在把解决方法说一下(其实上面的对话中已经说出来了):
如果多个 case 连续跑,当上一个用例脚本关闭后(driver.quit()),下一个测试类中的 setUp 中是不能用 self.driver=driver 的,这样的话 driver 根本没有初始化。所以在 setup 中再次把 driver 初始化需要这么写:

以上全部是个人理解(亲测能解决问题)。如果说的不对,请大佬指点

沙丘 回复

已统一回复

黄庆 回复

已统一回复,

cece0417 回复

已回复,答案在 19 楼

luckyhey 回复

答案在 19 楼

jiawei0113 回复

19 楼已回复

你好,请问你是怎么写的,可以贴具体的代码看一下吗,我按照你说的方法来写,还是不行

测试小白 回复

我贴代码了啊。总不能所有的都贴啊。那样会有好多

仅楼主可见
5楼 已删除
测试小白 回复

进入 app 按钮是个什么意思

仅楼主可见

已解决,谢谢

仅楼主可见
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册