1.將android studio 版本升級到3.0+
2.百度下載夜神模擬器html
PS:以上就是夜神模擬器的基本設置android
//夜神模擬器默認的地址
adb connect 127.0.0.1:62001
在頂部工具欄找到Run -> Record Espresso Test -> 選擇夜神模擬器雙擊啓動 -> 啓動後界面以下圖
PS:操做模擬器 -> Record Your Test 彈框將自動生成你的行爲代碼 -> 點擊OK -> 命名並保存代碼ios
注意每次運行都會從新安裝,從啓動頁開始,可是能夠針對功能點錄製,而後生成一份一份的測試腳本git
PS:靜待安裝,而後模擬器會自動執行你錄製的動做,Logcat能夠查看日誌github
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:代碼其實很簡單,照搬照套,還有別的花樣麼...錄製的時候,也就是操做模擬器,生成代碼的時候有點卡....有沒有人告訴我怎麼解決?app