Android Studio 3.0+ Record Espresso Test 自动化测试
准备工作
1.将android studio 版本升级到3.0+
2.百度下载夜神模拟器
夜神模拟器的基本设置



PS:以上就是夜神模拟器的基本设置
Android Studio 连接夜神模拟器
//夜神模拟器默认的地址
adb connect 127.0.0.1:62001
开始录制自动测试代码
在顶部工具栏找到Run -> Record Espresso Test -> 选择夜神模拟器双击启动 -> 启动后界面如下图

PS:操作模拟器 -> Record Your Test 弹框将自动生成你的行为代码 -> 点击OK -> 命名并保存代码
注意每次运行都会重新安装,从启动页开始,但是可以针对功能点录制,然后生成一份一份的测试脚本
运行自动化测试脚本
PS:静待安装,然后模拟器会自动执行你录制的动作,Logcat可以查看日志
贴上一份 Record Espresso Test的脚本看看
package com.example.administrator.teagarden.activity.login; import android.support.test.espresso.ViewInteraction;
import android.support.test.filters.LargeTest;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import com.example.administrator.teagarden.R;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
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.closeSoftKeyboard;
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.withClassName;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.is; @LargeTest
@RunWith(AndroidJUnit4.class)
public class SplashActivityTest { @Rule
public ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class); @Test
public void splashActivityTest() {
// Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(6000);
} catch (InterruptedException e) {
e.printStackTrace();
} ViewInteraction appCompatEditText = onView(
allOf(withId(R.id.login_edit_user),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
2),
0),
isDisplayed()));
appCompatEditText.perform(click()); ViewInteraction appCompatEditText2 = onView(
allOf(withId(R.id.login_edit_user),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
2),
0),
isDisplayed()));
appCompatEditText2.perform(replaceText("152****3478"), closeSoftKeyboard()); ViewInteraction appCompatEditText3 = onView(
allOf(withId(R.id.login_edit_mima),
childAtPosition(
allOf(withId(R.id.logint_edit_layout),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
4)),
0),
isDisplayed()));
appCompatEditText3.perform(replaceText("admin000"), closeSoftKeyboard()); ViewInteraction appCompatButton = onView(
allOf(withId(R.id.login_button), withText("登录"),
childAtPosition(
childAtPosition(
withClassName(is("android.widget.RelativeLayout")),
1),
2),
isDisplayed()));
appCompatButton.perform(click()); // Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} ViewInteraction radioButton = onView(
allOf(withId(R.id.main_home_radio2), withText("监控"),
childAtPosition(
allOf(withId(R.id.main_tab_group),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
0)),
1),
isDisplayed()));
radioButton.perform(click()); // Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} ViewInteraction radioButton2 = onView(
allOf(withId(R.id.main_home_radio3), withText("动态"),
childAtPosition(
allOf(withId(R.id.main_tab_group),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
0)),
2),
isDisplayed()));
radioButton2.perform(click()); // Added a sleep statement to match the app's execution delay.
// The recommended way to handle such scenarios is to use Espresso idling resources:
// https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
} ViewInteraction radioButton3 = onView(
allOf(withId(R.id.main_home_radio4), withText("我的"),
childAtPosition(
allOf(withId(R.id.main_tab_group),
childAtPosition(
withClassName(is("android.widget.LinearLayout")),
0)),
3),
isDisplayed()));
radioButton3.perform(click());
} private static Matcher<View> childAtPosition(
final Matcher<View> parentMatcher, final int position) { return new TypeSafeMatcher<View>() {
@Override
public void describeTo(Description description) {
description.appendText("Child at position " + position + " in parent ");
parentMatcher.describeTo(description);
} @Override
public boolean matchesSafely(View view) {
ViewParent parent = view.getParent();
return parent instanceof ViewGroup && parentMatcher.matches(parent)
&& view.equals(((ViewGroup) parent).getChildAt(position));
}
};
}
}
ps:代码其实很简单,照搬照套,还有别的花样么...录制的时候,也就是操作模拟器,生成代码的时候有点卡....有没有人告诉我怎么解决?
Android Studio 3.0+ Record Espresso Test 自动化测试的更多相关文章
- Android Studio 2.2 Record Espresso Test
Android Studio 已经更新到了2.2,在 Run 中发现了 Record Espresso Test 功能,很强大,但是不稳定. 尝试了下在 Android 6.0 上的登录页面,可以通过 ...
- Android Studio 3.0 变化之 implementation与compile
Android Studio 3.0 出来很久了,本文就着重介绍一下 新版本中 Moudle 中 build.gradle 文件中的变化. 我们来看看新建一个项目在 Moudle 中的 depende ...
- Android Studio 2.0使用指南
一.下载界面.[无激活码 无序列码 无毒请放心使用][需将JAVA程序升级到1.8] 网址:http://www.android-studio.org/index.php/download/andro ...
- Android Studio 1.0.2项目实战——从一个APP的开发过程认识Android Studio
Android Studio 1.0.1刚刚发布不久,谷歌紧接着发布了Android Studio 1.0.2版本,和1.0.0一样,是一个Bug修复版本.在上一篇Android Studio 1.0 ...
- Android Studio 1.0.1 + Genymotion安卓模拟器打造高效安卓开发环境
我们开发安卓大多是使用Eclipse和安卓SDK中自带的安卓模拟器.当然,Google早就推出了自己的安卓开发环境——Android studio,在不久前,Google发布了Android Stud ...
- [Android] 环境配置之正式版Android Studio 1.0
昨天看见 Android Studio 1.0 正式版本发布了:心里挺高兴的. 算是忠实用户了吧,从去年开发者大会一开始出现 AS 后就开始使用了:也是从那时开始就基本没有用过 Eclipse 了:一 ...
- [Android] android studio 2.0即时运行功能探秘
即时运行instant Run是android studio 2中,开发人员最关心的特性之一 在google发布studio 2.0之后,马上更新体验了一把,然而发现,并没快多少,说好的即时运行呢? ...
- Windows环境下Android Studio v1.0安装教程
Windows环境下Android Studio v1.0安装教程 准备工具 JDK安装包. 要求:JDK 7以及以上版本. Android Studio安装文件. Windows: exe(包含SD ...
- 告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能
以往的Android开发有一个头疼的且拖慢速度的问题,就是你每改一行代码要想看到结果必须要编译运行到手机或者模拟器上,而且需要从头(可能是登录界面)一直点击到你修改的界面为止.开发一个完整的Andro ...
随机推荐
- 学习之Redis(一)
一.redis简介 一般学习,最好先去官网,之所以建议看官网,是因为这是一手的学习资料,其他资料都最多只能算二手,一手资料意味着最权威,准确性最高.https://redis.io/topics/in ...
- 智能家居CC2530功率放大组网RFX2401C和AT2401C的区别
两者最大的区别就是RFX2401C的增益为12dbmAT2401C的增益为14dbm这就会导致AT2401C的功耗会比RFX2401C大一点点,但距离也会相对更远,并且增加了EDS防静电等级,多出2个 ...
- Reachability的使用
刚到一家新公司 做新项目 关于网络状态的监听和同事产生了不一样的看法 原来我的网络监听都是自己写的 后来发现自己不是一般的傻 有一个叫做Reachability的东西 很简单 很实用 很暴力 下面就是 ...
- MyBatis框架的基本配置
MyBatis的基本配置文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE config ...
- BZOJ 1861书架
小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本.由于这些书太有吸引 ...
- Hybrid App 应用开发中 9 个必备知识点复习(WebView / 调试 等)
前言 我们大前端团队内部 ?每周一练 的知识复习计划继续加油,本篇文章是 <Hybrid APP 混合应用专题> 主题的第二期和第三期的合集. 这一期共整理了 10 个问题,和相应的参考答 ...
- Elasticsearch系列---搜索分页和deep paging问题
概要 本篇从介绍搜索分页为起点,简单阐述分页式数据搜索与原有集中式数据搜索思维方式的差异,就分页问题对deep paging问题的现象进行分析,最后介绍分页式系统top N的案例. 搜索分页语法 El ...
- c++之运算符
运算符分为:算数运算符.赋值运算符.比较运算符.逻辑运算符 算数运算符:+(正) -(负) + - * / % i++(先赋值后自增) ++i(先自增后赋值) i--(先赋值后自减) --i(先自减后 ...
- ETL子系统
最近在看<Pentaho Kettle 解决方案>,看到 ETL子系统,发现信息量比较大,用简短的语句做一下笔记. ETL子系统有34种子系统,被分成4个部分:抽取.清洗和更正.发布.管理 ...
- javascript的ES6学习总结(第三部分)
1.ES6中的面向对象的类 1.1.定义类 在ES5中,我们写一个类,通常是这么写的 function Person(name,age){ this.name = name; this.age = a ...