Skip to content

Commit

Permalink
Update exec-out screenshot
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Oct 3, 2017
1 parent 36e10e0 commit 753f68b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
14 changes: 11 additions & 3 deletions lib/commands/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,19 @@ helpers.getScreenshotDataWithAdbShell = async function (adb, opts) {
};

helpers.getScreenshotDataWithAdbExecOut = async function (adb) {
let {stdout} = await exec(adb.executable.path, ['exec-out', '/system/bin/screencap -p'],
{encoding: 'binary', isBuffer: true});
let {stdout, stderr, code} = await exec(adb.executable.path,
adb.executable.defaultArgs
.concat(['exec-out', '/system/bin/screencap', '-p']),
{encoding: 'binary', isBuffer: true});
// if there is an error, throw
if (code || stderr.length) {
throw new Error(`Screenshot returned error, code: '${code}', stderr: '${stderr.toString()}'`);
}
// if we don't get anything at all, throw
if (!stdout.length) {
throw new Error('The size of the taken screenshot equals to zero.');
throw new Error('Screenshot returned no data');
}

return await jimp.read(stdout);
};

Expand Down
24 changes: 19 additions & 5 deletions test/unit/commands/actions-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,18 +456,32 @@ describe('Actions', () => {
it('should be able to take screenshot via exec-out', async () => {
sandbox.stub(teen_process, 'exec');
sandbox.stub(jimp, 'read');
teen_process.exec.returns({stdout: 'stdout'});
teen_process.exec.returns({stdout: 'stdout', stderr: ''});
driver.adb.executable.path = 'path/to/adb';
await helpers.getScreenshotDataWithAdbExecOut(driver.adb);
teen_process.exec.calledWithExactly('path/to/adb', ['exec-out', '/system/bin/screencap -p']
, {encoding: 'binary', isBuffer: true}).should.be.true;
teen_process.exec.calledWithExactly(driver.adb.executable.path,
driver.adb.executable.defaultArgs
.concat(['exec-out', '/system/bin/screencap', '-p']),
{encoding: 'binary', isBuffer: true}).should.be.true;
jimp.read.calledWithExactly('stdout').should.be.true;
});
it('should throw error if size of the screenshot is zero', async () => {
sandbox.stub(teen_process, 'exec');
teen_process.exec.returns({stdout: ''});
teen_process.exec.returns({stdout: '', stderr: ''});
await helpers.getScreenshotDataWithAdbExecOut(driver.adb)
.should.be.rejectedWith('screenshot equals to zero');
.should.be.rejectedWith('Screenshot returned no data');
});
it('should throw error if code is not 0', async () => {
sandbox.stub(teen_process, 'exec');
teen_process.exec.returns({code: 1, stdout: '', stderr: ''});
await helpers.getScreenshotDataWithAdbExecOut(driver.adb)
.should.be.rejectedWith(`Screenshot returned error, code: '1', stderr: ''`);
});
it('should throw error if stderr is not empty', async () => {
sandbox.stub(teen_process, 'exec');
teen_process.exec.returns({code: 0, stdout: '', stderr: 'Oops'});
await helpers.getScreenshotDataWithAdbExecOut(driver.adb)
.should.be.rejectedWith(`Screenshot returned error, code: '0', stderr: 'Oops'`);
});
});
describe('getScreenshot', () => {
Expand Down

0 comments on commit 753f68b

Please sign in to comment.