AndroidStudio通過UIAutomator進行測試 用例編寫(二)

樣本格式:

/**

* 樣例

*/

@RunWith(AndroidJUnit4.class)

@SdkSuppress(minSdkVersion = 18)

public class BasicSampleTest {

? ? private final static String TAG = "BasicSampleTest";

? ? private UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());

? ? private static final String FLAG = "";//選取主activity的 flag //TODO

? ? private static final String TEST_PACKAGE_NAME = "";//app package Name //TODO

? ? private static final int LAUNCH_TIMEOUT = 2000;

? ? private int[] controlInfo = new int[10]; //{x, y, centerX, centerY, width, height};//控件坐標信息

? ? private static final int SCRE_WIDTH = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).getInstance().getDisplayWidth();//獲取屏幕寬度

? ? private static final int SCRE_HEIGHT = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()).getInstance().getDisplayHeight();//獲取屏幕高度

? ? /*************************************定義ID begin**********************************************************/

? ? //private static final String SAMP = "samp";

? ? /*************************************定義ID end**********************************************************/

? ? /*************************************Test Before begin**********************************************************/

? ? /**

? ? * 從主屏幕啟動應(yīng)用

? ? */

? ? @Before

? ? public void startMainActivityFromHomeScreen() {

? ? ? ? // wakeup screen

? ? ? ? try {

? ? ? ? ? ? if (!mDevice.isScreenOn()) {

? ? ? ? ? ? ? ? mDevice.wakeUp();

? ? ? ? ? ? }

? ? ? ? } catch (RemoteException e) {

? ? ? ? ? ? Log.e(TAG, "Device wakeUp exception!");

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? // Wait for launcher

? ? ? ? final String launcherPackage = getLauncherPackageName();

? ? ? ? assertThat(launcherPackage, notNullValue());

? ? ? ? mDevice.wait(Until.hasObject(By.pkg(launcherPackage).depth(0)), LAUNCH_TIMEOUT);

? ? ? ? //如果測試應(yīng)用的主 activity 未顯示 則開啟

? ? ? ? if (mDevice.wait(Until.hasObject(By.pkg(TEST_PACKAGE_NAME).depth(0)), LAUNCH_TIMEOUT)) {

? ? ? ? ? ? //如果應(yīng)用已經(jīng)啟動,判斷是否是在 main activity 不在重新啟動

? ? ? ? ? ? Log.e(TAG, "應(yīng)用已經(jīng)啟動");

? ? ? ? ? ? UiObject mainActivityFlag = mDevice.findObject(new UiSelector().resourceId(FLAG));//獲取主activity的FLAG

? ? ? ? ? ? if (!mainActivityFlag.exists()) {//根據(jù) main activity 的元素是否存在判斷是否重新啟動

? ? ? ? ? ? ? ? Log.e(TAG, "重新啟動app");

? ? ? ? ? ? ? ? startApp(TEST_PACKAGE_NAME, LAUNCH_TIMEOUT);

? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? Log.e(TAG, "應(yīng)用未啟動柳弄,啟動應(yīng)用");

? ? ? ? ? ? startApp(TEST_PACKAGE_NAME, LAUNCH_TIMEOUT);

? ? ? ? }

? ? }

? ? /**

? ? * Uses package manager to find the package name of the device launcher. Usually this package

? ? * is "com.android.launcher" but can be different at times. This is a generic solution which

? ? * works on all platforms.`

? ? */

? ? public String getLauncherPackageName() {

? ? ? ? // Create launcher Intent

? ? ? ? final Intent intent = new Intent(Intent.ACTION_MAIN);

? ? ? ? intent.addCategory(Intent.CATEGORY_HOME);

? ? ? ? // Use PackageManager to get the launcher package name

? ? ? ? PackageManager pm = InstrumentationRegistry.getContext().getPackageManager();

? ? ? ? ResolveInfo resolveInfo = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);

? ? ? ? return resolveInfo.activityInfo.packageName;

? ? }

? ? /**

? ? * 進入抽屜點擊啟動APP

? ? *

? ? * @param className

? ? * @param dex

? ? * @param appName

? ? */

? ? public void clickApp(String className, int dex, String appName) {

? ? ? ? try {

? ? ? ? ? ? UiObject appItem = mDevice.findObject(new UiSelector().className(className).index(dex).childSelector(new UiSelector().text(appName)));

? ? ? ? ? ? appItem.click();

? ? ? ? } catch (UiObjectNotFoundException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? }

? ? /**

? ? * 打開app

? ? *

? ? * @param packageName

? ? * @param timeOut

? ? */

? ? public void startApp(String packageName, int timeOut) {

? ? ? ? Context context = InstrumentationRegistry.getContext();

? ? ? ? final Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName);

? ? ? ? intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);? ? // Clear out any previous instances

? ? ? ? context.startActivity(intent);

? ? ? ? // Wait for the app to appear

? ? ? ? mDevice.wait(Until.hasObject(By.pkg(packageName).depth(0)), timeOut);

? ? }

? ? /**

? ? * 獲取控件坐標信息

? ? * @param resourceID

? ? * @return

? ? */

? ? public int[] getControlInfo(String resourceID) {//TODO

? ? ? ? UiObject2 control = mDevice.findObject(By.res(TEST_PACKAGE_NAME, resourceID));

? ? ? ? Log.d(TAG, "control====" + control);

? ? ? ? Rect controlRect = control.getVisibleBounds();

? ? ? ? Log.d(TAG, "controlRect====" +controlRect);

? ? ? ? controlInfo[0] = controlRect.right;

? ? ? ? controlInfo[1] = controlRect.bottom;

? ? ? ? controlInfo[2] = controlRect.centerX();

? ? ? ? controlInfo[3] = controlRect.centerY();

? ? ? ? controlInfo[4] = controlRect.width();

? ? ? ? controlInfo[5] = controlRect.height();

? ? ? ? return controlInfo;

? ? }

? ? /**

? ? * 點擊控件

? ? * @param stringId

? ? * @param isLongClick

? ? */

? ? private void clickControl(String stringId, boolean isLongClick) {

? ? ? ? Log.d(TAG, "clickControl-stringId=====" + stringId);

? ? ? ? if (stringId != null) {

? ? ? ? ? ? if (isLongClick) {

? ? ? ? ? ? ? ? mDevice.findObject(By.res(TEST_PACKAGE_NAME, stringId)).longClick();

? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? mDevice.findObject(By.res(TEST_PACKAGE_NAME, stringId)).click();

? ? ? ? ? ? }

? ? ? ? }

? ? }

? ? /**

? ? * 獲取控件 TEXT 內(nèi)容

? ? * @param stringId

? ? * @return

? ? */

? ? private String getControlText(String stringId) {

? ? ? ? Log.d(TAG, "getControlText-stringId=====" + stringId);

? ? ? ? String text = mDevice.findObject(By.res(TEST_PACKAGE_NAME, stringId)).getText().trim();

? ? ? ? if (text != null) {

? ? ? ? ? ? return text;

? ? ? ? } else {

? ? ? ? ? ? return "TEST_ERROR";

? ? ? ? }

? ? }

? ? /*****************************************Test Before end**************************************************/

? ? /*****************************************定義其他方法 begin**************************************************/

? ? /**

? ? * 獲取屏幕寬度

? ? * @param activity

? ? * @return

? ? */

? ? public final static int getWindowsWidth(Activity activity) {

? ? ? ? DisplayMetrics displayMetrics = new DisplayMetrics();

? ? ? ? activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

? ? ? ? return displayMetrics.widthPixels;

? ? }

? ? /**

? ? * 獲取屏幕高度

? ? * @param activity

? ? * @return

? ? */

? ? public final static int getWindowsHight(Activity activity) {

? ? ? ? DisplayMetrics displayMetrics = new DisplayMetrics();

? ? ? ? activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

? ? ? ? return displayMetrics.heightPixels;

? ? }

? ? /**

? ? * 獲取控件高度

? ? * @param view

? ? * @return

? ? */

? ? public final static int getViewhight(View view) {

? ? ? ? int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

? ? ? ? int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

? ? ? ? view.measure(w, h);

? ? ? ? int height = view.getMeasuredHeight();

? ? ? ? return height;

? ? }

? ? /**

? ? * 獲取控件寬度

? ? * @param view

? ? * @return

? ? */

? ? public final static int getViewWidth(View view) {

? ? ? ? int w = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

? ? ? ? int h = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

? ? ? ? view.measure(w, h);

? ? ? ? int width = view.getMeasuredWidth();

? ? ? ? return width;

? ? }

? ? /*****************************************定義其他方法 begin**************************************************/

? ? /***************************************測試 CASE? begin**************************************************/

? ? /**

? ? *

? ? */

? ? @Test

? ? public void test_basic_sample_001() throws InterruptedException {

? ? ? ? sleep(500);

? ? ? ? assertEquals("message", "expected_result", "actual_result");

? ? }

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末撒蟀,一起剝皮案震驚了整個濱河市也殖,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌吩屹,老刑警劉巖邪财,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件袭灯,死亡現(xiàn)場離奇詭異,居然都是意外死亡姥份,警方通過查閱死者的電腦和手機郭脂,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來澈歉,“玉大人展鸡,你說我怎么就攤上這事∶葡椋” “怎么了娱颊?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵傲诵,是天一觀的道長。 經(jīng)常有香客問我箱硕,道長拴竹,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任剧罩,我火速辦了婚禮栓拜,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘惠昔。我一直安慰自己幕与,他們只是感情好,可當我...
    茶點故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布镇防。 她就那樣靜靜地躺著啦鸣,像睡著了一般。 火紅的嫁衣襯著肌膚如雪来氧。 梳的紋絲不亂的頭發(fā)上诫给,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天,我揣著相機與錄音啦扬,去河邊找鬼中狂。 笑死,一個胖子當著我的面吹牛扑毡,可吹牛的內(nèi)容都是我干的胃榕。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼瞄摊,長吁一口氣:“原來是場噩夢啊……” “哼勋又!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起泉褐,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赐写,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后膜赃,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡揉忘,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年跳座,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片泣矛。...
    茶點故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡疲眷,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出您朽,到底是詐尸還是另有隱情狂丝,我是刑警寧澤换淆,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站几颜,受9級特大地震影響倍试,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜蛋哭,卻給世界環(huán)境...
    茶點故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一县习、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谆趾,春花似錦躁愿、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至跷叉,卻和暖如春样勃,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背性芬。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工峡眶, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人植锉。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓辫樱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親俊庇。 傳聞我的和親對象是個殘疾皇子狮暑,可洞房花燭夜當晚...
    茶點故事閱讀 44,843評論 2 354