2016-02-26 105 views
1

我想要测试基于浓咖啡的快乐路径。我的流程是这样的: SplashActivity -> Activity_1 -> Activity_2 -> Activity_3 -> Activity_4. Activity_1中有一个按钮,如果用户还没有以其他方式登录Activity_3,则会将用户导向Activity_2浓缩咖啡,测试登录屏幕的快乐路径

我的测试通过,如果应用程序在这个方向去SplashActivity -> Activity_1 -> Activity_3 ->...。然而,我得到例外,android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching:当用户还没有登录,因此应用程序以这种方式SplashActivity -> Activity_1 -> Activity_2 ->...

这很明显,因为我的测试期望Activity_3,而Activity_2是可见的。

这是我的测试:

@RunWith(AndroidJUnit4.class) 
public class MinHappyPathTest 
{ 
    @Rule 
    public ActivityTestRule<Activity_1> mActivityTestRule = new ActivityTestRule<>(Activity_1.class); 

    private Activity_1 mActivity_1; 

    @Before 
    public void setup() 
    { 
     mActivity_1 = mActivityTestRule.getActivity(); 
    } 

    @Test 
    public void HappyPathMinimumTest() throws InterruptedException 
    {  
     // Wait to everything settles down (few animations there) 
     Thread.sleep(2000); 

     // On mActivity_1 press the button 
     onView(withId(R.id.btnNext)).perform(click()); 

     // On mActivity_3  onView(withId(R.id.editText)).perform(typeText(destination_short_name), ViewActions.closeSoftKeyboard()); 
     Thread.sleep(1000); // to results displays 
     onView(allOf(withId(R.id.recycler_view), isDisplayed())) 
       .perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); 

     // Other tests... 
    } 
} 

两个问题,我有:

  1. 如何把如果基于活动的声明Activity_1后是可见的?
  2. 根据我发现的与单元测试不同的是,您可以选择课程并对其进行测试,因此在Espresso中不可能做同样的事情。例如,我直接运行mActivity_4并进行测试,因为默认情况下应用启动mActivity_1显示,并且我得到NoMatchingViewException。我对吗?我实际上测试过,看起来像那样。

回答

0

我没有找到一个方式,我可以检查正在显示什么活动,所以我决定用if声明我正在使用的活动是这样的方式:

@Test 
public void HappyPathMinimumTest() throws InterruptedException 
{  
    // Wait to everything settles down (few animations there) 
    Thread.sleep(2000); 

    // On Activity_1 press the button 
    onView(withId(R.id.btnNext)).perform(click()); 

    // Display login screen if required (Activity_2) 
    if (myCondition) 
    { 
     // tests of Login activity... 
    } 

    // On mActivity_3  onView(withId(R.id.editText)).perform(typeText(destination_short_name), ViewActions.closeSoftKeyboard()); 
    Thread.sleep(1000); // to results displays 
    onView(allOf(withId(R.id.recycler_view), isDisplayed())) 
      .perform(RecyclerViewActions.actionOnItemAtPosition(2, click())); 

    // Other tests... 
} 
1

我想您正在接收NoMatchingViewException,因为您正在使用Thread.sleep。它不适用于Espresso。您应该使用IdlingResources在可以继续时通知Espresso。请参阅此处的IdlingResource实施示例 - http://droidtestlab.com/delay.html

+0

感谢您的信息。我之前读过关于islingResource的链接,感谢链接,但是'Thread.sleep()'对我很好。我有一个短信确认屏幕,等待30S通过短信接收代码。即使在这个屏幕上我的Thread.sleep(31000);效果很好。我在我的快乐道路上广泛使用了这种等待方法,并没有看到问题。再次感谢。 – Hesam