Search code examples
androidpythonmonkeyrunner

Python - turn off wifi with MonkeyRunner


The following Python script will be run with MonkeyRunner:

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

my_device = MonkeyRunner.waitForConnection()

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

The WiFi setting will pop up on the screen.

How can check what the WiFi status is? And in case it is turned on, how can turn it off?

It can be done with an ADB command:

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

But how can it be done without opening a process.


Solution

  • This script, that can be generated automatically using AndroidViewClient/culebra and then being slightly edited to handle the ON/OFF case does exactly what you need:

    #! /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()
    

    I created it using culebra GUI, clicking on the Wi-Fi item, checking the state using culebra's CTRL-T, and clicking on the toggle. Then I manually edit it to add the try/except block for ON/OFF.