#android #android-studio #android-espresso #android-chips
Вопрос:
Я разрабатываю приложение, которое использует начочипы, и я изо всех сил пытаюсь нацелить их на выпадающие предложения, которые они реализуют. Что я хочу, чтобы при тестировании фрагмента, содержащего их, произошло следующее:
Определенный текст вводится в выпадающее поле
, появляется выпадающее меню, нажимается.
Я попытался использовать запись эспрессо, чтобы воссоздать это, но пересадка кода из этого в фактический тест оказалась бесполезной, только создав исключение 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();
}
}
As you can see, in the test I’m trying to start from a FragmentScenario, but the recording starts in my app’s login page (by necessity) which may be the cause of the issue.
I also tried using the LiveLayout feature of android studio but any attempts to actually select the drop-down suggestion after it appeared would just grab an element behind the dropdown.
Any ideas on how to fix this?