勺子和浓缩咖啡测试

Posted

技术标签:

【中文标题】勺子和浓缩咖啡测试【英文标题】:Spoon and Espresso testing 【发布时间】:2016-09-15 10:50:29 【问题描述】:

所以,我有一个贯穿整个应用程序的测试。现在,我想拍下一切。由于它有 2 个活动和许多片段,我无法让它工作,因为它只需要每个活动的第一个片段。

如何实现对每个片段进行拍照?

   @RunWith(androidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HearthBeatUITest 

    private final int MILISECONDS_TIMEOUT = 700;

    @Rule
    public ActivityTestRule<IntroActivity> mActivityRule = new ActivityTestRule<>(IntroActivity.class);
    @Rule
    public ActivityTestRule<LoginActivity> mLoginActivityRule = new ActivityTestRule<>(LoginActivity.class);

    @Test
    /**
     * Testing all the screens on the application if they are actually there
     */
    public void startTest() 
        Session.clear();
        Spoon.screenshot(mActivityRule.getActivity(), "initial_state");
        threadSleep(MILISECONDS_TIMEOUT);
        onView(withId(R.id.button_register)).check(matches(isDisplayed())).perform(click());
        threadSleep(MILISECONDS_TIMEOUT);
        Spoon.screenshot(mLoginActivityRule.getActivity(), "register_intro");
        onView(withId(R.id.register_with_email)).check(matches(isDisplayed())).perform(click());
        threadSleep(MILISECONDS_TIMEOUT);
        Spoon.screenshot(mLoginActivityRule.getActivity(), "register_detailed");
        onView(withId(R.id.image_left_button)).check(matches(isDisplayed())).perform(click());
        threadSleep(MILISECONDS_TIMEOUT);
        Spoon.screenshot(mLoginActivityRule.getActivity(), "register_intro");
        onView(withId(R.id.image_left_button)).check(matches(isDisplayed())).perform(click());
        threadSleep(MILISECONDS_TIMEOUT);
        Spoon.screenshot(mActivityRule.getActivity(), "initial_state");
        onView(withId(R.id.button_signin)).check(matches(isDisplayed())).perform(click());
        threadSleep(MILISECONDS_TIMEOUT);
        Spoon.screenshot(mLoginActivityRule.getActivity(), "login_intro");
        onView(withId(R.id.sign_in_emal)).check(matches(isDisplayed())).perform(click());
        threadSleep(MILISECONDS_TIMEOUT);
        Spoon.screenshot(mLoginActivityRule.getActivity(), "login_detailed");
    


【问题讨论】:

你的片段有动画吗?在开发设置中删除动画并关闭动画。 【参考方案1】:

首先,您只需要一条规则:

@RunWith(AndroidJUnit4.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HearthBeatUITest 

    private final int MILISECONDS_TIMEOUT = 300;
    @Rule
    public IntentsTestRule<IntroActivity> mActivityRule = new IntentsTestRule<>(IntroActivity.class);
    private Activity currentActivity;

现在,当您需要上下文来创建屏幕截图时,您将调用此方法:

   private Activity getActivityInstance() 
        getInstrumentation().runOnMainSync(new Runnable() 
            public void run() 
                Collection resumedActivities = ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
                if (resumedActivities.iterator().hasNext()) 
                    currentActivity = (Activity) resumedActivities.iterator().next();
                
            
        );

        return currentActivity;
    

它将获取您当前的活动,您将能够截取屏幕截图。仅供参考,您不能截取对话框。

【讨论】:

以上是关于勺子和浓缩咖啡测试的主要内容,如果未能解决你的问题,请参考以下文章

我想针对特定的构建类型运行单元测试和浓缩咖啡测试用例

带有“hasBackground”的浓缩咖啡测试

是否可以截屏并用浓缩咖啡测试像素值?

指纹读取器浓缩咖啡测试

java 浓缩咖啡进口。 UI测试

java 浓缩咖啡测试 - 等待一段时间