bitrise-steplib / steps-virtual-device-testing-for-android

MIT License
22 stars 23 forks source link

[feature request] take screenshoot with the latest state before failing #43

Closed dabitdev closed 3 years ago

dabitdev commented 5 years ago

currently, bitrise shows a video in the instrumentationTests but it is very difficult to see what happened if you have plenty of tests. The espresso logs does not help, it will be awesome if it is possible to take a screenshot as soon as the test fails and it is presented in the web.

This is an example how to capture a screenshot. It will require to extract the screenshots from the sdcard once all tests are done.

public static void captureScreenshot(final String name)
   {
      final File sdCard = Environment.getExternalStorageDirectory();
      final File dir = new File(sdCard.getAbsolutePath() + "/FailedTestScreenshots");
      final String path = dir + "/" + name + ".png";
      final File imageFile;

// Get the current screen view
      final View screenView = _mainActivity.getWindow().getDecorView().getRootView();
      screenView.setDrawingCacheEnabled(true);

   // Create the bitmap which will be used for creating the screenshot image file.
      final Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
      screenView.setDrawingCacheEnabled(false);

// Activity needs to be present for the screenshot to be captured.
if (_mainActivity == null)
{
 CustomLogger.d("Activity quit before screenshot could be  taken.");
}

// Check if the directory is already present, if not, create it.
// Also add useful error messages for logging.
if (dir.isDirectory() || dir.mkdirs())
{
  imageFile = new File(path);

  // Java 8 style try with resources handling that auto close any streams for you after task is done.
  try (OutputStream out = new FileOutputStream(imageFile))
  {
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
    out.flush();
  }
  catch (final FileNotFoundException e)
  {
  Timber.d("Unable to find or create file for screenshot");
  }
  catch (final IOException e)
  {
  Timber.d("Unable to access I/O to create file for screenshot");
  }
}
else
  {
   Timber.d("Directory could not be created");
  }
}
fehersanyi-bitrise commented 5 years ago

Hello @dabitdev

these tests are running on firebase testlab, so this feature is out of our hands I guess, we send the artifact with the tests and get back the results.

dabitdev commented 5 years ago

it is optional to use firebase testlab. I currently use an emulator: NexusLowRes,24,en,portrait insrumentation

bitce commented 5 years ago

Hi @dabitdev, just to clarify this, the whole step is using the Firebase Test Lab solution, the device you listed is one of the devices you can select from their emulators.

ivanatworkjam commented 5 years ago

Hi, Shouldn't we be able to use this method to take screenshots?

bitce commented 4 years ago

Hi @ivanatworkjam!

You are absolutely able to use it 🙂

bitrise-coresteps-bot commented 3 years ago

Hello there, I'm a bot. On behalf of the community I thank you for opening this issue.

To help our human contributors focus on the most relevant reports, I check up on old issues to see if they're still relevant. This issue has had no activity for 90 days, so I marked it as stale.

The community would appreciate if you could check if the issue still persists. If it isn't, please close it. If the issue persists, and you'd like to remove the stale label, you simply need to leave a comment. Your comment can be as simple as "still important to me".

If no comment left within 21 days, this issue will be closed.

bitrise-coresteps-bot commented 3 years ago

I'll close this issue as it doesn't seem to be relevant anymore. We believe an old issue probably has a bunch of context that's no longer relevant, therefore, if the problem still persists, please open a new issue.