2014-12-04 101 views
0

以下Python腳本將MonkeyRunner運行:Python的 - 關閉wifi與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") 

但怎麼能不打開的過程來完成。

回答

2

這個腳本,它可以自動使用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檢查狀態,然後單擊切換。然後我手動編輯它以添加try/except塊爲ON/OFF

+0

謝謝,我正試圖獲得WiFi狀態。 是否有可能做某事: activity =「android.settings.WIFI_SETTINGS」 my_device.startActivity(action = activity) – Empario 2014-12-30 04:55:22

+0

是的,試試'culebra --help',你會看到'-a,--start -activity = COMPONENT在dump'之前啓動Activity – 2014-12-30 06:00:38