Skip to content

Commit

Permalink
Make getApiLevel to return number instead of string (appium#269)
Browse files Browse the repository at this point in the history
* Make getApiLevel to return number instead of string

* Use isInteger instead of isNumber
  • Loading branch information
Mykola Mokhnach authored and imurchie committed Oct 31, 2017
1 parent a87a7e9 commit 9ee5b92
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 11 deletions.
10 changes: 7 additions & 3 deletions lib/tools/adb-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,18 @@ methods.initZipAlign = async function () {
/**
* Retrieve the API level of the device under test.
*
* @return {string} The API level as integer number, for example 21 for
* @return {number} The API level as integer number, for example 21 for
* Android Lollipop. The result of this method is cached, so all the further
* calls return the same value as the first one.
*/
methods.getApiLevel = async function () {
if (!this._apiLevel) {
if (!_.isInteger(this._apiLevel)) {
try {
this._apiLevel = await this.getDeviceProperty('ro.build.version.sdk');
const strOutput = await this.getDeviceProperty('ro.build.version.sdk');
this._apiLevel = parseInt(strOutput.trim(), 10);
if (isNaN(this._apiLevel)) {
throw new Error(`The actual output "${strOutput}" cannot be converted to an integer`);
}
} catch (e) {
log.errorAndThrow(`Error getting device API level. Original error: ${e.message}`);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/tools/adb-emu-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ emuMethods.fingerprint = async function (fingerprintId) {
}
// the method used only works for API level 23 and above
let level = await this.getApiLevel();
if (parseInt(level, 10) < 23) {
if (level < 23) {
log.errorAndThrow(`Device API Level must be >= 23. Current Api level '${level}'`);
}
await this.adbExecEmu(['finger', 'touch', fingerprintId]);
Expand Down
2 changes: 1 addition & 1 deletion test/functional/adb-emu-commands-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('adb emu commands', () => {

// the test here only works if we have API level 23 or above
// it will also fail on emulators
if (parseInt(await adb.getApiLevel(), 10) < 23 || !process.env.REAL_DEVICE) {
if (await adb.getApiLevel() < 23 || !process.env.REAL_DEVICE) {
this.skip();
}
});
Expand Down
1 change: 1 addition & 0 deletions test/functional/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const platformVersion = process.env.PLATFORM_VERSION || '4.3';

let apiLevel = process.env.API_LEVEL ||
API_LEVEL_MAP[parseFloat(platformVersion).toString()];
apiLevel = parseInt(apiLevel, 10);

let MOCHA_TIMEOUT = process.env.TRAVIS ? 240000 : 60000;
let MOCHA_LONG_TIMEOUT = MOCHA_TIMEOUT * 10;
Expand Down
2 changes: 1 addition & 1 deletion test/functional/syscalls-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe('System calls', function () {
(await adb.isDeviceConnected()).should.be.true;
});
it('shell should execute command in adb shell ', async () => {
(await adb.shell(['getprop', 'ro.build.version.sdk'])).should.equal(apiLevel);
(await adb.shell(['getprop', 'ro.build.version.sdk'])).should.equal(`${apiLevel}`);
});
it('getConnectedEmulators should get all connected emulators', async () => {
(await adb.getConnectedEmulators()).length.should.be.above(0);
Expand Down
4 changes: 2 additions & 2 deletions test/unit/adb-commands-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { withMocks } from 'appium-test-support';

chai.use(chaiAsPromised);
const should = chai.should();
const apiLevel = '21',
const apiLevel = 21,
platformVersion = '4.4.4',
language = 'en',
country = 'US',
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('adb commands', () => {
it('should call shell with correct args', async () => {
mocks.adb.expects("getDeviceProperty")
.once().withExactArgs('ro.build.version.sdk')
.returns(apiLevel);
.returns(`${apiLevel}`);
(await adb.getApiLevel()).should.equal(apiLevel);
mocks.adb.verify();
});
Expand Down
6 changes: 3 additions & 3 deletions test/unit/apk-utils-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ describe('Apk-utils', () => {
it('should call getApiLevel and shell with correct arguments', async () => {
mocks.adb.expects('getApiLevel')
.once().withExactArgs()
.returns('17');
.returns(17);
mocks.adb.expects('shell')
.once().withExactArgs(cmd)
.returns('');
Expand All @@ -359,7 +359,7 @@ describe('Apk-utils', () => {
it('should call getApiLevel and shell with correct arguments', async () => {
mocks.adb.expects('getApiLevel')
.twice()
.returns('17');
.returns(17);
mocks.adb.expects('shell')
.onCall(0)
.returns('Error: Activity class foo does not exist');
Expand All @@ -374,7 +374,7 @@ describe('Apk-utils', () => {

mocks.adb.expects('getApiLevel')
.once().withExactArgs()
.returns('17');
.returns(17);
mocks.adb.expects('shell')
.once().withExactArgs(cmdWithInnerClass)
.returns('');
Expand Down

0 comments on commit 9ee5b92

Please sign in to comment.