我正在尝试在进行esspresso测试时截取屏幕截图 这是我的代码,我无法解决方法getActivity,我尝试使用这个,但也没有用。
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());
这就是问题所在
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());
}
}
答案 0 :(得分:1)
getActivity
用于Fragment
。在这种情况下,您应该使用getApplicationContext()
或YourClassName.this
。
答案 1 :(得分:0)
getActivity()方法提供Activity的上下文。您可以使用YourActivityName.this代替它。
getActivity() where it is defined?
希望这会对你有所帮助。