Android Espresso测试卡在perform(click()); [英] Android Espresso Test gets stuck at perform(click());

查看:213
本文介绍了Android Espresso测试卡在perform(click());的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一些自动测试用例中使用Espresso.情况是:

I am using Espresso for some automated test cases. The scenario is:

我有一个活动,其中包含两个片段,分别是FrgA和FrgB.

I have an activity which holds two fragments say FrgA and FrgB.

FrgA包含一个列表,其中包含一些项目,单击这些项目会将用户带到FrgB.

FrgA consists of a list with some items which takes the user to the FrgB on click of them.

一旦显示FrgB,我将检查FrgB上是否存在gridview.我为此使用的代码是:

Once FrgB is displayed, I check for existence of an gridview on FrgB. The code I use for this is:

@Test
public void testProductsDisplayed(){
    onData(anything()).atPosition(1).perform(click());
    onView(withId(R.id.gridview)).check(matches(isDisplayed()));
}

我一直面临的问题是,FrgA的列表项被成功单击,它也将我带到具有GridView的FrgB.但是我的测试不会继续进行到测试用例的第2行,并停留在第一行(click()).一段时间后,它超时了.奇怪的是,如果我删除gridview并放置一些TextView并搜索它的存在,则测试通过且没有任何错误.

The problem I have been facing is, the list item from FrgA gets clicked successfully and it also takes me to the FrgB which has the GridView. But my test won't proceed to the line 2 of the test case and gets stucked on the first line (click()). After some time it gets timed out. Strange thing is, if I remove the gridview and place some TextView and search for it's existence, the test passes without any error.

我唯一想到的是在FrgB中加载gridview内容时的一些时序问题. FrgB的代码如下:

The only thing I can think of is some timing issue while it is loading the gridview contents in FrgB. The code for FrgB is as follows:

public class FrgB extends BaseFragment implements OnItemClickListener {
private View mView;
private List<Product> mProducts;
private ImagesAdapter mAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    Log.d("Kart", "In onActivityCreated");

    GridView gvProducts = findTypedViewById(mView, R.id.gvProducts);
    mAdapter = new ImagesAdapter(getActivity(), R.layout.image_grid_item, new ArrayList<Product>());

    if(getArguments() != null && getArguments().containsKey(Constants.CATEGORY_ID)){
        String catId = getArguments().getString(Constants.CATEGORY_ID);
        Log.d("Kart", "In onActivityCreated - activity is: "+getActivity());
        if(getActivity() == null){
            Log.e("Kart", "Activity does not exist");
        }else{
            mProducts = ((ScrLanding)getActivity()).getProductsForCategory(catId);
            mAdapter.addAll(mProducts);
        }

        gvProducts.setAdapter(mAdapter);
        gvProducts.setOnItemClickListener(this);

    } else{
        returnToLastScreen();
    }
}
/**
 * Method to remove current fragment from fragment manager
 */
private void returnToLastScreen() {
    UiHelper.showToast(getString(R.string.msg_no_products), Toast.LENGTH_SHORT);
    removeFragment(this);       
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    mView = inflater.inflate(R.layout.frg_product_listing, container, false);
    return mView;
}


@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
        long id) {
    switch (parent.getId()) {
    case R.id.gvProducts:
        FrgProductDetails fragment = new FrgProductDetails();
        Bundle bundle = new Bundle();
        bundle.putSerializable(Constants.PRODUCT_INFO, mProducts.get(position));
        ((ScrLanding)getActivity()).setSelectedProductBitmap(mAdapter.getThumbsMap().get(position));
        fragment.setArguments(bundle);
        addFragment(fragment, R.id.fragment_container);
        break;

    default:
        break;
    }
}

}

我要去哪里错了?

推荐答案

在使用ListView时,我遇到了同样的问题.

I had the same issue while I was working with a ListView.

有趣的是,我注意到,如果我手动尝试滚动屏幕​​,测试将重新开始. 添加此行即可解决问题,然后恢复测试.

Interestingly, I noticed that the test would resume if I manually tried to scroll the screen. Adding this line solved the issue and resumed the test.

 onView(withId(R.id.myListView)).perform(swipeUp());

这篇关于Android Espresso测试卡在perform(click());的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆