UIAutomator在AndroidStudio中的使用

来源:互联网 发布:崔顺实长得像网络小胖 编辑:程序博客网 时间:2024/04/29 10:57

这几天在看UIAutomator ,但网上关于UIAutomator的基本上都是基于Eclipse adt的,很少有androidstudio的,所以在这里总结下,省的以后忘记了。


androidStudio是支持UIAutomator的  这点毋庸置疑,而且android已经为每个module都设置好了,即androidTest。


首先 我们要加入依赖包,在build.gradle中的dependencies中加入

// Testing-only dependenciesandroidTestCompile 'com.android.support.test:runner:0.3'// Set this dependency to use JUnit 4 rulesandroidTestCompile 'com.android.support.test:rules:0.3'// Set this dependency to build and run Espresso testsandroidTestCompile 'com.android.support.test.espresso:espresso-core:2.2'// Set this dependency to build and run UI Automator testsandroidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.1'
当然  这一步如果没想相应的Android Support Library是会报错的 ,需要到Android sdk manager中去下载,Extras包中的Android Support Library
然后更新gradle,如果你的minSdkVersion小于18会报错,可能是我下的support的版本缘故,如果有哪位知道,麻烦告诉下。
我这里暂时设置minSDKVersion为18
然后就可以开始写我们的测试用例了
在androidTest下创建我们的测试用例即可
@RunWith(AndroidJUnit4.class)@SdkSuppress(minSdkVersion = 18)public class UIautomatorTest{

androidStudio中的测试用例采用了注解的方式
@Before
这个注解下的方法一般用于启动你的app
@Test 
这个注解下的方法,就可以写自己的测试用例了,可以有多个
写好后,设置启动配置
在启动设置中的Android Tests中选择你要启动的测试用例
Test有  1.All in Module  2.All in Package 3.Class 4.Method等4个选项 我这里选择的Class
runner 选择AndroidJUnitRunner
apply ok后
启动测试用例即可

0 0
原创粉丝点击