Java+Uiautomator 实现手机的五种解锁方式(无/滑动/图案/PIN码/密码|)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ricky_yangrui/article/details/89004359

首先 先封装下每个解锁方式

package com.秘密xxx.autotest.page.systemui;

import android.graphics.Point;
import android.graphics.Rect;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.Until;

import com.transsion.autotest.common.PackageConstants;
import com.transsion.autotest.page.BasePage;

public class SystemUiPage extends BasePage {

    private final long LAUNCH_CALL_PAGE = 5000L;
    private BySelector lockIconSelector = By.res(PackageConstants.SystemUI.PACKAGE, "lock_icon");
    private BySelector unLockPinEnterSelector = By.res("com.android.systemui:id/key_enter")
            .desc("Enter");
    private BySelector unlockPasswordEnterSelector = By.res("com.android.systemui:id/passwordEntry")
            .desc("Device password");
    public BySelector homeButtonSelector = By.res("com.android.systemui:id/home")
            .desc("Home");
    private BySelector backButtonSelector = By.res("com.android.systemui:id/back")
            .desc("Back");
    private BySelector recentButtonSelector = By.res("com.android.systemui:id/recent_apps")
            .desc("Overview");
    private BySelector launcherObjectSelector = By.pkg("com.android.launcher3")
            .clazz("android.view.ViewGroup");


    public SystemUiPage(UiDevice device) {
        super(device);
    }


    /**
     * 屏幕解锁页面,上划解锁
     */
    public void screenUnLockSwipeUp() {
        Point p = find(lockIconSelector).getVisibleCenter();
        mDevice.swipe(p.x, p.y, p.x, 0, 5);
        mDevice.wait(Until.gone(lockIconSelector), LAUNCH_CALL_PAGE);

    }


    /**
     * 获取传入键的中心坐标
     */
    public Point getCenterCoordinateOfTheKey(BySelector button) {
        UiObject2 uiObject = find(button);
        Rect stRect = uiObject.getVisibleBounds();
        int nPressX = stRect.centerX();
        int nPressY = stRect.centerY();
        return new Point(nPressX, nPressY);
    }

    /**
     * 从Key键上划
     */
    public void swipeUpFromKey(Point keyButton) {
        Point[] pointIntoArray = new Point[]{
                new Point(keyButton.x, keyButton.y + 0),
                new Point(keyButton.x, keyButton.y - 150),
        };
        mDevice.swipe(pointIntoArray, 40);
    }


    /**
     * 屏幕解锁页面,倒L图案解锁
     */
    public void screenUnLockPattern() {
        screenUnLockSwipeUp();
        Point[] pointUnlockArray = new Point[]{
                new Point(175, 784), new Point(357, 784), new Point(531, 784), new Point(531, 969), new Point(531, 1144)
        };
        int step = 50;
        mDevice.swipe(pointUnlockArray, step);
    }

    /**
     * 屏幕解锁页面,Pin 1234解锁
     */
    public void screenUnLockPin() {
        screenUnLockSwipeUp();
        String[] passWord = {"1", "2", "3", "4"};
        for (String i : passWord) {
            find(By.res("com.android.systemui:id/digit_text")
                    .clazz("android.widget.TextView").text(i)).click();
        }
        find(unLockPinEnterSelector).clickAndWait(Until.newWindow(), 3000);
    }

    /**
     * 屏幕解锁页面,Password 1111解锁
     */
    public void screenUnLockPassword() {
        screenUnLockSwipeUp();
        find(unlockPasswordEnterSelector).setText("1111");
        mDevice.pressEnter();
    }

    public SystemUiPage longClickBackButton() throws UiObjectNotFoundException {
        Point backCenterPoint = getCenterCoordinateOfTheKey(backButtonSelector);
        Point[] pointIntoArray = new Point[]{backCenterPoint, backCenterPoint};
        int nSecondsSteps = 200;
        mDevice.swipe(pointIntoArray, nSecondsSteps);
        return new SystemUiPage(mDevice);
    }

    public boolean isKeyExists() {
        boolean isHomeButtonExists = mDevice.hasObject(homeButtonSelector);
        boolean isBackButtonExists = mDevice.hasObject(backButtonSelector);
        boolean isRecentButtonExists = mDevice.hasObject(recentButtonSelector);
        return isHomeButtonExists && isBackButtonExists && isRecentButtonExists;
    }

    public void mostLeftSwipeToRight() {
        mDevice.swipe(0, mDevice.getDisplayHeight() / 2,
                mDevice.getDisplayWidth(), mDevice.getDisplayHeight() / 2, 100);

    }

    public boolean isOnTheDesktop() {
        return mDevice.hasObject(launcherObjectSelector);
    }


}

解锁的Page

package com.xxxxxx.autotest.page.settings;

import android.graphics.Point;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.BySelector;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObjectNotFoundException;
import android.support.test.uiautomator.Until;

import com.xxxxxx.autotest.page.BasePage;

public class SecurityLocationPage extends BasePage {

    SettingsMainPage mSettingsMainPage = new SettingsMainPage(mDevice);
    private final long LAUNCH_CALL_PAGE = 5000L;
    private final String mScreenLock = "Screen lock";
    private BySelector screenLockButtonSelector = By.text("Screen lock");
    private BySelector locationButtonSelector = By.text("Location");
    private BySelector noneScreenLockSelector = By.text("None").res("android:id/title");
    private BySelector swipeScreenLockSelector = By.text("Swipe").res("android:id/title");
    private BySelector patternScreenLockSelector = By.text("Pattern").res("android:id/title");
    private BySelector pinScreenLockSelector = By.text("PIN").res("android:id/title");
    private BySelector passwordScreenLockSelector = By.text("Password").res("android:id/title");
    private BySelector ChooseScreenLockSelector = By.text("Choose screen lock");
    private BySelector SecureStartUpSelector = By.text("Secure start-up").res("com.android.settings:id/suw_layout_title");
    private BySelector SecureStartUpNoButtonSelector = By.text("NO")
            .res("com.android.settings:id/encrypt_dont_require_password");
    private BySelector locationSwitchButtonSelector = By.res("com.android.settings:id/switch_widget")
            .clazz("android.widget.Switch");
    private BySelector securityLocationHeadlineSelector = By.text("Security & location");
    private BySelector setScreenLockNextSelector = By.text("NEXT");
    private BySelector setScreenLockConfirmSelector = By.text("CONFIRM");
    private BySelector setScreenLockDoneSelector = By.text("DONE");
    private BySelector setScreenQuestionSkipSelector = By.text("Skip");
    private BySelector confirmYourPatternSelector = By.text("Confirm your pattern");
    private BySelector setScreenLockPasswordSelector = By.res("com.android.settings:id/password_entry")
            .clazz("android.widget.EditText");
    private BySelector reEnterYourPinSelector = By.text("Re-enter your PIN");
    private BySelector reEnterYourPasswordSelector = By.text("Re-enter your password");
    private BySelector removeDeviceProtectionSelector = By.text("Remove device protection?").res("android:id/alertTitle");
    private BySelector yesRemoveButtonSelector = By.text("YES, REMOVE").res("android:id/button1");
    private BySelector getScreenLockButtonSelector = By.text("Screen lock");
    private BySelector quitSecurityLocationButtonSelector = By.text("Use Accessibility Shortcut?");
    private BySelector settingsSearchTextSelector = By.res("com.android.settings:id/search_action_bar_title").text("Search settings");


    public SecurityLocationPage(UiDevice device) {
        super(device);
    }

    /**
     * 判断是否在Security&location页面
     */
    public boolean isSecurityLocationPage() {
        return mDevice.hasObject(securityLocationHeadlineSelector);
    }

    /**
     * 从 Security&location页面进入choose screen lock页面,选择None锁屏方式
     */
    public void selectScreenLockNone() throws UiObjectNotFoundException {
        mSettingsMainPage.settingsListItem(mScreenLock);
        mDevice.wait(Until.hasObject(ChooseScreenLockSelector), LAUNCH_CALL_PAGE);
        find(noneScreenLockSelector).click();
        mDevice.wait(Until.hasObject(screenLockButtonSelector), LAUNCH_CALL_PAGE);
    }

    /**
     * 从 Security&location页面进入choose screen lock页面,选择Swipe锁屏方式
     */
    public void selectScreenLockSwipe() throws UiObjectNotFoundException {
        mSettingsMainPage.settingsListItem(mScreenLock);
        //  Password密码为1111
        if (mDevice.hasObject(reEnterYourPasswordSelector)) {
            find(setScreenLockPasswordSelector).setText("1111");
            mDevice.pressEnter();
        }
        mDevice.wait(Until.hasObject(ChooseScreenLockSelector), LAUNCH_CALL_PAGE);
        find(swipeScreenLockSelector).click();
        if (mDevice.hasObject(removeDeviceProtectionSelector)) {
            find(yesRemoveButtonSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        }
        mDevice.wait(Until.hasObject(screenLockButtonSelector), LAUNCH_CALL_PAGE);
    }

    /**
     * 从 Security&location页面进入choose screen lock页面,选择Pattern锁屏方式,图案为倒L
     */
    public void selectScreenLockPattern() throws UiObjectNotFoundException {
        mSettingsMainPage.settingsListItem(mScreenLock);
        mDevice.wait(Until.hasObject(ChooseScreenLockSelector), LAUNCH_CALL_PAGE);
        find(patternScreenLockSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        if (mDevice.hasObject(SecureStartUpSelector)) {
            find(SecureStartUpNoButtonSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        }
        Point[] pointLockArray = new Point[]{
                new Point(175, 645), new Point(357, 645), new Point(531, 645), new Point(531, 825), new Point(531, 1006)
        };
        int step = 50;
        mDevice.swipe(pointLockArray, step);
        find(setScreenLockNextSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        mDevice.swipe(pointLockArray, step);
        find(setScreenLockConfirmSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        find(setScreenLockDoneSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        find(setScreenQuestionSkipSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        mDevice.wait(Until.hasObject(screenLockButtonSelector), LAUNCH_CALL_PAGE);
    }

    /**
     * 从 Security&location页面进入choose screen lock页面,选择PIN锁屏方式
     */
    public void selectScreenLockPin() throws UiObjectNotFoundException {
        mSettingsMainPage.settingsListItem(mScreenLock);
        //  Pattern为倒L图案
        if (mDevice.hasObject(confirmYourPatternSelector)) {
            Point[] pointIntoArray = new Point[]{
                    new Point(175, 633), new Point(357, 633), new Point(531, 633), new Point(531, 819), new Point(531, 1000)
            };
            mDevice.swipe(pointIntoArray, 50);
        }
        find(pinScreenLockSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        //排除 Secure start-up
        if (mDevice.hasObject(SecureStartUpSelector)) {
            find(SecureStartUpNoButtonSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        }
        find(setScreenLockPasswordSelector).setText("1234");
        find(setScreenLockNextSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        find(setScreenLockPasswordSelector).setText("1234");
        find(setScreenLockConfirmSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        mDevice.wait(Until.hasObject(screenLockButtonSelector), LAUNCH_CALL_PAGE);
    }

    /**
     * 从 Security&location页面进入choose screen lock页面,选择Password锁屏方式
     */
    public void selectScreenLockPassword() throws UiObjectNotFoundException {
        mSettingsMainPage.settingsListItem(mScreenLock);
        //  pin密码为1234
        if (mDevice.hasObject(reEnterYourPinSelector)) {
            find(setScreenLockPasswordSelector).setText("1234");
            mDevice.pressEnter();
        }
        find(passwordScreenLockSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        //排除 Secure start-up
        if (mDevice.hasObject(SecureStartUpSelector)) {
            find(SecureStartUpNoButtonSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        }
        find(setScreenLockPasswordSelector).setText("1111");
        find(setScreenLockNextSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        find(setScreenLockPasswordSelector).setText("1111");
        find(setScreenLockConfirmSelector).clickAndWait(Until.newWindow(), LAUNCH_CALL_PAGE);
        mDevice.wait(Until.hasObject(screenLockButtonSelector), LAUNCH_CALL_PAGE);
    }

    public boolean enterChooseScreenLockPage() {
        find(getScreenLockButtonSelector).click();
        return mDevice.wait(Until.gone(getScreenLockButtonSelector), LAUNCH_CALL_PAGE);
    }

    public boolean quitSecurityLocation() {
        if (mDevice.hasObject(quitSecurityLocationButtonSelector)) {
            find(quitSecurityLocationButtonSelector).click();
            return mDevice.wait(Until.hasObject(settingsSearchTextSelector), LAUNCH_CALL_PAGE);
        } else {
            return false;
        }
    }

    public boolean backSecurityLocation() {
        mDevice.pressBack();
        return mDevice.wait(Until.hasObject(settingsSearchTextSelector), LAUNCH_CALL_PAGE);
    }
}

最后就是各种解锁的实现啦

@Test
public void testSettings_0175_checkUnlockWays() throws UiObjectNotFoundException, RemoteException {
    // 常进入【选择屏幕锁定方式】界面,解锁方式包括:无/滑动/图案/PIN码/密码
    mSettingsPage.enterSecurityLocationList();
    SecurityLocationPage mSecurityLocationPage = new SecurityLocationPage(mDevice);
    mSecurityLocationPage.enterChooseScreenLockPage();
    ChooseScreenLockPage mChooseScreenLockPage = new ChooseScreenLockPage(mDevice);
    boolean isExistsFiveScreenLock = mChooseScreenLockPage.isExistsScreenLockWays();
    Assert.assertTrue("some lock ways lose", isExistsFiveScreenLock);
}

@Test
public void testSettings_0176_setNoneScreenLock() throws UiObjectNotFoundException, RemoteException {
    //None解锁功能
    mSettingsPage.enterSecurityLocationList();
    SecurityLocationPage mSecurityLocationPage = new SecurityLocationPage(mDevice);
    mSecurityLocationPage.selectScreenLockNone();
    mDevice.sleep();
    mHelper.wakeUp();
    Assert.assertTrue("Lock screen set to None may not be implemented",
            mSecurityLocationPage.isSecurityLocationPage());
}

@Test
public void testSettings_0177_setSwipeScreenLock() throws UiObjectNotFoundException, RemoteException {
    // Swipe解锁功能
    mSettingsPage.enterSecurityLocationList();
    SecurityLocationPage mSecurityLocationPage = new SecurityLocationPage(mDevice);
    mSecurityLocationPage.selectScreenLockSwipe();
    mDevice.sleep();
    mHelper.unlock();
    Assert.assertTrue("Lock screen set to Swipe may not be implemented",
            mSecurityLocationPage.isSecurityLocationPage());
}

@Test
public void testSettings_0178_setPatternScreenLock() throws UiObjectNotFoundException, RemoteException {
    // pattern解锁功能
    mSettingsPage.enterSecurityLocationList();
    SecurityLocationPage mSecurityLocationPage = new SecurityLocationPage(mDevice);
    mSecurityLocationPage.selectScreenLockPattern();
    mDevice.sleep();
    mDevice.wakeUp();
    SystemUiPage mSystemUiPage = new SystemUiPage(mDevice);
    mSystemUiPage.screenUnLockPattern();
    Assert.assertTrue("Lock screen set to Pattern may not be implemented",
            mSecurityLocationPage.isSecurityLocationPage());
}

@Test
public void testSettings_0179_setPinScreenLock() throws UiObjectNotFoundException, RemoteException {
    // pin解锁功能
    mSettingsPage.enterSecurityLocationList();
    SecurityLocationPage mSecurityLocationPage = new SecurityLocationPage(mDevice);
    mSecurityLocationPage.selectScreenLockPin();
    mDevice.sleep();
    mDevice.wakeUp();
    SystemUiPage mSystemUiPage = new SystemUiPage(mDevice);
    mSystemUiPage.screenUnLockPin();
    Assert.assertTrue("Lock screen set to Pin may not be implemented",
            mSecurityLocationPage.isSecurityLocationPage());
}

@Test
public void testSettings_0180_setPasswordScreenLock() throws UiObjectNotFoundException, RemoteException {
    // password解锁功能
    mSettingsPage.enterSecurityLocationList();
    SecurityLocationPage mSecurityLocationPage = new SecurityLocationPage(mDevice);
    mSecurityLocationPage.selectScreenLockPassword();
    mDevice.sleep();
    mDevice.wakeUp();
    SystemUiPage mSystemUiPage = new SystemUiPage(mDevice);
    mSystemUiPage.screenUnLockPassword();
    Assert.assertTrue("Lock screen set to Password may not be implemented",
            mSecurityLocationPage.isSecurityLocationPage());
}

因为我们采用的事POM的写法,后续会拆分下,写的详细一点。

猜你喜欢

转载自blog.csdn.net/ricky_yangrui/article/details/89004359