如何使用UiAutomation点击Android中的按钮?

时间:2014-11-26 04:07:20

标签: android ui-automation android-instrumentation

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

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

我很乐意为您提供一些入门提示。

1 个答案:

答案 0 :(得分:-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