Android Studio 3.0+ Record Espresso Test 自动化测试
生活随笔
收集整理的這篇文章主要介紹了
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 自动化测试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 字符串前加u r b的意义
- 下一篇: 函数参数 不定参数,和 默认参数