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

时间:2017-05-09 18:39:48

标签: android spoon

我正在尝试在JUnit esspresso测试期间捕获屏幕截图。 Android之前没有重新调整活动,所以我添加了private Activity activity。这使得代码可以运行。但是,现在我尝试截取屏幕截图时会得到以下堆栈跟踪:

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());

        }
}

1 个答案:

答案 0 :(得分:0)

Spoon.screenshot(activity, "state_changed");

您永远不会初始化activity。使用现有的ActivityTestRule,您可以访问mActivityRule.getActivity()