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

jinyang.deng · 发布于 2017年09月07日 · 最后由 cece0417 回复于 2017年09月19日 · 670 次阅读

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

共收到 14 条回复
A491f2

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

6841

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

A491f2
6841u1452407011 回复

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

9498

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

A491f2
9498czw 回复

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

6841

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

9498
A491f2jinyang.deng 回复

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

A491f2
6841u1452407011 回复

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

A491f2
9498czw 回复

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

A491f2
6841u1452407011 回复

谢谢,我试了一下只写

不写

也是可以的

12759

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

A491f2
12759xiaohengdada 回复

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

12000
A491f2jinyang.deng 回复

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

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