1

我想为一些Android UI测试做UiAutomation,比如打开Wifi,并在提示输入Wifi用户名和密码时在文本字段中输入一些文本:http ://android.zedcn.com/reference/android/app/UiAutomation.html

如何调用 UiAutomation 类中的方法来完成此操作?1) 单击设置应用程序 2) 单击 Wifi 的切换图标 3) 单击网络 SSID 4) 在文本字段中输入用户名和密码

我很乐意获得一些入门提示。

4

1 回答 1

-2
There are couple of ways you can accomplish. I prefer each page of the device as single class and defining all the required methods for that page. Example: Wifi Page

Create following classes:
 - Lock Screen
 - Pin Entry
 - Home Screen
 - All App
 - Settings
 - Wifi
 - MoreOption Menu
 - Add Network Dialouge

LockScreenPage: Define following Methods:
 - VerifyLockScreenPage
 - SwipeLockImage

PinEntryPage: Define following methods:
 - VerifyPinEntryPage
 - EnterPin
 - PressEnter

HomeScreenPage: Define following methods:
 - VerifyHomeScreenPage
 - ClickAllAppButton

AllAppPage: Define following methods:
 - VerifyAllAppPage
 - IsAppPresent : used for searching the expected app. In Your case it would be settings
 - OpenApp : Once IsAppPresent returns true, OpenApp is called to open the expected app. In your case its Settings.

SettingsPage : Define Following methods:
 - VerifySettingPage
 - IsSettingOptionPresent: Used to search through the available options whether expected option is present or not. If present return true else false. In your case it would be Wifi.
 - clickOption: Once IsSettingOptionPresent returns true, open this expected option .

WiFiPage: Define following methods:
 - VerifyWiFiPage
 - TurnOnWiFi
 - TurnOffWiFi
 - IsWiFiConnected
 - ClickMoreOption

MoreOptionMenu: Define Following methods:
 - VerifyMoreOptionMenu
 - ClickAddNetwork

AddNetworkDialogue: Define following methods:
 - VerifyAddNetworkDialogue
 - EnterNetworkId
 - ClickAndSelectSecurity
 - EnterPassword
 - ClickSave

Now once you are done with these, your test would look like this:
LockScreenPage lockScreen = new LockScreenPage();
lockScreen. VerifyLockScreenPage
lockScreen. SwipeLockImage

PinEntryPage pinEntryPage = new PinEntryPage();
pinEntryPage.VerifyPinEntryPage;
pinEntryPage.EnterPin
pinEntryPage.PressEnter

HomeScreenPage homeScreenPage = new HomeScreenPage();
homeScreenPage.VerifyHomeScreenPage
homeScreenPage.ClickAllAppButton
 ….. Similarly for the rest


Below is some sample code for LockScreenPage. Similarly define for other too.
package com.google.android.pages.common;

import android.util.Log;

import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiSelector;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.google.android.common.TestConstants;
import com.google.android.pages.Page;

public class LockScreenPage extends Page {

    public static String TITLE = "LOCK SCREEN";

    // Lock button image on the lock screen.
    public static UiSelector TITLE_SELECTOR = new UiSelector()
            .className("android.widget.ImageView")
            .resourceId("com.android.systemui:id/lock_icon")
            .descriptionContains("Unlock");

    // All App button on the home screen after unlock.
    public UiSelector ALL_APP_BUTTON = new UiSelector().packageName(
            "com.google.android.googlequicksearchbox").descriptionContains(
            "Apps");

    // Constructor
    public LockScreenPage(UiAutomatorTestCase testCase) throws Exception {
        super(testCase);
        getDevice().wakeUp();
        Log.d(getTag(), String.format(">>>>\t %s page.", TITLE));
        getTestUtils().storeUiAutomatorViewerFiles(TITLE);
    }

    // Swiping the unlock image to go to the enter pin page
    public EnterPinPage swipeUpToUnlock(UiObject obj) throws Exception {
        Log.d(getTag(), "Swiping up the object.");
        obj.swipeUp(TestConstants.DEFAULT_SWIPE_STEPS);
        getTestUtils().storeUiAutomatorViewerFiles(TITLE + 
                " after swiping up the object");
        return new EnterPinPage(getTestCase());
    }

    // Verifying whether device is locked or not.
    public boolean isDeviceLocked(UiObject obj) throws Exception {
        Log.d(getTag(), "Is devices Locked?");
        getDevice().wakeUp();
        if (new UiObject(ALL_APP_BUTTON).exists()) {
            Log.d(getTag(), "Divice is: Unlocked.");
            return false;
        }
        Log.d(getTag(), "Divice is: Locked.");
        return true;
    }

    @Override
    public String getPageName() {
        return TITLE;
    }

    @Override
    public UiSelector getPageTitleSelector() {
        return TITLE_SELECTOR;
    }
}

Hope this helps
于 2014-12-05T10:13:35.820 回答