【问题标题】:Python - turn off wifi with MonkeyRunnerPython - 使用 MonkeyRunner 关闭 wifi
【发布时间】:2014-12-04 20:18:14
【问题描述】:

以下 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")

但是不打开进程怎么办呢。

【问题讨论】:

    标签: android python monkeyrunner


    【解决方案1】:

    这个脚本可以使用 AndroidViewClient/culebra 自动生成,然后稍作编辑以处理 ON/OFF 情况,这正是您所需要的:

    #! /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 检查状态,然后单击切换。然后我手动编辑它,为 ON/OFF 添加try/except 块。

    【讨论】:

    • 谢谢,我正在尝试获取 WiFi 状态。是否有可能以某种方式处理:activity = "android.settings.WIFI_SETTINGS" my_device.startActivity(action=activity)
    • 是的,试试culebra --help,你会看到-a, --start-activity=COMPONENT starts Activity before dump
    猜你喜欢
    • 2017-10-30
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多