Skip to content

Commit

Permalink
USe base driver logging methods
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Feb 23, 2018
1 parent bbb7567 commit 488cd02
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 62 deletions.
38 changes: 0 additions & 38 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,10 @@ import { util } from 'appium-support';
import B from 'bluebird';
import log from '../logger';
import { NATIVE_WIN } from '../webview-helpers';
import os from 'os';


let commands = {}, helpers = {}, extensions = {};

const SUPPORTED_LOG_TYPES = {
logcat: {
description: 'Logs for Android applications on real device and emulators via ADB',
getter: async (self) => await self.adb.getLogcatLogs(),
},
bugreport: {
description: `'adb buregport' output for advanced issues diagnostic`,
getter: async (self) => (await self.adb.bugreport()).split(os.EOL),
},
server: {
description: 'Appium server logs',
getter: (self) => {
if (!self.relaxedSecurityEnabled) {
throw new Error('Appium server must have relaxed security flag set ' +
'in order to be able to get server logs');
}
return log.record;
},
},
};

commands.keys = async function (keys) {
// Protocol sends an array; rethink approach
keys = _.isArray(keys) ? keys.join('') : keys;
Expand Down Expand Up @@ -112,22 +90,6 @@ commands.getCurrentPackage = async function () {
return (await this.adb.getFocusedPackageAndActivity()).appPackage;
};

commands.getLogTypes = function () {
return _.keys(SUPPORTED_LOG_TYPES);
};

commands.getLog = async function (logType) {
if (!_.has(SUPPORTED_LOG_TYPES, logType)) {
const typesWithDescriptions = {};
for (const type of _.keys(SUPPORTED_LOG_TYPES)) {
typesWithDescriptions[type] = SUPPORTED_LOG_TYPES[type].description;
}
throw new Error(`Unsupported log type '${logType}'. ` +
`Supported types are ${JSON.stringify(typesWithDescriptions)}`);
}
return await SUPPORTED_LOG_TYPES[logType].getter(this);
};

commands.background = async function (seconds) {
if (seconds < 0) {
// if user passes in a negative seconds value, interpret that as the instruction
Expand Down
2 changes: 2 additions & 0 deletions lib/commands/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import performanceCmds from './performance';
import executeCmds from "./execute";
import shellCmds from "./shell";
import appManagementCmds from './app-management';
import logCmds from './log';


let commands = {};
Expand All @@ -33,6 +34,7 @@ Object.assign(
executeCmds,
shellCmds,
appManagementCmds,
logCmds,
// add other command types here
);

Expand Down
30 changes: 30 additions & 0 deletions lib/commands/log.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import log from '../logger';
import os from 'os';


let commands = {}, helpers = {}, extensions = {};

extensions.supportedLogTypes = {
logcat: {
description: 'Logs for Android applications on real device and emulators via ADB',
getter: async (self) => await self.adb.getLogcatLogs(),
},
bugreport: {
description: `'adb bugreport' output for advanced issues diagnostic`,
getter: async (self) => (await self.adb.bugreport()).split(os.EOL),
},
server: {
description: 'Appium server logs',
getter: (self) => {
if (!self.relaxedSecurityEnabled) {
throw new Error('Appium server must have relaxed security flag set ' +
'in order to be able to get server logs');
}
return log.record;
},
},
};

Object.assign(extensions, commands, helpers);
export { commands, helpers };
export default extensions;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"appium-adb": "^6.3.0",
"appium-android-bootstrap": "^2.7.5",
"appium-android-ime": "^2.0.0",
"appium-base-driver": "^2.0.0",
"appium-base-driver": "^2.23.0",
"appium-chromedriver": "^3.0.0",
"appium-support": "^2.13.0",
"appium-unlock": "^2.0.0",
Expand Down
24 changes: 1 addition & 23 deletions test/unit/commands/general-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { fs } from 'appium-support';
import Bootstrap from 'appium-android-bootstrap';
import B from 'bluebird';
import ADB from 'appium-adb';
import os from 'os';


chai.should();
chai.use(chaiAsPromised);
Expand Down Expand Up @@ -146,28 +146,6 @@ describe('General', function () {
await driver.getCurrentPackage().should.eventually.equal('pkg');
});
});
describe('getLogTypes', function () {
it('should get log types', async function () {
const types = await driver.getLogTypes();
for (const type of ['logcat', 'bugreport', 'server']) {
types.should.include(type);
}
});
});
describe('getLog', function () {
it('should get logcat logs', async function () {
sandbox.stub(driver.adb, 'getLogcatLogs').returns(['logs']);
(await driver.getLog('logcat')).should.be.deep.equal(['logs']);
});
it('should get bugreport logs', async function () {
sandbox.stub(driver.adb, 'bugreport').returns(`line1${os.EOL}line2`);
(await driver.getLog('bugreport')).should.be.deep.equal(['line1', 'line2']);
});
it('should throws exception if log type is unsupported', async function () {
await driver.getLog('unsupported_type').should.eventually
.be.rejectedWith(/Unsupported log type/);
});
});
describe('isAppInstalled', function () {
it('should return true if app is installed', async function () {
sandbox.stub(driver.adb, 'isAppInstalled').withArgs('pkg').returns(true);
Expand Down
38 changes: 38 additions & 0 deletions test/unit/commands/log-specs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import chai from 'chai';
import chaiAsPromised from 'chai-as-promised';
import _ from 'lodash';
import sinon from 'sinon';
import ADB from 'appium-adb';
import os from 'os';
import AndroidDriver from '../../..';


chai.should();
chai.use(chaiAsPromised);

describe('commands - logging', function () {
let driver;
before(function () {
driver = new AndroidDriver();
driver.adb = new ADB();
});
describe('getLogTypes', function () {
it('should get log types', async function () {
const types = await driver.getLogTypes();
// all the types should be returned
_.xor(['logcat', 'bugreport', 'server'], types).should.eql([]);
});
});
describe('getLog', function () {
it('should get logcat logs', async function () {
sinon.stub(driver.adb, 'getLogcatLogs').returns(['logs']);
(await driver.getLog('logcat')).should.be.deep.equal(['logs']);
driver.adb.getLogcatLogs.restore();
});
it('should get bugreport logs', async function () {
sinon.stub(driver.adb, 'bugreport').returns(`line1${os.EOL}line2`);
(await driver.getLog('bugreport')).should.be.deep.equal(['line1', 'line2']);
driver.adb.bugreport.restore();
});
});
});

0 comments on commit 488cd02

Please sign in to comment.