我在takeScreenShot()
中遇到错误,并且在执行命令fastlane Screengrab
时也出现了语言配置错误
这是我的测试执行代码
/**
* Instrumentation test, which will execute on an Android device.
*
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@ClassRule
public static final LocaleTestRule localeTestRule = new LocaleTestRule();
@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule<>(MainActivity.class);
@Test
public void testTakeScreenshot() {
Screengrab.screenshot("Dashboard");
}
@Test
public void useAppContext() {
}
}
终端错误:-
[11:09:17]:▸com.tcc.gstknowledge.ExampleInstrumentedTest: [11:09:17]:▸错误 testTakeScreenshot(com.tcc.gstknowledge.ExampleInstrumentedTest): [11:09:17]:▸java.lang.IllegalStateException:无法获取 视图上下文中的活动[11:09:17]:▸在 tools.fastlane.screengrab.DecorViewScreenshotStrategy $ ScreenshotViewAction.perform(DecorViewScreenshotStrategy.java:69) [11:09:17]:▸在 android.support.test.espresso.ViewInteraction $ SingleExecutionViewAction.perform(ViewInteraction.java:356) [11:09:17]:▸在 android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:248) [11:09:17]:▸在 android.support.test.espresso.ViewInteraction.access $ 100(ViewInteraction.java:63) [11:09:17]:▸在 android.support.test.espresso.ViewInteraction $ 1.call(ViewInteraction.java:153) [11:09:17]:▸在 android.support.test.espresso.ViewInteraction $ 1.call(ViewInteraction.java:150) [11:09:17]:▸在 java.util.concurrent.FutureTask.run(FutureTask.java:237)[11:09:17]:▸ 在android.os.Handler.handleCallback(Handler.java:751)[11:09:17]:▸ 在android.os.Handler.dispatchMessage(Handler.java:95)[11:09:17]:▸ 在android.os.Looper.loop(Looper.java:154)[11:09:17]:▸在 android.app.ActivityThread.main(ActivityThread.java:6776)[11:09:17]: ▸在java.lang.reflect.Method.invoke(本机方法)[11:09:17]:▸在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1518) [11:09:17]:▸在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) [11:09:19]:▸。 [11:09:19]:▸时间:5.788 [11:09:19]:▸有1 失败:[11:09:19]:▸1) testTakeScreenshot(com.tcc.gstknowledge.ExampleInstrumentedTest) [11:09:19]:▸java.lang.IllegalStateException:无法获取 视图上下文中的活动[11:09:19]:▸在 tools.fastlane.screengrab.DecorViewScreenshotStrategy $ ScreenshotViewAction.perform(DecorViewScreenshotStrategy.java:69) [11:09:19]:▸在 android.support.test.espresso.ViewInteraction $ SingleExecutionViewAction.perform(ViewInteraction.java:356) [11:09:19]:▸在 android.support.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:248) [11:09:19]:▸在 android.support.test.espresso.ViewInteraction.access $ 100(ViewInteraction.java:63) [11:09:19]:▸在 android.support.test.espresso.ViewInteraction $ 1.call(ViewInteraction.java:153) [11:09:19]:▸在 android.support.test.espresso.ViewInteraction $ 1.call(ViewInteraction.java:150) [11:09:19]:▸在 java.util.concurrent.FutureTask.run(FutureTask.java:237)[11:09:19]:▸ 在android.os.Handler.handleCallback(Handler.java:751)[11:09:19]:▸ 在android.os.Handler.dispatchMessage(Handler.java:95)[11:09:19]:▸ 在android.os.Looper.loop(Looper.java:154)[11:09:19]:▸在 android.app.ActivityThread.main(ActivityThread.java:6776)[11:09:19]: ▸在java.lang.reflect.Method.invoke(本机方法)[11:09:19]:▸在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1518) [11:09:19]:▸在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1408) [11:09:19]:▸失败!!! [11:09:19]:▸测试运行:2,失败:1
[!]在设备3300dea196566381上针对美国区域设置的测试失败。
答案 0 :(得分:2)
这可能会有所帮助:
从screengrab 0.5.0开始,您可以指定不同的策略来控制screengrab捕获屏幕截图的方式。较新的策略委托给UI Automator,与原始策略相比,它解决了许多问题:
- 已正确捕获Material UI的阴影/高程
- 正确捕获多窗口情况(对话框等)
- 在Android N上工作
但是,UI Automator需要具有API级别> = 18的设备,因此它不是默认策略。要在默认情况下为所有屏幕快照启用该功能,请在测试运行前进行以下调用:
Screengrab.setDefaultScreenshotStrategy(new UiAutomatorScreenshotStrategy());