2012-12-21 124 views
3

我想在android中使用uiautomator工具來開啓wifi作爲測試用例的一部分。我試着在uiautomator測試情況下,使用下面的代碼:如何在android設備的uiautomator測試用例中打開wifi?

WifiManager wi = (WifiManager) this.getSystemService(Context.WIFI_SERVICE); 
     if(wi.isWifiEnabled()){ 
     wi.setWifiEnabled(false); 
     }else{ 
     wi.setWifiEnabled(true); 
    } 

,但它給了這個錯誤:

"getSystemservice" method is undefined for Mainclass

回答

-3

你不能做到這一點。 UI Automator測試不會作爲Android框架的一部分運行,因此它無法訪問Android系統服務。它旨在測試用戶界面;它並沒有聲稱是一個全功能的測試框架。在運行測試之前手動打開WiFi。

+0

另外我想知道,如果我們可以在uiautomator中運行測試用例時檢查wifi連接是否處於活動狀態,即使我手動開啓wifi也可以檢查數據包是否交換。那可能嗎? – user1907534

+0

既然我們可以在這個中使用android的所有assert函數,爲什麼不用這個函數呢? – user1907534

0

您可以啓用或通過亞行停用WiFi如下

adb shell sqlite3 /data/data/com.android.providers.settings/databases/settings.db update secure set value=1 where name='wifi_on'; .exit

但你不能使用uiautomator工具做同樣的

+1

以上命令給sqlite3命令找不到錯誤 – user1907534

7

實際上,你可以使用UIAutomator設置無線網絡設置和關閉。我今天晚上寫了代碼:)

這是代碼。你可以把它添加到Android例子是在這裏http://developer.android.com/tools/testing/testing_ui.html

在類

private enum OnOff { 
    Off, 
    On 
}; 

添加新代碼的頂部添加以下枚舉後:

// Validate that the package name is the expected one 
    UiObject settingsValidation = new UiObject(new UiSelector() 
    .packageName("com.android.settings")); 
    assertTrue("Unable to detect Settings", settingsValidation.exists()); 

這是新的代碼:

UiSelector settingsItems = new UiSelector().className(android.widget.TextView.class.getName()); 
    UiObject wiFi = appViews.getChildByText(settingsItems, "Wi-Fi"); 

    // We can click on Wi-Fi, e.g. wiFi.clickAndWaitForNewWindow(); 
    // So we know we have found the Wi-Fi setting 

    UiSelector switchElement = new UiSelector().className(android.widget.Switch.class.getName()); 
    setSwitchTo(OnOff.Off); // Or set it to On as you wish :) 
} 

private void setSwitchTo(OnOff value) throws UiObjectNotFoundException { 

    String text; 
    UiObject switchObject = getSwitchObject(); 
    for (int attempts = 0; attempts < 5; attempts++) { 
     text = switchObject.getText(); 
     boolean switchIsOn = switchObject.isChecked(); 
     final OnOff result; 
     if (switchIsOn) { 
      result = OnOff.On; 
     } else { 
      result = OnOff.Off; 
     } 

     System.out.println("Value of switch is " + switchObject.isSelected() + ", " + text + ", " + switchIsOn); 
     if (result == value) { 
      System.out.println("Switch set to correct value " + result); 
      break; 
     } else { 
      switchObject.click(); 
     } 
    } 
} 

private UiObject getSwitchObject() { 
    UiObject switchObject = new UiObject(new UiSelector().className(android.widget.Switch.class.getName())); 
    assertTrue("Unable to find the switch object", switchObject.exists()); 
    String text; 
    return switchObject; 
} 

該循環是爲了補償我觀察到的一些行爲,其中點擊似乎沒有改變開關位置。

+0

如果有多個switch元素會發生什麼情況? – Rilwan

+0

@Rilwan您可以在Android中提供一個示例,其中有一個項目有多個開關元素嗎?如果是這樣,歡迎您在StackOverflow上發佈一個新問題,並從此鏈接到它。 – JulianHarty

+0

這裏的Wi-Fi上面的代碼將工作。但是如果你在三星手機上看NFC,如果你點擊NFC並轉到NFC頁面 - >同樣你可以看到兩個開關。因此,如果我們修改代碼更通用這樣'UIObject類項目= itemsList.getChildByText(新UiSelector() \t \t \t \t類名(LinearLayout.class.getName()),switch_label,真的。); \t \t UIObject的switchObject = item.getChild(新UiSelector() \t \t \t \t類名(android.widget.Switch.class.getName()));'這是否有道理? – Rilwan

1

在我與UIAutomator套件測試中,我使用:

Runtime.getRuntime().exec("\"svc wifi disable\"") 

Runtime.getRuntime().exec("\"svc wifi enable\"") 

Runtime.getRuntime().exec("\"svc data disable\"") 

Runtime.getRuntime().exec("\"svc data enable\"") 
0

因爲UIAutomator不是Android context您收到此錯誤;因此,你不能訪問getSystemservice方法

UIAutomator基於UI自動化只能訪問xml地圖應用程序,而不是Android APIs

可以使用Android shell命令的建議啓用/禁用無線網絡由Eric Rommel

並不需要進行驗證,因爲這些類型的系統指令(需要根訪問)

Runtime.getRuntime().exec("\"svc wifi disable\"") 

Runtime.getRuntime().exec("\"svc wifi enable\"") 

或者,也通過使用UIAutomator到ENA UI元素遍歷ble/disable WiFi並驗證爲JulianHarty

1

建議在Android 4.2和4上使用。4

在代碼中開啓Android WLAN設置:

final Intent intent = new Intent(WifiManager.ACTION_PICK_WIFI_NETWORK); 
mContext.startActivity(intent); 

要與UiAutomator點擊on/off開關(你確定你是在良好的活性後):

public void enableWifiOnAndroidWifiSettings(boolean enabled) throws UiObjectNotFoundException { 
    final UiSelector wifiSwitchSelector = new UiSelector().className(android.widget.Switch.class.getName()); 
    UiObject wifiSwitch = UiDevice.getInstance(sInstrumentation).findObject(wifiSwitchSelector); 
    if (wifiSwitch.waitForExists(5000) && wifiSwitch.isEnabled()) { 
     if (wifiSwitch.isChecked() != enabled) { 
      wifiSwitch.click(); 
     } 
    } 
} 

已知限制:它正在搜索第一個可用的Switch。如果你有自定義ROM,或者Android設置應用程序將來會發展,那可能還不夠。