第四章:使用UIAutomator在不同手机上启动app

刚开始在一部手机上用uiautomator跑用例,所以只是简单的使用了new UiObject(new UiSelector().text("app名字")).click(); 来启动app的,后因为启动后的用例出了些问题,想用其他手机测试时才发现靠这种方法只能适用一款手机,于是就开始百度,百度大多说可以使用RunTime().getRunTime().exec("adb shell am -w 包名/.类名");去开启app,但小白也不清楚因为什么原因,试了好多次,这种方式在小白这里并不适用,几经辗转,从github上找到了可以在不同手机启动app的方法:

private UiDevice mDevice;

private static final int LAUNCH_TIMEOUT=7000;

private static final String packageName="org.txst.tst";

@Test

public void startapp() throws UiObjectNotFoundException{

final String launcherPackage=mDevice.getLauncherPackageName();

assertThat(launcherPackage,notNullValue());

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

Context context=InstrumentationRegistry.getContext();

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

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);

context.startActivity(intent);

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

}

所以将此记录下来,如还有其他好建议的小伙伴可以评论告诉小白。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容