Androidのメーカー3.0+録音エスプレッソテスト自動テスト

準備

1.バージョン3.0+アンドロイドスタジオにアップグレードしてください
。2. Baiduの夜神シミュレータのダウンロード


神シミュレータの基本設定

 

 

 

 

PS:これらは神のシミュレータのための基本的な設定です

Androidのスタジオ接続矢上シミュレータ

// 神シミュレータのデフォルトのアドレス 
ADBは127.0.0.1:62001を接続します

 

自動テストコードの記録を開始

上部のツールバーで[実行を探す - >録音エスプレッソテスト - > [開始]をダブルクリックして神のシミュレータを選択 - >インタフェースを起動した後に示されています

 

PS:運転シミュレータ - >テスト爆弾ボックスが自動的にあなたの行動のためのコードを生成します記録 - > [OK]をクリック - >名前とコードを保存する(ページがページ記録すること、およびその後、テストスクリプトのコピーを生成する場合があります)

自動テストスクリプトを実行します

PS:インストールを待ち、その後、シミュレータが自動的に記録されたアクションを実行する、Logcatは、ログを表示することができます


録音エスプレッソテストスクリプトを貼り付けると見ます

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

 

おすすめ

転載: www.cnblogs.com/94xiyang/p/11068928.html