UIAutomator1.0 Page Object實踐

以打開飛行模式的操作爲例

一. 父類page頁

package com.qwert.autotest.simcard.page;

import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObject;
import com.android.uiautomator.core.UiSelector;

public class Page {
    UiDevice mDevice;
    public Page(UiDevice device) {
        mDevice = device;
    }
    public UiObject find(UiSelector selector) {
        return new UiObject(selector);
    }
    // UI2.0
    /*public UiObject2 find(BySelector by) {
        return mDevice.findObject(by);
    }*/
    // UI2.0
    /*public UiObject find(UiSelector selector) {
        return mDevice.findObject(selector);
    }*/
}

二. 快速設置頁面

package com.qwert.autotest.simcard.page;
​
import android.widget.ImageView;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.core.UiObjectNotFoundException;
import com.android.uiautomator.core.UiSelector;
​
public class QuickSettingPage extends Page {
​
    private UiSelector airPlaneSelector = new UiSelector().description("Airplane mode");
    private UiSelector airPlaneIconSelector = new UiSelector().description("Airplane mode.").className(ImageView.class);
​
    public QuickSettingPage(UiDevice device) {
        super(device);
    }
​
    public static QuickSettingPage openQuickSettings(UiDevice device) {
        device.openQuickSettings();
        device.waitForIdle();
        return new QuickSettingPage(device);
    }
​
    private boolean isAirplaneModeEnabled() {
        try {
            return "On".equals(find(airPlaneSelector).getText());
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }
​
    public boolean setAirplaneMode(boolean enabled) {
        if (enabled == isAirplaneModeEnabled()) {
            return true;
        }
        try {
            find(airPlaneSelector).click();
        } catch (UiObjectNotFoundException e) {
            e.printStackTrace();
        }
        return enabled == isAirplaneModeEnabled();
    }
    public boolean isAirPlaneMode() {
        return find(airPlaneIconSelector).exists();
    }
}
​

三. 測試用例

package com.qwert.autotest.simcard.testcase;
​
import android.os.SystemClock;
import com.android.uiautomator.core.UiDevice;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import com.transsion.autotest.simcard.common.AutomatorHelper;
import com.transsion.autotest.simcard.page.*;
​
public class SimCardTest extends UiAutomatorTestCase {
​
    private UiDevice mDevice;
    private AutomatorHelper mHelper;
​
    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mDevice = UiDevice.getInstance();
        mHelper = new AutomatorHelper(mDevice);
​
        System.out.println("RUN START!!");
        mHelper.openLauncher();// 喚醒屏幕,解鎖,打開桌面
    }
​
    /**
     * 飛行模式切換到普通模式
     */
    public void testSwitchToNormalMode() {
        // 打開快速設置,打開飛行模式
        QuickSettingPage settingPage = QuickSettingPage.openQuickSettings(mDevice);
        settingPage.setAirplaneMode(true);
​
        // 由飛行模式切換到普通模式
        settingPage.setAirplaneMode(false);
​
        // 驗證是否切換成功
        if (!settingPage.isAirPlaneMode()) {
            System.out.println("RESULT:Pass");
            System.out.println("switch to normal mode success");
        } else {
            System.out.println("RESULT:Fail switch to normal mode fail");
        }
    }
​
    /**
     * 普通模式切換到飛行模式
     */
    public void testSwitchToAirplaneMode() {
        // 打開快速設置,打開飛行模式,驗證是否打開成功
        QuickSettingPage settingPage = QuickSettingPage.openQuickSettings(mDevice);
        settingPage.setAirplaneMode(true);
        if (settingPage.isAirPlaneMode()) {
            System.out.println("RESULT:Pass");
            System.out.println("switch to airplane mode success");
        } else {
            System.out.println("RESULT:Fail switch to airplane mode fail");
        }
​
        // 測試完成,取消飛行模式。恢復手機狀態,避免影響後續用例執行。
        settingPage.setAirplaneMode(false);
    }
​
    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        System.out.println("RUN FINISH!!");
        getUiDevice().pressBack();
        getUiDevice().pressHome();
    }
​
}
​

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章