How to catch a View with Tag by Espresso in Android?

前端 未结 2 1160
自闭症患者
自闭症患者 2021-01-18 01:34

I have a PinCodeView that extends LinearLayout. I have following code in my init() method. DigitEditText extends Ed

2条回答
  •  粉色の甜心
    2021-01-18 02:13

    I solved my problem with this trick. Hope it saves some times for you.

    First I used Id rather than tag.

    for (int i = 0; i < 4; i++)
        {
            DigitEditText digitView = getDigitInput();
            digitView.setId(R.id.etPinCodeView + i); // uses for Espresso testing
            digitView.setKeyEventCallback(this);
            ...
    

    And this is test for it:

    onView(withId(R.id.etPinCodeView + 0)).perform(typeText("2"));
    onView(withId(R.id.etPinCodeView + 1)).perform(typeText("0"));
    onView(withId(R.id.etPinCodeView + 2)).perform(typeText("1"));
    onView(withId(R.id.etPinCodeView + 3)).perform(typeText("6"));
    

提交回复
热议问题