首页 > 解决方案 > 在浓缩咖啡测试中瞄准 NachoChip

问题描述

我正在开发一个使用NachoChips的应用程序,并且我正在努力以他们为目标,下拉他们实施的建议。测试包含它们的片段时我想要发生的是:
某些文本输入到字段
下拉出现
单击下拉。
我曾尝试使用 espresso 录制来重新创建它,但是将这样做的代码移植到实际测试中证明是徒劳的,只给出了 androidx.test.espresso.NoMatchingViewException。以下是录音中的完整代码:

ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.editText_username_input),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.RelativeLayout")),
                                        1),
                                3),
                        isDisplayed()));
        appCompatEditText.perform(replaceText("test2@l.com"), closeSoftKeyboard());

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.editText_password_input),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.RelativeLayout")),
                                        1),
                                2),
                        isDisplayed()));
        appCompatEditText2.perform(replaceText("123"), closeSoftKeyboard());

        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.button_login), withText("Login"),
                        childAtPosition(
                                childAtPosition(
                                        withClassName(is("android.widget.RelativeLayout")),
                                        1),
                                4),
                        isDisplayed()));
        appCompatButton.perform(click());

        ViewInteraction bottomNavigationItemView = onView(
                allOf(withId(R.id.ic_camera), withContentDescription("Picture"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.bottomNavView_bar),
                                        0),
                                1),
                        isDisplayed()));
        bottomNavigationItemView.perform(click());

        ViewInteraction appCompatSpinner = onView(
                allOf(withId(R.id.planSelectSpinner), withContentDescription("Dropdown menu for selecting plan to submit with picture"),
                        childAtPosition(
                                allOf(withId(R.id.LinearLayout),
                                        childAtPosition(
                                                withId(R.id.fragment_container),
                                                0)),
                                2),
                        isDisplayed()));
        appCompatSpinner.perform(click());

        DataInteraction textView = onData(anything())
                .inAdapterView(childAtPosition(
                        withClassName(is("android.widget.PopupWindow$PopupBackgroundView")),
                        0))
                .atPosition(4);
        textView.perform(click());

        ViewInteraction nachoTextView = onView(
                allOf(withId(R.id.medicationInputNachoEditText),
                        childAtPosition(
                                allOf(withId(R.id.MedicationInputLayout),
                                        childAtPosition(
                                                withId(R.id.MedicationLinearLayout),
                                                0)),
                                2)));
        nachoTextView.perform(scrollTo(), replaceText("Minocycline"), closeSoftKeyboard());

        DataInteraction appCompatTextView = onData(anything())
                .inAdapterView(childAtPosition(
                        withClassName(is("android.widget.PopupWindow$PopupBackgroundView")),
                        0))
                .atPosition(0);
        appCompatTextView.perform(click());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.dosageInputEditText),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.MedicationInputLayout),
                                        3),
                                0),
                        isDisplayed()));
        appCompatEditText3.perform(replaceText("10"), closeSoftKeyboard());

        ViewInteraction appCompatEditText4 = onView(
                allOf(withId(R.id.timesDailyInputEditText),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.MedicationInputLayout),
                                        3),
                                2),
                        isDisplayed()));
        appCompatEditText4.perform(replaceText("2"), closeSoftKeyboard());

        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.addMedicationButton), withText("Add Medication"),
                        childAtPosition(
                                allOf(withId(R.id.MedicationInputLayout),
                                        childAtPosition(
                                                withId(R.id.MedicationLinearLayout),
                                                0)),
                                4)));
        appCompatButton2.perform(scrollTo(), click());

        ViewInteraction multiAutoCompleteTextView = onView(
                allOf(withId(R.id.medicationsInputtedDisplay), withText(" .Minocycline 10mg, 2xDaily. "),
                        withParent(allOf(withId(R.id.MedicationInputLayout),
                                withParent(withId(R.id.MedicationLinearLayout)))),
                        isDisplayed()));
        multiAutoCompleteTextView.check(matches(isDisplayed()));

这是测试代码本身:

package com.androidapp.zitgenius;


import static androidx.test.core.app.ActivityScenario.launch;
import static androidx.test.espresso.Espresso.onData;
import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.closeSoftKeyboard;
import static androidx.test.espresso.action.ViewActions.replaceText;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.intent.Intents.intended;
import static androidx.test.espresso.intent.matcher.IntentMatchers.hasComponent;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withParent;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static com.androidapp.zitgenius.AddMedication.childAtPosition;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.is;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.testing.FragmentScenario;
import androidx.test.core.app.ActivityScenario;
import androidx.test.espresso.DataInteraction;
import androidx.test.espresso.ViewInteraction;
import androidx.test.espresso.intent.Intents;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

/**
 * Instrumented test of NewPlan fragment
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)

public class NewPlanFragmentTest {
    public final String INCORRECT_USERNAME = "this isn't a real username";
    public final String INCORRECT_PASSWORD = "this isn't a real password";
    public final String CORRECT_USERNAME = "test2@l.com";
    public final String CORRECT_PASSWORD = "123";
    private static final String SHARE_PREF_NAME ="login_ref" ;
    private static final String KEY_USER_ID ="id" ;

    public FragmentScenario<NewPlanFragment> scenario;
    private ActivityScenario<BottomNavigationActivity> activityScenario = null;

    @Before
    public void setUp() {
        Intents.init();
        scenario = FragmentScenario.launchInContainer(NewPlanFragment.class);
    }
    @Test
    public void testEventFragment() {

        ViewInteraction nachoTextView = onView(withId(R.id.planNameInputEditText));
        nachoTextView.perform(scrollTo(), replaceText("Minocycline"), closeSoftKeyboard());

        DataInteraction appCompatTextView = onData(anything())
                .inAdapterView(childAtPosition(
                        withClassName(is("android.widget.PopupWindow$PopupBackgroundView")),
                        0))
                .atPosition(0);
        appCompatTextView.perform(click());

        ViewInteraction appCompatEditText3 = onView(
                allOf(withId(R.id.dosageInputEditText),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.MedicationInputLayout),
                                        3),
                                0),
                        isDisplayed()));
        appCompatEditText3.perform(replaceText("10"), closeSoftKeyboard());

        ViewInteraction appCompatEditText4 = onView(
                allOf(withId(R.id.timesDailyInputEditText),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.MedicationInputLayout),
                                        3),
                                2),
                        isDisplayed()));
        appCompatEditText4.perform(replaceText("2"), closeSoftKeyboard());

        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.addMedicationButton), withText("Add Medication"),
                        childAtPosition(
                                allOf(withId(R.id.MedicationInputLayout),
                                        childAtPosition(
                                                withId(R.id.MedicationLinearLayout),
                                                0)),
                                4)));
        appCompatButton2.perform(scrollTo(), click());

        ViewInteraction multiAutoCompleteTextView = onView(
                allOf(withId(R.id.medicationsInputtedDisplay), withText(" .Minocycline 10mg, 2xDaily. "),
                        withParent(allOf(withId(R.id.MedicationInputLayout),
                                withParent(withId(R.id.MedicationLinearLayout)))),
                        isDisplayed()));
        multiAutoCompleteTextView.check(matches(isDisplayed()));

    }
    @After
    public void tearDown() throws Exception {
        Intents.release();
    }

    }

如您所见,在测试中,我尝试从 FragmentScenario 开始,但录制从我的应用程序的登录页面开始(必要时),这可能是问题的原因。
我也尝试使用 android studio 的 LiveLayout 功能,但任何尝试在下拉建议出现后实际选择它只会抓住下拉列表后面的元素。
有想法该怎么解决这个吗?

标签: androidandroid-studioandroid-espressoandroid-chips

解决方案


推荐阅读