Espresso에 대화 상자가 표시되는지 확인
새로운 android-test-kit (Espresso)으로 몇 가지 테스트를 작성하려고합니다 . 그러나 대화 상자가 표시 되는지 확인하고 대화 상자에서 몇 가지 작업을 수행하는 방법에 대한 정보를 찾을 수 없습니다 (예 : 양수 및 음수 버튼 클릭 등). 대화 상자는 WebView응용 프로그램 자체 가 아니라로 표시 될 수도 있습니다 .
어떤 도움을 주시면 감사하겠습니다. 기본에 대한 링크 또는 몇 가지 예제 코드가 필요합니다.
- 대화 상자가 나타나는지 확인
- 대화 버튼 클릭 수행
- 대화 상자의 내부보기와 상호 작용 (사용자 정의보기 인 경우)
- 프리폼이 대화 상자 외부를 클릭하고 표시되는지 확인합니다 (예 :
setCancelable(false)대화 상자 작성기에서 호출되었고 확인하려는 경우).
조언 감사합니다!
대화 상자가 나타나는지 확인하려면 대화 상자 내에있는 텍스트가있는보기가 표시되는지 확인하면됩니다.
onView(withText("dialogText")).check(matches(isDisplayed()));또는 ID가있는 텍스트를 기반으로
onView(withId(R.id.myDialogTextId)).check(matches(allOf(withText(myDialogText), isDisplayed()));대화 상자 버튼을 클릭하려면 다음과 같이하십시오 (button1-확인, button2-취소) :
onView(withId(android.R.id.button1)).perform(click());최신 정보
- Espresso는 다중 창을 지원하기 때문에 가능하다고 생각합니다 .
- 사용자 정의 대화 상자보기 외부를 클릭하는 것은 확실하지 않지만 표시되는지 여부를 확인하려면 사용자 정의 매처를 만들고 내부를 확인해야합니다.
나는 현재 이것을 사용하고 있으며 잘 작동하는 것 같습니다.
onView(withText(R.string.my_title))
.inRoot(isDialog()) // <---
.check(matches(isDisplayed()));
다음과 같은 AlertDialog가있는 경우 :
구성 요소가 표시되는지 확인할 수 있습니다.
int titleId = mActivityTestRule.getActivity().getResources()
.getIdentifier( "alertTitle", "id", "android" );
onView(withId(titleId))
.inRoot(isDialog())
.check(matches(withText(R.string.my_title)))
.check(matches(isDisplayed()));
onView(withId(android.R.id.text1))
.inRoot(isDialog())
.check(matches(withText(R.string.my_message)))
.check(matches(isDisplayed()));
onView(withId(android.R.id.button2))
.inRoot(isDialog())
.check(matches(withText(android.R.string.no)))
.check(matches(isDisplayed()));
onView(withId(android.R.id.button3))
.inRoot(isDialog())
.check(matches(withText(android.R.string.yes)))
.check(matches(isDisplayed()));
조치를 수행하십시오.
onView(withId(android.R.id.button3)).perform(click());
수락 된 대답이 아닌 질문 4에 대답하기 위해 다음 코드를 수정했습니다. 여기에 토스트가 표시되었는지 테스트하기 위해 Stack Overflow ( link ) 에서 찾았습니다 .
@NonNull
public static ViewInteraction getRootView(@NonNull Activity activity, @IdRes int id) {
return onView(withId(id)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))));
}
The id passed in is the id of a View currently displayed in your dialog. You could also write the method like so:
@NonNull
public static ViewInteraction getRootView(@NonNull Activity activity, @NonNull String text) {
return onView(withText(text)).inRoot(withDecorView(not(is(activity.getWindow().getDecorView()))));
}
And now it's looking for a View containing a particular text string.
Use it like so:
getRootView(getActivity(), R.id.text_id).perform(click());
Just in case anyone stumbles across this question like I did. All the answers will only work for dialogs WITH dialog buttons. Do not try and use this for progress dialogs without user interaction. Espresso keeps waiting for the app to enter an idle state. As long as the progress dialog is visible the app is not idle.
The button Ids R.id.button1 and R.id.button2 are not going to be same across devices. The Ids may change with the OS versions.
The correct way to achieve this is to use UIAutomator. Include UIAutomator dependency in your build.gradle
// Set this dependency to build and run UI Automator tests
androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
and use
// Initialize UiDevice instance
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
// Search for correct button in the dialog.
UiObject button = uiDevice.findObject(new UiSelector().text("ButtonText"));
if (button.exists() && button.isEnabled()) {
button.click();
}
참고 URL : https://stackoverflow.com/questions/21045509/check-if-a-dialog-is-displayed-with-espresso
'program story' 카테고리의 다른 글
| 연결이 로컬 호스트인지 javascript로 확인하는 방법은 무엇입니까? (0) | 2020.10.18 |
|---|---|
| 목록 항목을 가장 좋은 방법으로 바꾸는 방법 (0) | 2020.10.18 |
| Chrome 기기 모드 에뮬레이션 미디어 쿼리가 작동하지 않음 (0) | 2020.10.18 |
| 배열 또는 목록이 기본적으로 C #에서 참조로 전달됩니까? (0) | 2020.10.18 |
| PostgreSQL : 명령 줄에서 매개 변수를 전달하는 방법은 무엇입니까? (0) | 2020.10.18 |
