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:代码其实很简单,照搬照套,还有别的花样么...录制的时候,也就是操作模拟器,生成代码的时候有点卡....有没有人告诉我怎么解决?