Moved screenshot functions into Device's class.

Summary:
I have moved the screenshot functions from ScreenCaptureButtons to the Device classes. I have slightly rewritten them so that they return a Promise which resolves to a Buffer. The Buffer can then be saved to a file or converted to a data Blob.

I have removed streaming and simply loaded the image into memory. Once the image is in memory it can be manipulated for various tasks i.e. written to a file, or displayed in the app.

iOS screenshots had to be rewritten. I now save the image to a temp folder, load it into the apps memory, and then remove the temp image.

Reviewed By: jknoxville

Differential Revision: D16939901

fbshipit-source-id: 3e39a5aeda8d48829ac5a8ff912a98f110341c07
This commit is contained in:
Benjamin Elo
2019-08-22 05:26:46 -07:00
committed by Facebook Github Bot
parent 263b47f82f
commit 7def9bb681
6 changed files with 81 additions and 46 deletions

View File

@@ -10,6 +10,12 @@ import child_process from 'child_process';
import BaseDevice from './BaseDevice';
import JSONStream from 'JSONStream';
import {Transform} from 'stream';
import electron from 'electron';
import fs from 'fs';
import uuid from 'uuid/v1';
import path from 'path';
import {promisify} from 'util';
import {exec} from 'child_process';
type IOSLogLevel = 'Default' | 'Info' | 'Debug' | 'Error' | 'Fault';
@@ -42,6 +48,18 @@ export default class IOSDevice extends BaseDevice {
this.log = this.startLogListener();
}
screenshot(): Promise<Buffer> {
const tmpImageName = uuid() + '.png';
const tmpDirectory = (electron.app || electron.remote.app).getPath('temp');
const tmpFilePath = path.join(tmpDirectory, tmpImageName);
const command = `xcrun simctl io booted screenshot ${tmpFilePath}`;
return promisify(exec)(command)
.then(() => promisify(fs.readFile)(tmpFilePath))
.then(buffer => {
return promisify(fs.unlink)(tmpFilePath).then(() => buffer);
});
}
teardown() {
if (this.log) {
this.log.kill();