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

jinyang.deng · 2017年09月07日 · 最后由 liqianna2 回复于 2018年09月20日 · 2564 次阅读

在测试类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

最佳回复
george.he 回复

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

共收到 30 条回复 时间 点赞

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

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

u1452407011 回复

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

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

czw 回复

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

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

jinyang.deng 回复

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

u1452407011 回复

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

czw 回复

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

u1452407011 回复

谢谢,我试了一下只写

不写

也是可以的

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

xiaohengdada 回复

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

jinyang.deng 回复

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

jinyang.deng 回复

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

george.he 回复

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

仅楼主可见

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

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

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

barana 回复

已统一回复

qingtian1110 回复

已统一回复,

cece0417 回复

已回复,答案在19楼

george.he 回复

答案在19楼

jiawei0113 回复

19楼已回复

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

liqianna2 回复

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

仅楼主可见
28楼 已删除
liqianna2 回复

进入app按钮是个什么意思

仅楼主可见

已解决,谢谢

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