Robotium (4.1) 中似乎存在一个已知问题,已在此处报告。
在正式修复之前,我想贡献我的个人技巧,即使 JUnit 通过命令行启动也对我有用。
在执行各种测试期间以及在 DearDown() 中,我将其称为 JUnit;
public void takeScreenshot(final String filename) {
//hack -to ensure that the current view has been fully loaded
while(view.equals(null)) {
m_solo.sleep(500);
}
View view = View view = m_solo.getCurrentViews().get(0).getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
File directory = new File("/mnt/sdcard/Robotium-Screenshots/");
directory.mkdirs();
if (bitmap != null) {
try {
File outputFile = new File(directory, filename + ".jpg");
FileOutputStream ostream = new FileOutputStream(outputFile);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
logError(e.getMessage());
}
}
}