6

Tôi muốn nhấn nút dưới đây bằng cách sử dụng Espresso, nhưng tôi không chắc chắn như thế nào. Tôi có nên lấy tài nguyên-id không? Hoặc làm thế nào để thiết lập một ID cho AlertDialog ??Làm thế nào để sử dụng espresso để nhấn một nút AlertDialog

enter image description here

@RunWith(AndroidJUnit4.class) 
public class ApplicationTest { 

@Rule 
public ActivityTestRule<LoadingActivity> mActivityRule = 
     new ActivityTestRule<>(LoadingActivity.class); 

@Test 
public void loginClickMarker() { 
//Doesn't work: 
    onView(withText("GA NAAR INSTELLINGEN")).perform(click()); 
} 
} 

public class PopupDialog { 

public static void showGPSIsDisabled(Context context, String msg, final PopupDialogCallback popupDialogCallback) { 
    new AlertDialog.Builder(context) 
      .setTitle(context.getString(R.string.location_turned_off)) 
      .setMessage(msg) 
      .setPositiveButton(context.getString(R.string.go_to_settings), new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.dismiss(); 
        popupDialogCallback.hasClicked(); 
       } 
      }).show(); 
} 
} 

android.support.test.espresso.NoMatchingViewException: Không có quan điểm trong phân cấp trùng khớp với: với văn bản: là "GA Naar INSTELLINGEN"

+0

Tôi nghĩ bạn có thể sử dụng phương thức withText() thay vì khớp với id – jeprubio

+0

onView (withText ("GA NAAR INSTELLINGEN")). (Thực hiện (click()); không hoạt động –

Trả lời

11

Theo StackOverflow vấn đề tương tự: Check if a dialog is displayed with Espresso

Bạn nên thay đổi mã của mình từ:

onView(withText("GA NAAR INSTELLINGEN")).perform(click()); 

để

onView(withText("GA NAAR INSTELLINGEN"))) 
    .inRoot(isDialog()) // <--- 
    .check(matches(isDisplayed())) 
    .perform(click()); 

Nếu nó sẽ không làm việc, không bận tâm đến việc sử dụng lâu dài với Espresso một thử nghiệm thiết bị đo đạc lớn của Google gọi uiatomator.

Kiểm tra: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

Ví dụ mã:

// Initialize UiDevice instance 
UiDevice uiDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()); 

// Search for correct button in the dialog. 
UiObject button = uiDevice.findObject(new UiSelector().text("GA NAAR INSTELLINGEN")); 
if (button.exists() && button.isEnabled()) { 
    button.click(); 
} 

Hy vọng nó sẽ giúp

1

Bạn có thể cần phải đóng softkeyboard như thế này:

 onView(withId(R.id.username)) 
       .perform(typeText("username")) 
       .perform(closeSoftKeyboard()) 
     onView(withId(android.R.id.button1)).perform((click())) 

Như đã trình bày kỹ hơn bởi this answer.

2

Kể từ inRoot(isDialog()) dường như không làm việc cho DialogFragment tôi sử dụng workaround này cho đến nay:

enum class AlertDialogButton(@IdRes val resId: Int) { 
    POSITIVE(android.R.id.button1), 
    NEGATIVE(android.R.id.button2), 
    NEUTRAL(android.R.id.button3) 
} 

fun clickOnButtonInAlertDialog(button: AlertDialogButton) { 
    onView(withId(button.resId)).perform(click()) 
} 
+0

Cảm ơn bạn! isDialog() không hoạt động đối với tôi, nhưng tôi có thể tìm thấy button1 và button2. Cảm ơn bạn đã viết một cách rõ ràng cách các nút khác nhau xếp hàng với số ID của chúng. –

0

Những điều tốt về bản cập nhật cuối cùng là nó cho phép bạn thiết lập quyền trước mỗi bài kiểm tra, do đó bạn don' t phải nhấp vào hộp thoại (điều này tăng tốc độ thử nghiệm bởi rất nhiều!)

Sử dụng các quy tắc sau đây (ví dụ như đối với địa điểm/lưu trữ .. thay thế chúng với các điều khoản bạn cần rõ ràng)

@Rule 
    public GrantPermissionRule runtimePermissionRule = GrantPermissionRule.grant(
     ACCESS_FINE_LOCATION, READ_EXTERNAL_STORAGE); 
Các vấn đề liên quan