测试用例在Android的碎片 [英] Test case for fragment in android

查看:252
本文介绍了测试用例在Android的碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的申请,我有一个活动的多个片段。现在我想编写一个测试案例来检查这些碎片正常加载。首先,我通过一些触摸事件滚动到一个特定的片段,然后我试图获取该片段的名字。下面是我的code测试用例: -

In my application, I have multiple fragments on a single activity. Now I want to write a test case to check if these fragments are loading properly. To begin with, I passed some touch event to scroll to a particular fragment and then I am trying to fetch the name of this fragment. Below is my code for the test case:-

public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity> 
{
    MainActivity mMainActivity;
    ActionBar tactionbar;
    Fragment tFragment;


    public static final int TEST_POSITION = 2;
    private static String mSelection ;
    private int mPos = 0;




    public MainActivityTest()
    {
        super(MainActivity.class);
    }

    protected void setUp() throws Exception
    {
        super.setUp();
        mMainActivity = (MainActivity) getActivity();
        tactionbar = mfoneclay.getActionBar();
    }

    public void testPreConditions()
    {
        assertNotNull(mMainActivity);
        assertNotNull(tactionbar);
    }

    public void testFragmentUI()
     {
        mMainActivity.runOnUiThread(
                 new Runnable(){
                     public void run()
                     {  
                         mMainActivity.getCurrentFocus();

                     }
                 });

        for (int i = 1; i <= TEST_POSITION; i++) 
        {
          this.sendKeys(KeyEvent.KEYCODE_DPAD_RIGHT);
          mPos = tactionbar.getSelectedNavigationIndex();

        }

        this.sendKeys(KeyEvent.KEYCODE_DPAD_CENTER);

        mSelection = (String)tactionbar.getTabAt(mPos).getText();



        String resultText = "Exclusive";

        assertEquals(resultText,mSelection);

      }

}

在这里,独家是我的标签之一,而我要前往通过触摸事件的名称。现在,在运行测试的情况下,我可以看到它正确导航到独占的片段,但结果显示msection变量作为活动的名称,而不是片段名称的值。我在做什么错了?

Here, "Exclusive" is the name of one of my tab to which I am navigating to via the touch event. Now, while running the test case, I can see that it is properly navigating to the "Exclusive" fragment, but the result shows the value of the msection variable as the name of the activity and not the fragments name. What am I doing wrong?

推荐答案

得到了解决。它是如此愚蠢的我使用错误的组件来获取片段。事实证明,我必须使用ViewPager来获取片段。

Got the solution. It was so stupid of me to use the wrong components to fetch the fragment. It turns out that I have to use "ViewPager" to fetch the fragments.

这篇关于测试用例在Android的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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