话不多说,代码先贴上。反正我备注的都很详细啦。。。关于人生中的第一段代码。(BaseTest是android开发写的)
package com.example.xiaofeng.uiautotest; import android.content.Context; import android.content.Intent; import android.os.RemoteException; import android.support.test.InstrumentationRegistry; 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.Until; import android.widget.Button; import org.junit.Assert; import org.junit.Before; import static android.support.test.InstrumentationRegistry.getContext; /** * Created by nanfeng on 2017/4/24. */ public class BaseTest { protected UiDevice mUIDevice = null; protected Context mContext = null;//Context是android中上下文 String APP = "com.maihaoche.bentley.debug"; // String BASE_ID = "com.maihaoche.bentley.debug:id/" ; String BASE_ID2 = "com.maihaoche.bentley.debug" ; int displayTimeOut = 5 * 2000 ; //默认的界面刷新超时时间 String userPhone = "18868816760" ; String password = "111111" ; @Before public void setUp() throws RemoteException { mUIDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); //获得device对象 mContext = getContext(); // APP = getLauncherPackageName() ; if(!mUIDevice.isScreenOn()){ //判断屏幕是否唤醒状态,如果非,唤醒屏幕 mUIDevice.wakeUp(); } mUIDevice.pressHome(); //因为可能会处于其他的应用中,所有先按home键 } protected void waitUpdate(){ //等待界面渲染 mUIDevice.waitForWindowUpdate(APP,displayTimeOut) ; } public void openApp(String packageName) { //打开APP Context context = InstrumentationRegistry.getInstrumentation().getContext(); Intent intent = context.getPackageManager().getLaunchIntentForPackage(packageName); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); context.startActivity(intent); } /** * 第一次进APP,走引导页时的逻辑 */ protected void firstEntrance() { new Thread(){ @Override public void run() { super.run(); UiObject2 powerView = mUIDevice.wait(Until.findObject(By.clazz(Button.class).text("允许"