简体   繁体   English

使用浓缩咖啡拍摄屏幕截图

[英]Taking Screen shot using espresso

I am using Espresso for my UI Testing in my project. 我在我的项目中使用Espresso进行UI测试。 I want to take screen shot of each Activity(Screen). 我想拍摄每个活动(屏幕)的屏幕截图。 I am using ScreenShooter from GoogleCloudTestLab for taking screen shot. 我正在使用GoogleCloudTestLab中的ScreenShooter进行截屏。

   ScreenShotter.takeScreenshot("main_screen_2", getActivity());

But it only taking the screen shot of the 1st activity which i defined in my ActivityTestRule. 但它只拍摄了我在ActivityTestRule中定义的第一个活动的屏幕截图。 How can I take other activity screen shot with in the same testcase. 如何在同一个测试用例中使用其他活动屏幕截图。

My understanding is ActivityTestRule is designed to test only one Activity within the testcase so getActivity() will only return the activity that you specified in the ActivityTestRule. 我的理解是ActivityTestRule旨在测试测试用例中的一个Activity,因此getActivity()只返回您在ActivityTestRule中指定的活动。

To capture the screenshot, the library currently uses: 要捕获屏幕截图,库目前使用:

View screenView = activity.getWindow().getDecorView().getRootView(); screenView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); screenView.setDrawingCacheEnabled(false);

(where activity is the activity the user passes us.) (活动是用户通过我们的活动。)

So because the same activity is being given to takescreenshot, we are only able to capture that activity's view hierarchy at that time. 因为同样的活动被赋予了截屏,我们当时只能捕获该活动的视图层次结构。 Would you be able to split up your tests to test only one activity per testcase? 您是否能够将测试分开以仅测试每个测试用例的一个活动?

Also, we are currently exploring other ways to capture the screen and will add to this thread if we change this method. 此外,我们目前正在探索捕获屏幕的其他方法,如果我们更改此方法,将添加到此线程。

Note: If you are using this library to run tests in Firebase Test Lab and you have a prefered way of capturing screenshots (instead of using the library), as long as they end up in the /sdcard/screenshots directory then they will be pulled and uploaded to the dashboard at the end of the test. 注意:如果您使用此库在Firebase测试实验室中运行测试,并且您有一种优先捕获屏幕截图(而不是使用库)的方法,只要它们最终位于/ sdcard / screenshots目录中,那么它们将被拉出并在测试结束时上传到仪表板。

I had the same issue since my tests cover flows which span multiple activities. 我有同样的问题,因为我的测试涵盖了跨越多个活动的流程。 A helper method such as this one can be used to get a reference to the currently active (on top) activity: 可以使用诸如此类的辅助方法来获取对当前活动(在顶部)活动的引用:

/**
 * A helper method to get the currently running activity under test when a test run spans across multiple
 * activities. The {@link android.support.test.rule.ActivityTestRule} only returns the initial activity that
 * was started.
 */
public static final Activity getCurrentActivity(Instrumentation instrumentation)
{
    final Activity[] currentActivity = new Activity[1];
    instrumentation.runOnMainSync(new Runnable()
    {
        public void run()
        {
            Collection<Activity> resumedActivities =
                ActivityLifecycleMonitorRegistry.getInstance().getActivitiesInStage(RESUMED);
            if (resumedActivities.iterator().hasNext())
            {
                currentActivity[0] = resumedActivities.iterator().next();
            }
        }
    });
    return currentActivity[0];
}

Pass it getInstrumentation() from within your test and you should be good to go. 从测试中传递getInstrumentation(),你应该好好去。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM