Android屏幕截图的错误屏幕状态

Android屏幕截图的错误屏幕状态,android,user-interface,testing,screenshot,android-espresso,Android,User Interface,Testing,Screenshot,Android Espresso,我在UI测试中有一些步骤: rotateDevice(Orientation.LANDSCAPE); closeSoftKeyboard(); assertActionBarTitle(getString(R.string.demo_activity_title)); assertButtonsAreDisplayedInScrollView(mExpectedDisplayedButtonIds); saveScreenshot(); 当我运行测试时,我可以在屏幕上看到操作,当AssertB

我在UI测试中有一些步骤:

rotateDevice(Orientation.LANDSCAPE);
closeSoftKeyboard();
assertActionBarTitle(getString(R.string.demo_activity_title));
assertButtonsAreDisplayedInScrollView(mExpectedDisplayedButtonIds);
saveScreenshot();
当我运行测试时,我可以在屏幕上看到操作,当AssertButtonsAredisplayedScrollView方法启动时:显示滚动到底部直到最后一个按钮,确保在调用saveScreenshot方法后显示最低的按钮

但是当我打开screenshot.png时,我看到它已经拍摄了初始屏幕状态,即使用了上面的按钮(就像它没有滚动到底部一样)

有人能帮忙解决这个问题吗?我必须截屏屏幕的最后状态

方法主体的屏幕截图:

Bitmap bitmap = InstrumentationRegistry.getInstrumentation().getUiAutomation().takeScreenshot();

File screenshotFile = getScreenshotFile();

try (FileOutputStream outputStream = new FileOutputStream(screenshotFile)) {
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
    Log.i(mTag, "Saved screenshot: " + screenshotFile.getAbsolutePath());
} catch (IOException e) {
    Log.e(mTag, "Could not save screenshot", e);
}

谢谢

尝试在截图时添加延迟

Handler handler = new Handler();
handler.postDelayed(new Runnable() {
      @Override
      public void run() {
        //Do something after 100ms
        saveScreenshot();
      }
}, 100);

可能会发生这样的事情,100毫秒后将开始另一次测试。当另一个测试运行时,将拍摄屏幕截图。为什么是100毫秒?在一些旧设备上,测试速度可能较慢,需要超过100毫秒。在测试中添加显式等待不是一个好的实践。我仍然不明白为什么我可以在ui测试的最后一个语句之后看到屏幕处于不同的状态,但在屏幕截图上看到的是不同的状态。首先尝试检查在添加延迟后,您是否可以在哪里获得正确的屏幕截图。然后,只有您才会知道问题是由于延迟还是其他问题。我尝试在AssertButtonSaRedisplayedScrollView之后和截图之前添加延迟。屏幕截图是正确的。在UI更新之前,您正在拍摄屏幕截图,因此您需要添加几毫秒的延迟,然后拍摄屏幕截图。然后只有你才能在屏幕截图中看到更新视图。延迟或睡眠是实现这一点的唯一方法吗?