Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android java.lang.RuntimeException:使用spoon时无法捕获屏幕截图_Android_Spoon - Fatal编程技术网

Android java.lang.RuntimeException:使用spoon时无法捕获屏幕截图

Android java.lang.RuntimeException:使用spoon时无法捕获屏幕截图,android,spoon,Android,Spoon,我试图在JUnit esspresso测试期间捕获一个屏幕截图。Android之前没有重新确认活动,所以我添加了私人活动。这使代码能够运行。但是,现在当它尝试截图时,我得到了以下堆栈跟踪: java.lang.RuntimeException: Unable to capture screenshot. at com.squareup.spoon.Spoon.screenshot(Spoon.java:87) at com.squareup.spoon.Spoon.screenshot(Spoo

我试图在JUnit esspresso测试期间捕获一个屏幕截图。Android之前没有重新确认活动,所以我添加了
私人活动
。这使代码能够运行。但是,现在当它尝试截图时,我得到了以下堆栈跟踪:

java.lang.RuntimeException: Unable to capture screenshot.
at com.squareup.spoon.Spoon.screenshot(Spoon.java:87)
at com.squareup.spoon.Spoon.screenshot(Spoon.java:60)
at com.lifehealth.irma.test.Screenshotscripts.screencap2.Screencap1(screencap2.java:122)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:270)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1853)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.app.Activity.getApplicationContext()' on a null object reference
at com.squareup.spoon.Spoon.screenshot(Spoon.java:78)
... 33 more
以下是我的代码:

import android.app.Activity;
import android.support.test.espresso.action.ViewActions;
import android.support.test.espresso.matcher.ViewMatchers;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import com.lifehealth.MainActivity;
import com.lifehealth.irma.R;
import com.squareup.spoon.Spoon;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.doubleClick;
import static android.support.test.espresso.action.ViewActions.swipeUp;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;


//hamcrest

/**
 *
 * Basic tests showcasing simple view matchers and actions like {@link ViewMatchers#withId},
 * {@link ViewActions#click} and {@link ViewActions#typeText}.
 * <p>
 * Note that there is no need to tell Espresso that a view is in a different {@link Activity}.
 */

@RunWith(AndroidJUnit4.class)
@LargeTest
// Name of the activity + Test
public class screencap2  {
    //Strings to be typed into tests declaration
    public static final String STRING_TO_BE_TYPED = "Test1233";
    public static final String loginID = "LIFEHEALTH";
    public static final String Device = "TestDevice";




        /**
         * A JUnit {@link Rule @Rule} to launch your activity under test. This is a replacement
         * for {@link ActivityInstrumentationTestCase2}.
         * <p>
         * Rules are interceptors which are executed for each test method and will run before
         * any of your setup code in the {@link Before @Before} method.
         * <p>
         * {@link ActivityTestRule} will create and launch of the activity for you and also expose
         * the activity under test. To get a reference to the activity you can use
         * the {@link ActivityTestRule#getActivity()} method.
         */
        //Rule that tells the system which screen to start/boot up on this is telling the device to start from the main menu screen.
        @Rule
        public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(
                MainActivity.class);
    private Activity activity;


    //@Test must be there to start a test if this is missing it will not run the test
        @Test
        public void Screencap1  () { /* passes*/
            //Enters login
            onView(withId(R.id.editOid)) /*editTextUserInput*/
                    .perform(typeText(loginID), closeSoftKeyboard());
            // then click next
            onView(withId(R.id.btn_next)).perform(click());
            // "Clicks the settings
            onView(withId(R.id.btnSettings)).perform(click());
            //Clicks Device Settings.
            onView(withText("Device Settings")).perform(click());
            //Scroll up
            onView(withText("Configure IRMA Base")).perform(swipeUp());
            onView(withText("Configure IRMA Base")).perform(swipeUp());
            onView(withText("Configure IRMA Base")).perform(swipeUp());
            onView(withText("Configure IRMA Base")).perform(swipeUp());
            onView(withText("Configure IRMA Base")).perform(swipeUp());
            onView(withText("Configure IRMA Base")).perform(swipeUp());
            onView(withText("Barcode Reader Timeout")).perform(swipeUp());
            onView(withText("Barcode Reader Timeout")).perform(swipeUp());
            onView(withText("Barcode Reader Timeout")).perform(swipeUp());
            //language
            onView(withText("Language")).perform(doubleClick());
            Spoon.screenshot(activity, "state_changed");


            //wait 1 min
            try {
                Thread.sleep(1 * 60 * 1000);
            } catch (InterruptedException e) {

            }
        /* Clicks back button */
            onView(withId(R.id.btn_back)).perform(click());
            //clicks main menu button
            onView(withText("Main Menu")).perform(click());

        }
}
导入android.app.Activity;
导入android.support.test.espresso.action.ViewActions;
导入android.support.test.espresso.matcher.ViewMatchers;
导入android.support.test.rule.ActivityTestRule;
导入android.support.test.runner.AndroidJUnit4;
导入android.test.ActivityInstrumentationTestCase2;
导入android.test.suitebuilder.annotation.LargeTest;
导入com.lifehealth.main活动;
导入com.lifehealth.irma.R;
导入com.squareup.spoon.spoon;
导入org.junit.Before;
导入org.junit.Rule;
导入org.junit.Test;
导入org.junit.runner.RunWith;
导入静态android.support.test.espresso.espresso.onView;
导入静态android.support.test.espresso.action.ViewActions.click;
导入静态android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
导入静态android.support.test.espresso.action.ViewActions.doubleClick;
导入静态android.support.test.espresso.action.ViewActions.swipeUp;
导入静态android.support.test.espresso.action.ViewActions.typeText;
导入静态android.support.test.espresso.matcher.ViewMatchers.withId;
导入静态android.support.test.espresso.matcher.ViewMatchers.withText;
//汉克雷斯特
/**
*
*基本测试展示了简单的视图匹配器和动作,如{@link ViewMatchers#withId},
*{@link ViewActions#click}和{@link ViewActions#typeText}。
*
*注意,不需要告诉Espresso视图位于不同的{@link Activity}。
*/
@RunWith(AndroidJUnit4.class)
@最大的
//活动+测试的名称
公共类屏幕CAP2{
//要在测试声明中键入的字符串
public static final String-TO-BE-type=“Test1233”;
公共静态最终字符串loginID=“LIFEHEALTH”;
公共静态最终字符串Device=“TestDevice”;
/**
*一个JUnit{@link Rule@Rule}来启动测试中的活动。这是一个替换
*对于{@link ActivityInstrumentationTestCase2}。
*
*规则是为每个测试方法执行的拦截器,将在测试之前运行
*{@link Before@Before}方法中的任何设置代码。
*
*{@link ActivityTestRule}将为您创建和启动活动,并公开
*正在测试的活动。若要获取对该活动的引用,可以使用
*{@link ActivityTestRule#getActivity()}方法。
*/
//告诉系统在哪个屏幕上启动/引导的规则是告诉设备从主菜单屏幕启动。
@统治
公共活动测试规则mActivityRule=新活动测试规则(
主要活动(课堂);
私人活动;
//@测试必须在那里才能启动测试。如果缺少该测试,则不会运行该测试
@试验
公共无效屏幕CAP1(){/*通过*/
//输入登录名
onView(带id(R.id.editOid))/*editTextUserInput*/
.执行(键入文本(loginID),关闭软键盘());
//然后单击下一步
onView(带id(R.id.btn_next)).perform(单击());
//“单击设置
onView(带id(R.id.btnSettings)).perform(单击());
//单击设备设置。
onView(使用文本(“设备设置”))。执行(单击();
//向上滚动
onView(使用文本(“配置IRMA基”))。执行(swipeUp());
onView(使用文本(“配置IRMA基”))。执行(swipeUp());
onView(使用文本(“配置IRMA基”))。执行(swipeUp());
onView(使用文本(“配置IRMA基”))。执行(swipeUp());
onView(使用文本(“配置IRMA基”))。执行(swipeUp());
onView(使用文本(“配置IRMA基”))。执行(swipeUp());
onView(带文本(“条形码读取器超时”)。执行(swipeUp());
onView(带文本(“条形码读取器超时”)。执行(swipeUp());
onView(带文本(“条形码读取器超时”)。执行(swipeUp());
//语言
onView(使用文本(“语言”))。执行(双击();
屏幕截图(活动,“状态改变”);
//等待1分钟
试一试{
线程睡眠(1*60*1000);
}捕捉(中断异常e){
}
/*单击后退按钮*/
onView(带id(R.id.btn_back))。执行(单击());
//单击主菜单按钮
打开查看(使用文本(“主菜单”)。执行(单击());
}
}

您从不初始化
活动
。使用现有的
活动测试规则
,您可以访问
mActivityRule.getActivity()

请帮助!!!!这很有效,但我只能抓取一个屏幕。你知道我如何才能抓拍我导航的屏幕吗?@fivestargamer不直接使用Spoon-这是。你可以使用来捕获屏幕并
Spoon.save()
将其附加到测试结果中。
Spoon.screenshot(activity, "state_changed");