-2

I am trying to take a screenshot while doing an esspresso test here is my code and i cannot resolve the method getActivity and i have tried using this and that did not work either.

package com.lifehealth.irma.test.Screenshotscripts;
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 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

@RunWith(AndroidJUnit4.class)
@LargeTest

// Name of the activity + Test
public class Screencap {
    //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";

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

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

this is where the issue is

        HelperClass.takeScreenshot("Whatever you want to call the file", getActivity());


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

    }

}
Abubakar
  • 374
  • 3
  • 16

2 Answers2

2

getActivity used for Fragment. You should use getApplicationContext() or YourClassName.this in this case.

John Joe
  • 12,412
  • 16
  • 70
  • 135
0

The getActivity() method gives the context of the Activity. You can use YourActivityName.this instead of it.

getActivity() where it is defined?

Hope this will help you.

Community
  • 1
  • 1