Android 傻瓜式自动测试 利用AS2.2 Record Espresso + Instrumentation

来源:互联网 发布:jersey json 传递对象 编辑:程序博客网 时间:2024/06/05 06:11

让开发的app自动去跑 测试 流程 ,是不是很酷
那么通过操作屏幕自动生成 测试流程代码 是不是更酷

接下来 我们借助Android Studio 2.2 来实现 简单的自动测试吧
先上动态图
这里写图片描述

环境准备

可以直接去github去copy代码 欢迎star fork
https://github.com/shf981862482/AutoTextApplication

配置我们的app下的 build.gradle

android {    defaultConfig {        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'    }}

添加packagingOptions 代码解决冲突。不然会报以下错误

Duplicate files copied in APK META-INF/maven/com.google.guava/guava/pom.properties

android {    packagingOptions {        pickFirst('META-INF/maven/com.google.guava/guava/pom.xml')        pickFirst('META-INF/maven/com.google.guava/guava/pom.properties')    }}
dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    testCompile 'junit:junit:4.12'    androidTestCompile 'com.android.support.test:runner:0.5'    androidTestCompile 'com.android.support.test:rules:0.5'    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'}

添加configurations.all代码解决冲突。不然会报以下错误

Conflict with dependency ‘com.android.support:support-annotations’. Resolved versions for app (23.1.0) and test app (23.0.1) differ

configurations.all {    resolutionStrategy.force 'com.android.support:support-annotations:23.1.0'}

或者尝试

    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {        exclude group: 'com.android.support', module: 'support-annotations'    })

环境搭建好了

必备的工具

找到自己的sdk目录

C:\AndroidSDK\tools

打开uiautomatorviewer.bat

uiautomatorviewer
工具提供了一个便利的方式来查看UI布局结构,并且可以查看各个控件的相关属性。利用这些信息可以用来创建UI测试代码。

使用方法

usb连接手机,点击图标
这里写图片描述
这里写图片描述
选中某一个控件,比如button
我们看note detail 出 可以找到resorce_id cotnent-dec text hint 等信息
这对我们自动测试找到控件很重要、
当然后面的Record Espresso 会自动帮我们找到能定位到某个空间的信息

当然,为了在自动化测试的时候方便找到控件,那么注意以下几点

1、使用android:contentDescription属性给 ImageButton, ImageView,
CheckBox和其他控件设置标签。 2、使用android:hint 属性来标记EditText
控件,而不是使用里面的文本(文本内容用户是可以修改的)。
3、对于用来提供操作视觉反馈的UI(文本或者图标),都添加一个android:hint 属性来识别。
4、确保所有用户可操作的界面元素都可以通过方向控制键选中(例如轨迹球)。 5、通过uiautomatorviewer
5、工具来确保所有的UI元素都可以被测试工具访问到。还可以通过“辅助功能”(在设置界面)中的“TalkBack”等服务来测试UI的可访问性。

AS2.2 Record Espresso

通过 Record Espresso自动生成测试流程文件

这里写图片描述

这里写图片描述

以上两步操作 会到如下界面

这里写图片描述

这个时候不要点击任何按钮,让我们拿出手机,进行测试记录
先说一下 我的程序流程

LauchActivity 进行耗时操作模拟初始化
LoginActivity 输入用户名和密码 点击button进行耗时操作后进入MainActivity

注意,这相当于debug操作,所以运行会比较慢
这里写图片描述

可以看到上图,我输入了用户名和密码,左边就自动生成了测试代码
点击button 到了MainActivity

这里写图片描述

我们点击Complete Recording 完成录制,真正生成测试代码
这里写图片描述

这里写图片描述

打开LauchActivityTest可能会有乱码,我们按情况更成汉字就行了
这里写图片描述

规范并保证我们的测试代码正确

生成之后,我们可以先编译试试,当然我们也可以设断点 debug

import android.app.Instrumentation;import android.support.test.espresso.ViewInteraction;import android.support.test.rule.ActivityTestRule;import android.support.test.runner.AndroidJUnit4;import android.test.suitebuilder.annotation.LargeTest;import org.junit.Rule;import org.junit.Test;import org.junit.runner.RunWith;import static android.support.test.InstrumentationRegistry.getInstrumentation;import static android.support.test.espresso.Espresso.onView;import static android.support.test.espresso.action.ViewActions.click;import static android.support.test.espresso.action.ViewActions.replaceText;import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;import static android.support.test.espresso.matcher.ViewMatchers.withId;import static android.support.test.espresso.matcher.ViewMatchers.withParent;import static android.support.test.espresso.matcher.ViewMatchers.withText;import static org.hamcrest.Matchers.allOf;@LargeTest@RunWith(AndroidJUnit4.class)public class LauchActivityTest {    @Rule    public ActivityTestRule<LauchActivity> mActivityTestRule = new ActivityTestRule<>(LauchActivity.class);    @Test    public void lauchActivityTest() {        ViewInteraction appCompatEditText = onView(                allOf(withId(R.id.etxt_name),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatEditText.perform(click());        ViewInteraction appCompatEditText2 = onView(                allOf(withId(R.id.etxt_name),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatEditText2.perform(replaceText("hong"));        ViewInteraction appCompatEditText5 = onView(                allOf(withId(R.id.etxt_pass),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatEditText5.perform(replaceText("123"));        ViewInteraction appCompatButton = onView(                allOf(withId(R.id.btn_login), withText("登陆"),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatButton.perform(click());}

这里写图片描述

这个时候运行会有以下类似错误

//at dalvik.system.VMStack.getThreadStackTrace(Native Method) ....

意思就是找不到 这个控件

那么这是为什么呢,
前面我们说过程序流程 在LauchActivity有耗时操作,
但我们的EditText是在LoginActivity find的,所以app还没有到LoginActivity,
测试线程在LauchActivity时间段找 控件,是找不到的,所以测试失败
那么解决办法就是等待LoginActivity启动再去找这个控件

        Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("sun.com.autotextapplication.LoginActivity", null, false);        getInstrumentation().addMonitor(am);        getInstrumentation().waitForMonitor(am);

当然,有时候我们会有一些动画,动画完了才会出来布局,
那么这个时候怎么去找控件呢

解决办法就是 直接将测试线程 sleep(…),之后再去找控件

        try {            Thread.sleep(2000);        } catch (InterruptedException e) {            e.printStackTrace();        }

下面是我更改后的代码

    @Test    public void lauchActivityTest() {        //at dalvik.system.VMStack.getThreadStackTrace(Native Method)        Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("sun.com.autotextapplication.LoginActivity", null, false);        getInstrumentation().addMonitor(am);        getInstrumentation().waitForMonitor(am);        ViewInteraction appCompatEditText = onView(                allOf(withId(R.id.etxt_name),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatEditText.perform(click());        ViewInteraction appCompatEditText2 = onView(                allOf(withId(R.id.etxt_name),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatEditText2.perform(replaceText("hong"));        ViewInteraction appCompatEditText5 = onView(                allOf(withId(R.id.etxt_pass),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatEditText5.perform(replaceText("123"));        ViewInteraction appCompatButton = onView(                allOf(withId(R.id.btn_login), withText("登陆"),                        withParent(allOf(withId(R.id.activity_login),                                withParent(withId(android.R.id.content)))),                        isDisplayed()));        appCompatButton.perform(click());        Instrumentation.ActivityMonitor amMain = new Instrumentation.ActivityMonitor("sun.com.autotextapplication.MainActivity", null, false);        getInstrumentation().addMonitor(amMain);        getInstrumentation().waitForMonitor(amMain);        try {            Thread.sleep(20000);        } catch (InterruptedException e) {            e.printStackTrace();        }    }

好了大功告成,然后执行测试代码
这里写图片描述

参考资料
https://developer.android.com/reference/android/app/Instrumentation.html

https://riggaroo.co.za/automated-android-testing-part-2-setup/?utm_source=Android+Weekly&utm_campaign=905515fe31-Android_Weekly_213&utm_medium=email&utm_term=0_4eb677ad19-905515fe31-337851921

国内测试博客
http://kikujiang.com/2016/05/07/android-test-junit-160507/ Android测试之旅(一)
http://kikujiang.com/2016/05/07/android-test-junit-160508/ Android测试之旅(二)
http://www.jianshu.com/p/03118c11c199 在Android Studio中进行单元测试和UI测试

http://blog.csdn.net/eclipsexys/article/details/45622813 解放双手——Android自动化测试

3 0
原创粉丝点击