Macaca macaca 在 python 下 driver.quit () 没有反应,仍然显示在最后操作页面

linpengcheng · 2017年02月09日 · 最后由 linpengcheng 回复于 2017年02月10日 · 2392 次阅读

代码很简单,就是微信打开到我的钱包页面,然后 driver.quit()
只是 macaca 的一个 bug 还是有什么解决办法?
python2.7
windows7SP1
macaca 1.1.21

from macaca.webdriver import WebDriver
from time import sleep

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0'
desired_caps['udid'] = 'C4Y5T16810004018'
desired_caps['reuse'] = '3'
desired_caps['package'] = 'com.tencent.mm'
desired_caps['activity'] = '.ui.LauncherUI'
desired_caps['androidProcess'] ='com.tencent.mm:tools'

server_url = {
    'hostname': '127.0.0.1',
    'port': 3456
}

driver = WebDriver(desired_caps, server_url)
driver.init()
sleep(2)
driver.element_by_xpath('//*[@text="我"]').click()
driver.element_by_xpath('//*[@text="钱包"]').click()
sleep(3)
driver.quit()
print 'Done'

跑完之后再我的钱包页面就停了,没有退出 app。
下面是 log


>> index.js:17:12 [master] pid:5048 webdriver server start with config:
 { port: 3456,
  verbose: true,
  always: true,
  ip: '192.168.1.104',
  host: 'LiN-PC',
  loaded_time: '2017-02-09 22:35:05' }
>> middlewares.js:17:10 [master] pid:5048 base middlewares attached
>> router.js:127:10 [master] pid:5048 router set
>> webdriver sdk launched
>> responseHandler.js:11:12 [master] pid:5048 Recieve HTTP Request from Client:
method: POST url: /wd/hub/session, jsonBody: {"desiredCapabilities":{"androidPro
cess":"com.tencent.mm:tools","reuse":"3","package":"com.tencent.mm","udid":"C4Y5
T16810004018","autoAcceptAlerts":"True","activity":".ui.LauncherUI","platformNam
e":"Android"}}
>> session.js:47:10 [master] pid:5048 Creating session, sessionId: 582366a5-c3f5
-4ac4-b21e-3cd3ca804a1b.
INSTRUMENTATION_STATUS: numtests=1
INSTRUMENTATION_STATUS: stream=
com.android.uiautomator.client.Initialize:
INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
INSTRUMENTATION_STATUS: test=testStartServer
INSTRUMENTATION_STATUS: class=com.android.uiautomator.client.Initialize
INSTRUMENTATION_STATUS: current=1
INSTRUMENTATION_STATUS_CODE: 1

>> socket server ready
>> socket client ready
recive: {"cmd":"wake","args":{}}

return: {"success":true,"data":{"status":0,"value":true}}

recive: {"cmd":"alert","args":{"action":"registerAutoAcceptAlerts"}}

return: {"success":true,"data":{"status":0,"value":true}}

>> responseHandler.js:47:14 [master] pid:5048 Send HTTP Respone to Client: {"ses
sionId":"582366a5-c3f5-4ac4-b21e-3cd3ca804a1b","status":0,"value":"{\"androidPro
cess\":\"com.tencent.mm:tools\",\"reuse\":\"3\",\"package\":\"com.tencent.mm\",\
"udid\":\"C4Y5T16810004018\",\"autoAcceptAlerts\":\"True\",\"activity\":\".ui.La
uncherUI\",\"platformName\":\"Android\"}"}
>> responseHandler.js:11:12 [master] pid:5048 Recieve HTTP Request from Client:
method: POST url: /wd/hub/session/582366a5-c3f5-4ac4-b21e-3cd3ca804a1b/element,
jsonBody: {"using":"xpath","value":"//*[@text=\"我\"]"}
recive: {"cmd":"find","args":{"strategy":"xpath","selector":"//*[@text=\"我\"]",
"multiple":false}}

return: {"success":true,"data":{"status":0,"value":{"ELEMENT":"1"}}}

>> responseHandler.js:47:14 [master] pid:5048 Send HTTP Respone to Client: {"ses
sionId":"582366a5-c3f5-4ac4-b21e-3cd3ca804a1b","status":0,"value":"{\"ELEMENT\":
\"1\"}"}
>> responseHandler.js:11:12 [master] pid:5048 Recieve HTTP Request from Client:
method: POST url: /wd/hub/session/582366a5-c3f5-4ac4-b21e-3cd3ca804a1b/element/1
/click, jsonBody: {}
recive: {"cmd":"click","args":{"elementId":"1"}}

>> responseHandler.js:47:14 [master] pid:5048 Send HTTP Respone to Client: {"ses
sionId":"582366a5-c3f5-4ac4-b21e-3cd3ca804a1b","status":0,"value":"true"}
return: {"success":true,"data":{"status":0,"value":true}}

>> responseHandler.js:11:12 [master] pid:5048 Recieve HTTP Request from Client:
method: POST url: /wd/hub/session/582366a5-c3f5-4ac4-b21e-3cd3ca804a1b/element,
jsonBody: {"using":"xpath","value":"//*[@text=\"钱包\"]"}
recive: {"cmd":"find","args":{"strategy":"xpath","selector":"//*[@text=\"钱包\"]
","multiple":false}}

return: {"success":true,"data":{"status":0,"value":{"ELEMENT":"2"}}}

>> responseHandler.js:47:14 [master] pid:5048 Send HTTP Respone to Client: {"ses
sionId":"582366a5-c3f5-4ac4-b21e-3cd3ca804a1b","status":0,"value":"{\"ELEMENT\":
\"2\"}"}
>> responseHandler.js:11:12 [master] pid:5048 Recieve HTTP Request from Client:
method: POST url: /wd/hub/session/582366a5-c3f5-4ac4-b21e-3cd3ca804a1b/element/2
/click, jsonBody: {}
recive: {"cmd":"click","args":{"elementId":"2"}}

return: {"success":true,"data":{"status":0,"value":true}}

>> responseHandler.js:47:14 [master] pid:5048 Send HTTP Respone to Client: {"ses
sionId":"582366a5-c3f5-4ac4-b21e-3cd3ca804a1b","status":0,"value":"true"}
>> responseHandler.js:11:12 [master] pid:5048 Recieve HTTP Request from Client:
method: DELETE url: /wd/hub/session/582366a5-c3f5-4ac4-b21e-3cd3ca804a1b, jsonBo
dy: {}
>> session.js:80:12 [master] pid:5048 Delete session, sessionId: 582366a5-c3f5-4
ac4-b21e-3cd3ca804a1b
>> responseHandler.js:47:14 [master] pid:5048 Send HTTP Respone to Client: {"ses
sionId":"582366a5-c3f5-4ac4-b21e-3cd3ca804a1b","status":0}

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 6 条回复 时间 点赞

现在还没有关闭,重启 app 的方法,不过应该快了,github 上有这个需求

#1 楼 @michael_wang 那现在的 driver.quit() 的作用是什么呢? 我切换到 webview 后 driver.quit() 后,电脑里的 chromedriver.exe 进程还保留着,导致下次再跑的时候 pid 诶占用,开启两个 chromedriver.exe 进程。 现在临时的办法是 os.system('taskkill /f /im chromedriver.exe') 切换到 webview 前先杀进程。

driver.init() 会初始化生成一个 session_id, 我的理解 driver.quit() 是消除这个 session_id 的。

#3 楼 @michael_wang 的确是的 ,源码这样写的。这样是关闭不了 app 的吗

@fluent
    def quit(self):
        """Quit the driver.

        Support:
            Android iOS Web(WebView)

        Returns:
            WebDriver Object.
        """
        self._execute(Command.QUIT)


 QUIT = Endpoint(
        'DELETE',
        '/session/{session_id}'

#4 楼 @cynic 你的问题应该在 chromedriver.exe 为什么没有关闭, app 是否关闭对你没有影响吧。如果一定要关闭,使用
adb shell am force-stop package_name

#5 楼 @michael_wang 其实算两个问题 :
问题 1
chromedriver.exe 为什么没有关闭
解决办法:os.system('taskkill /f /im chromedriver.exe')
问题 2
driver.quit() 后 app 没有关闭
解决方法:os.system('adb shell am force-stop com.tencent.mm')

十分感谢

linpengcheng 关闭了讨论 02月10日 13:00
linpengcheng 关闭了讨论 02月11日 13:04
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册