Espresso-특정 작업을 수행 한 후 활동이 시작되었는지 어떻게 확인할 수 있습니까?
다음은 내 Espresso 테스트 사례 중 하나입니다.
public void testLoginAttempt() {
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@krossover.com"));
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));
Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
// AFTER CLICKING THE BUTTON, A NEW ACTIVITY WILL POP UP.
// Clicking launches a new activity that shows the text entered above. You don't need to do
// anything special to handle the activity transitions. Espresso takes care of waiting for the
// new activity to be resumed and its view hierarchy to be laid out.
Espresso.onView(ViewMatchers.withId(R.id.action_logout))
.check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())));
}
현재 내가 한 일은 새 활동 (R.id.action_logout)의보기가 표시되는지 여부를 확인하는 것입니다. 보이는 경우 활동이 성공적으로 열린 것으로 가정합니다. 하지만 예상대로 작동하지 않는 것 같습니다. 해당 활동의보기가 표시되는지 확인하는 대신 새 활동이 성공적으로 시작되었는지 확인하는 더 좋은 방법이 있습니까? 감사
당신이 사용할 수있는:
intended(hasComponent(YourExpectedActivity.class.getName()));
이 gradle 항목이 필요합니다.
androidTestCompile ("com.android.support.test.espresso:espresso-intents:$espressoVersion")
intended()및에 대한 가져 오기hasComponent()
import static android.support.test.espresso.intent.Intents.intended;
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent;
Shubam Gupta가 언급했듯이 전화 Intents.init()하기 전에 전화하는 것을 잊지 마십시오 intended(). 결국 @Before메서드 에서 호출 할 수 있습니다 .
시험:
intended(hasComponent(YourActivity.class.getName()));
또한 명심하십시오
java.lang.NullPointerExceptionIntents.init()전에 호출되지 않으면 던져집니다.intended()
시도
intended(hasComponent(new ComponentName(getTargetContext(), ExpectedActivity.class)));
문제는 로그인 버튼을 클릭하면 응용 프로그램이 네트워크 작업을 수행한다는 것입니다. Espresso는 기본적으로 완료하기 위해 네트워크 호출을 처리 (대기)하지 않습니다. IdlingResource가 네트워크 요청이 완료되었음을 의미하는 IdlingResource가 다시 Idle 상태로 돌아올 때까지 Espresso가 테스트를 진행하지 못하도록 차단하는 사용자 지정 IdlingResource를 구현해야합니다. Espresso 샘플 페이지 살펴보기-https: //google.github.io/android-testing-support-library/samples/index.html
Espresso 인 텐트 라이브러리가 gradle 종속성에 있는지 확인
androidTestImplementation "com.android.support.test.espresso:espresso-intents:3.0.1"
그런 다음이 두 가지를 테스트 파일로 가져옵니다
import static android.support.test.espresso.intent.Intents.intended
import static android.support.test.espresso.intent.matcher.IntentMatchers.hasComponent
그런 다음 테스트 클래스 에 IntentsTestRule 을 추가 하십시오.
@Rule
@JvmField
val mainActivityRule = IntentsTestRule(MainActivity::class.java)
마지막으로 활동이 의도를 시작했는지 확인하십시오.
@Test
fun launchActivityTest() {
onView(ViewMatchers.withId(R.id.nav_wonderful_activity))
.perform(click())
intended(hasComponent(WonderfulActivity::class.java!!.getName()))
}
다음과 같이 할 수 있습니다.
@Test
public void testLoginAttempt() {
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("nonexistinguser@example.com"));
Espresso.onView(ViewMatchers.withId(R.id.username)).perform(ViewActions.clearText()).perform(ViewActions.typeText("invalidpassword"));
Intents.init();
Espresso.onView(ViewMatchers.withId(R.id.login_button)).perform(ViewActions.click());
Intents.release();
}
java.lang.NullPointerException is thrown if Intents.init() is not called.
I use this approach:
// The IntentsTestRule class initializes Espresso Intents before each test, terminates the host activity, and releases Espresso Intents after each test
@get:Rule
var tradersActivity: IntentsTestRule<TradersActivity> = IntentsTestRule(TradersActivity::class.java)
@get:Rule
var jsonViewActivity: IntentsTestRule<JsonViewActivity> = IntentsTestRule(JsonViewActivity::class.java)
@Test
fun scrollToItemAndClick() {
onView(withId(R.id.tradersRecyclerView)).perform(RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>(ITEM_POS, click()))
// check is activity was started
intended(hasComponent(JsonViewActivity::class.java.getName()))
}
@RunWith(RobolectricTestRunner.class)
public class WelcomeActivityTest {
@Test
public void clickingLogin_shouldStartLoginActivity() {
WelcomeActivity activity = Robolectric.setupActivity(WelcomeActivity.class);
activity.findViewById(R.id.login).performClick();
Intent expectedIntent = new Intent(activity, LoginActivity.class);
assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);
}
}
ReferenceURL : https://stackoverflow.com/questions/25998659/espresso-how-can-i-check-if-an-activity-is-launched-after-performing-a-certain
'Program Club' 카테고리의 다른 글
| Play 스토어 알파 테스트 다운로드 링크가 작동하지 않음 (0) | 2021.01.07 |
|---|---|
| 찾기와 색인의 차이점 (0) | 2021.01.07 |
| JSON.NET 데이터 구문 분석 중 구문 분석 오류 무시 (0) | 2021.01.07 |
| mdDialog에 데이터 전달 (0) | 2021.01.07 |
| JavaScript의 Razor 모델 개체에서 JSON 개체를 가져 오는 방법 (0) | 2021.01.06 |