Python - 使用MonkeyRunner关闭wifi

时间:2014-12-04 20:18:14

标签: android python monkeyrunner

以下Python脚本将与MonkeyRunner一起运行:

from com.android.monkeyrunner import MonkeyDevice, MonkeyRunner, MonkeyView, MonkeyImage

my_device = MonkeyRunner.waitForConnection()

activity = "android.settings.WIFI_SETTINGS"
my_device.startActivity(action=activity)

屏幕上会弹出WiFi设置。

如何检查 WiFi状态 是什么? 如果打开它,怎么能把它关掉?

可以使用ADB命令完成:

wifistate = os.popen("adb -s emulator-5554 shell getprop wlan.driver.status")

但如果不打开流程怎么办呢。

1 个答案:

答案 0 :(得分:2)

此脚本可以使用 AndroidViewClient/culebra 自动生成,然后进行轻微编辑以处理 ON / OFF 案例,这正是您所需要的:< / p>

#! /usr/bin/env python
# -*- coding: utf-8 -*-
'''
Copyright (C) 2013-2014  Diego Torres Milano
Created on 2014-12-05 by Culebra v8.18.1
                      __    __    __    __
                     /  \  /  \  /  \  /  \ 
____________________/  __\/  __\/  __\/  __\_____________________________
___________________/  /__/  /__/  /__/  /________________________________
                   | / \   / \   / \   / \   \___
                   |/   \_/   \_/   \_/   \    o \ 
                                           \_____/--<
@author: Diego Torres Milano
@author: Jennifer E. Swofford (ascii art snake)
'''


import re
import sys
import os


import unittest

from com.dtmilano.android.viewclient import ViewClient, CulebraTestCase


class CulebraTests(CulebraTestCase):

    @classmethod
    def setUpClass(cls):
        cls.kwargs1 = {'ignoreversioncheck': False, 'verbose': False, 'ignoresecuredevice': False}
        cls.kwargs2 = {'startviewserver': True, 'forceviewserveruse': False, 'autodump': False, 'ignoreuiautomatorkilled': True}
        cls.options = {'start-activity': None, 'multi-device': False, 'unit-test-class': True, 'save-screenshot': None, 'use-dictionary': False, 'dictionary-keys-from': 'id', 'scale': 1, 'find-views-with-content-description': True, 'window': -1, 'orientation-locked': None, 'save-view-screenshots': None, 'find-views-by-id': True, 'do-not-verify-initial-screen-dump': True, 'use-regexps': False, 'auto-regexps': None, 'use-jar': False, 'verbose-comments': False, 'gui': True, 'find-views-with-text': True, 'prepend-to-sys-path': False, 'output': '/Users/diego/tmp/settings-wifi.py', 'unit-test-method': None, 'interactive': False}
        cls.sleep = 5

    def setUp(self):
        super(CulebraTests, self).setUp()

    def tearDown(self):
        super(CulebraTests, self).tearDown()

    def preconditions(self):
        if not super(CulebraTests, self).preconditions():
            return False
        return True

    def testSomething(self):
        if not self.preconditions():
            self.fail('Preconditions failed')

        self.vc.dump(window=-1)
        self.vc.findViewWithTextOrRaise(re.compile(u'Wi.Fi')).touch()
        self.vc.sleep(CulebraTests.sleep)
        self.vc.dump(window=-1)
        try:
            self.assertIsNotNone(self.vc.findViewWithText(u'OFF'))
        except:
            self.vc.findViewWithTextOrRaise(u'ON').touch()


if __name__ == '__main__':
    CulebraTests.main()

我使用culebra GUI创建了它,点击 Wi-Fi 项,使用culebra的CTRL-T检查状态,然后点击切换。然后我手动编辑它,为开启/关闭添加try/except块。