首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用UiAutomation在安卓系统中点击按钮?

如何使用UiAutomation在安卓系统中点击按钮?
EN

Stack Overflow用户
提问于 2014-11-26 12:07:20
回答 1查看 1K关注 0票数 1

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

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

我将很高兴获得一些技巧来开始。

EN

回答 1

Stack Overflow用户

发布于 2014-12-05 18:13:36

代码语言:javascript
复制
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
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27141147

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档