【问题标题】:Espresso Testing ListView items click action of childView (extends FrameLayout)Espresso 测试 ListView 项的 childView 的点击动作(扩展 FrameLayout)
【发布时间】:2019-02-06 02:51:26
【问题描述】:

我有一个活动,在这个活动中我有多个子视图:

public class CategoriesActivity extends AppCompatActivity {

    private StaggeredGridLayoutManager categoryGridLayoutManager;
    private DrawerLayout mDrawerLayout;
    private RecyclerView recyclerView;
    private NavigationView navigationView;
    private List<Category> categoryList;
    private Toolbar toolbar;
    private MaterialSearchView searchView;
    ...
 }

我有一个工具栏,在这个工具栏上有一个菜单按钮和一个搜索按钮。

主屏幕如下所示:

[初始屏幕] (https://imgur.com/bk0GXyOm.png)

当点击搜索按钮时,搜索视图会在现有列表视图的顶部打开,看起来像这样:

[搜索屏幕] (https://imgur.com/XefVGFwm.png)

我有兴趣测试搜索的功能,更准确地说,是用 Espresso 测试单击搜索视图的每一行的操作(例如,检查单击“Hello”,然后检查单击“Bonjour” , 等等)。

到目前为止我所拥有的是以下(获得建议的数量):

@RunWith(AndroidJUnit4.class)
public class CategoryActivityInstrumentedTests {
    @Rule
    public ActivityTestRule<CategoriesActivity> activityActivityTestRule = new ActivityTestRule<CategoriesActivity>(CategoriesActivity.class);

    @Before
    public void init(){
        //activityActivityTestRule.getActivity().getSupportFragmentManager().beginTransaction();
        Utils.level = 2;
    }

    @Test
    public void TestSuggestions(){
        onView(withId(R.id.action_search)).perform(click());
        onView(withId(R.id.search_view)).perform(new MyCustomViewAction());
    }

    public class MyCustomViewAction implements ViewAction {
        int size = 0;
        @Override
        public Matcher<View> getConstraints(){
            return isAssignableFrom(MaterialSearchView.class);
        }


        @Override
        public String getDescription(){
            return "whatever, size = " + size;
        }

        @Override
        public void perform(UiController uiController, View view){
            MaterialSearchView yourCustomView = (MaterialSearchView) view;
            size = yourCustomView.getSuggestionsCount();
            System.out.println(size);
            yourCustomView.mSuggestionsListView.performItemClick(yourCustomView.mSuggestionsListView.getAdapter().getView(1, null, null), 1, yourCustomView.mSuggestionsListView.getAdapter().getItemId(1));
                    //getItemAtPosition(0) toString(); // .performClick();
            Log.e(Constants.TAG_CategoriesActivity, " size = " + size);

        }

    }
}

问题是我无法在此活动中获取此列表(搜索视图包含建议列表),因此我无法对此视图的元素执行点击操作。

当我在 MyCustomViewAction 中执行单击时,它可以工作,但由于 viewGroup 未初始化,因此在我的主要活动中出现空指针异常。

这是我的搜索视图的代码:

public class MaterialSearchView extends FrameLayout implements Filter.FilterListener {

    //Views
    private View mSearchLayout;
    private View mTintView;
    public ListView mSuggestionsListView;
    private EditText mSearchSrcTextView;
    private ImageButton mBackBtn;
    private ImageButton mVoiceBtn;
    private ImageButton mEmptyBtn;
    private RelativeLayout mSearchTopBar;
    ...
}

有什么想法吗? 谢谢!


编辑

我在测试方法 TestSuggestions() 中尝试了以下调用:

onData(allOf(withText("Bonjour"))) // Find the row in the list .inAdapterView(withId(R.id.search_view)) // Find the list in the view hierarchy .perform(click());

我遇到了以下异常:

`I/TestRunner: android.support.test.espresso.PerformException: Error performing 'load adapter data' on view 'with id: com...:id/search_view'.`

【问题讨论】:

    标签: java android android-espresso android-testing


    【解决方案1】:

    我终于找到了解决方案(至少有2个解决方案):

    第一个解决方案:

        @Test
        public void TestSuggestions1(){
            for (int i=0 ; i<4081; i++) {
    
                onView(withId(R.id.action_search)).perform(click());    // opens the search
                Espresso.pressBack();   // performs the go back action to dismiss the keyboard
                onData(anything())      // selects the actual item at the desired position (which opens another activity)
                        .atPosition(i)
                        .perform(click());
                Espresso.pressBack();   // performs the go back action from the newer activity
                Log.e("TestSuggestions", " i = " + i);
            }
        }
    

    我一直到 4081 为止,因为这是我的建议数量。

    第二种解决方案:

        @Test
        public void TestSuggestions2() {
            for (i = 0; i < 4081; i++) {
                onView(withId(R.id.action_search)).perform(click());    // opens the search
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Espresso.pressBack();   // performs the go back action from the newer activity
                onView(withId(R.id.search_view)).perform(new MyCustomViewAction()); // selects the actual item at the desired position (which opens another activity)
                try {
                    Thread.sleep(250);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                Espresso.pressBack();
            }
        }
    

    使用以下 MyCustomViewAction :

        public class MyCustomViewAction implements ViewAction {
            int size = 0;
    
            @Override
            public Matcher<View> getConstraints(){
                return isAssignableFrom(MaterialSearchView.class);
            }
    
            @Override
            public String getDescription(){
                return "whatever, size = " + size;
            }
    
            @Override
            public void perform(UiController uiController, View view){
                MaterialSearchView yourCustomView = (MaterialSearchView) view;
                size = yourCustomView.getSuggestionsCount();
                yourCustomView.mSuggestionsListView.performItemClick(yourCustomView.mSuggestionsListView.getAdapter().getView(i, null, null), i, yourCustomView.mSuggestionsListView.getAdapter().getItemId(i));
                Log.e(Constants.TAG_CategoriesActivity, " size = " + size);
            }
        }
    

    其中 i 是我的 TestClass 中从 0 开始的全局变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多