6

दबाकर एस्प्रेसो का उपयोग कैसे करें मैं एस्प्रेसो का उपयोग करके नीचे बटन दबा देना चाहता हूं, लेकिन मुझे यकीन नहीं है कि कैसे। क्या मुझे संसाधन-आईडी मिलनी चाहिए? या 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: पदानुक्रम में कोई देखे जाने मिलान मिला: पाठ के साथ: "जीए Naar INSTELLINGEN"

+0

मुझे लगता है कि आप ID – jeprubio

+0

ऑन व्यू (withText ("GA NAAR INSTELLINGEN") से मिलान करने के बजाय withText() विधि का उपयोग कर सकते हैं। (प्रदर्शन()) पर क्लिक करें; काम नहीं करता –

उत्तर

11
StackOverflow समान मुद्दे के अनुसार

है: Check if a dialog is displayed with Espresso

आपको अपना कोड बदलना चाहिए:

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

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

को यह काम नहीं करेगा, तो Espresso के साथ एक और महान गूगल के उपकरण परीक्षण uiatomator कहा जाता है लंबे समय तक उपयोग करने के लिए परेशान नहीं है।

की जांच: http://qathread.blogspot.com/2015/05/espresso-uiautomator-perfect-tandem.html

उदाहरण कोड:

// 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(); 
} 

आशा है कि यह मदद मिलेगी

1

आप इस तरह softkeyboard बंद करने के लिए आवश्यकता हो सकती है: जैसा कि अधिक अच्छी तरह से विस्तृत

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

this answer द्वारा।

2

inRoot(isDialog()) के बाद से DialogFragment मैं इस समाधान का अब तक का उपयोग करने के लिए काम करने के लिए प्रतीत नहीं होता:

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

धन्यवाद! isDialog() मेरे लिए काम नहीं कर रहा था, लेकिन मैं बटन 1 और बटन 2 खोजने में सक्षम था। स्पष्ट रूप से यह लिखने के लिए धन्यवाद कि अलग-अलग बटन उनके आईडी नंबरों के साथ कैसे जुड़ते हैं। –

0

पिछले अद्यतन के बारे में अच्छी बात यह आप, प्रत्येक परीक्षा से पहले अनुमति सेट करने के लिए इतना है कि आप डॉन की अनुमति देता है ' टी संवाद क्लिक करना (यह एक बहुत से परीक्षण को गति!)

निम्नलिखित नियम का उपयोग करें (स्थान/भंडारण के लिए उदाहरण के लिए .. अनुमतियाँ आपको स्पष्ट रूप से जरूरत के साथ इन स्थानापन्न)

@Rule 
    public GrantPermissionRule runtimePermissionRule = GrantPermissionRule.grant(
     ACCESS_FINE_LOCATION, READ_EXTERNAL_STORAGE);