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 {@Rulepublic ActivityTestRule<SplashActivity> mActivityTestRule = new ActivityTestRule<>(SplashActivity.class);@Testpublic 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.htmltry {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.htmltry {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.htmltry {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.htmltry {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>() {@Overridepublic void describeTo(Description description) {description.appendText("Child at position " + position + " in parent ");parentMatcher.describeTo(description);}@Overridepublic boolean matchesSafely(View view) {ViewParent parent = view.getParent();return parent instanceof ViewGroup && parentMatcher.matches(parent)&& view.equals(((ViewGroup) parent).getChildAt(position));}};} }
ps:代码其实很简单,照搬照套,还有别的花样么...录制的时候,也就是操作模拟器,生成代码的时候有点卡....有没有人告诉我怎么解决?
转载于:https://www.cnblogs.com/94xiyang/p/11068928.html
Android Studio 3.0+ Record Espresso Test 自动化测试相关推荐
- (Android Studio 3.0)Android Profiler内存泄漏检查
前提概要 内存泄漏是常见又重要的问题,针对这个问题谷歌在Android Studio 3.0中推出了Android Profiler.笔者此篇文章主要记录一下Android Profiler在内存泄漏 ...
- 告别编译运行 ---- Android Studio 2.0 Preview发布Instant Run功能
以往的Android开发有一个头疼的且拖慢速度的问题,就是你每改一行代码要想看到结果必须要编译运行到手机或者模拟器上,而且需要从头(可能是登录界面)一直点击到你修改的界面为止.开发一个完整的Andro ...
- Android Studio 3.0 安装注意点
在安装Android studio 3.0+ 时候,会遇到默认不带Android SDK 的问题. 在启动Android studio 后,会提示让选择SDK目录,选择下载目录,对应的去下载 那么问题 ...
- android studio项目总结,android studio 3.0 升级 项目遇到的问题及更改思路(问题小结)...
Android Studio从3.0版本新增了许多功能,当然首当其冲就是从3.0版本新增了对 Kotlin 开发语言的支持,除此之外还有其他一些新功能,例如:Android Profiler (其中包 ...
- android studio m1,Android Studio 4.0发布 为Android 11做好准备
新的 Motion 编辑器 MotionLayout API扩展了ConstraintLayout丰富的功能,可以帮助 Android 开发管理应用的复杂运动和 widget 动画,在 Android ...
- android studio 3.0新功能介绍
谷歌2017发布会更新了挺多内容的,而且也发布了AndroidStudio3.0预览版,一些功能先睹为快.(英语一般,有些翻译不太好) 下载地址 https://developer.android.g ...
- Google发布Android Studio 1.0
第一个IDE稳定版本:Google发布Android Studio 1.0 摘要:Google于今天发布了Android Studio 1.0,其新增的突出特性包括:首次安装向导方面.示例导入和 ...
- android studio device功能,Android Studio 3.0找不到Android Device Monitor
因为自Android Studio 3.0开始弃用Android Device Monitor,Android Developers官网上的原话是: Android Device Monitor is ...
- 图解Android Studio 2.0安装步骤
转载自: 图解Android Studio 2.0安装步骤_百度经验 http://jingyan.baidu.com/article/a948d6510b54fb0a2dcd2e30.html
最新文章
- linux 查看链接最终目标,linux学习笔记7-链接
- Python 网页编程- Pyramid 安装测试
- Base64 编解码
- transit, transfer, convey-conveyance, transport-transportation
- react 中使用import()实现按需加载报错 解决方法 --‘import’ and ‘export’ may only appear at the top level
- String StringBuffer StringBuilder的异同
- 【温故而知新-Javascript】使用canvas元素(第一部分)
- python学习笔记1
- SDUT—Python程序设计实验四(字符串)
- AxureRP9 主功能界面
- 雷军内部信:米家有品拆分 做独立电商
- 【重构-改善代码】笔记
- visio取消捕捉连接点方法
- mysql重启数据库实例命令_mysql重启数据库实例命令
- PDN电源完整性设计
- C语言 将十六进制字符串转为十六进制数 (二进制、十进制都适用)
- 【Axure高保真原型】中继器版下拉列表
- 武汉理工考研复试计算机,2017武汉理工计算机复试
- ARP病毒专杀工具免费下载
- 易语言时间转化linux格式,易语言取时间年月日格式化
热门文章
- stl的set,multiset, map, multimap, deque, list, stack, queue, priority_queue
- SDOI2015寻宝游戏 dfs序+set
- ubuntu 16.04下源码安装opencv3.4
- springMvc 的参数验证 BindingResult result 的使用
- C# 获取字符串长度 获取字符串字节长度
- 获取json的节点名称
- CF 6 A. Triangle
- Java和Android中一些常用的公共方法
- linux 内核网络协议栈--linux内核路由机制(一)
- mysql 按照指定字段拼接_mysql 根据某个字段将多条记录的某个字段拼接成一个字段...