Skip to content

Commit f987b1d

Browse files
authored
screen off before wake up to clean text field, avoid no element crash (#498)
* screen off before wake up to clean text field, avoid no element crash * add a comment, define keycode as int * tweak comment message
1 parent fb3dfd7 commit f987b1d

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

lib/unlock-helpers.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ const PASSWORD_UNLOCK = 'password';
88
const PATTERN_UNLOCK = 'pattern';
99
const FINGERPRINT_UNLOCK = 'fingerprint';
1010
const UNLOCK_TYPES = [PIN_UNLOCK, PASSWORD_UNLOCK, PATTERN_UNLOCK, FINGERPRINT_UNLOCK];
11-
const KEYCODE_NUMPAD_ENTER = '66';
11+
const KEYCODE_NUMPAD_ENTER = 66;
12+
const KEYCODE_POWER = 26;
13+
const KEYCODE_WAKEUP = 224; // Can work over API Level 20
1214
const UNLOCK_WAIT_TIME = 100;
1315
const HIDE_KEYBOARD_WAIT_TIME = 100;
1416
const INPUT_KEYS_WAIT_TIME = 100;
@@ -41,7 +43,10 @@ helpers.isValidKey = function (type, key) {
4143

4244
helpers.dismissKeyguard = async function (driver, adb) {
4345
logger.info('Waking up the device to unlock it');
44-
await adb.keyevent('224');
46+
// Screen off once to force pre-inputted text field clean after wake-up
47+
// Just screen on if the screen defaults off
48+
await driver.pressKeyCode(KEYCODE_POWER);
49+
await driver.pressKeyCode(KEYCODE_WAKEUP);
4550
let isKeyboardShown = await driver.isKeyboardShown();
4651
if (isKeyboardShown) {
4752
await driver.hideKeyboard();
@@ -117,10 +122,13 @@ helpers.pinUnlock = async function (adb, driver, capabilities) {
117122
await driver.click(util.unwrapElement(el));
118123
}
119124
}
120-
const el = await driver.findElOrEls('xpath', "//*[contains(@resource-id, 'id/key_enter')]", false);
121-
await driver.click(util.unwrapElement(el));
122-
// Waits a bit for the device to be unlocked
125+
// Some devices accept entering the code without pressing the Enter key
126+
// When I rushed commands without this wait before pressKeyCode, rarely UI2 sever crashed
123127
await sleep(UNLOCK_WAIT_TIME);
128+
if (await adb.isScreenLocked()) {
129+
await driver.pressKeyCode(KEYCODE_NUMPAD_ENTER);
130+
await sleep(UNLOCK_WAIT_TIME);
131+
}
124132
};
125133

126134
helpers.passwordUnlock = async function (adb, driver, capabilities) {

test/unit/unlock-helper-specs.js

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import AndroidDriver from '../../lib/driver';
77
import * as asyncbox from 'asyncbox';
88
import ADB from 'appium-adb';
99

10-
const KEYCODE_NUMPAD_ENTER = '66';
10+
const KEYCODE_NUMPAD_ENTER = 66;
1111
const INPUT_KEYS_WAIT_TIME = 100;
1212
const HIDE_KEYBOARD_WAIT_TIME = 100;
1313
const UNLOCK_WAIT_TIME = 100;
@@ -61,9 +61,10 @@ describe('Unlock Helpers', function () {
6161
});
6262
});
6363
describe('dismissKeyguard', withMocks({driver, adb, asyncbox, helpers}, (mocks) => {
64-
it('should hide keyboard if keyboard is snown', async function () {
64+
it('should hide keyboard if keyboard is shown', async function () {
6565
mocks.driver.expects('isKeyboardShown').returns(true);
66-
mocks.adb.expects('keyevent').withExactArgs('224').once();
66+
mocks.driver.expects('pressKeyCode').withExactArgs(224).once();
67+
mocks.driver.expects('pressKeyCode').withExactArgs(26).once();
6768
mocks.driver.expects('hideKeyboard').once();
6869
mocks.asyncbox.expects('sleep').withExactArgs(HIDE_KEYBOARD_WAIT_TIME).once();
6970
mocks.adb.expects('shell').once();
@@ -77,7 +78,8 @@ describe('Unlock Helpers', function () {
7778
});
7879
it('should dismiss notifications and dissmiss keyguard via swipping up', async function () {
7980
mocks.driver.expects('isKeyboardShown').returns(false);
80-
mocks.adb.expects('keyevent').withExactArgs('224').once();
81+
mocks.driver.expects('pressKeyCode').withExactArgs(224).once();
82+
mocks.driver.expects('pressKeyCode').withExactArgs(26).once();
8183
mocks.adb.expects('shell')
8284
.withExactArgs(['service', 'call', 'notification', '1']).once();
8385
mocks.adb.expects('back').once();
@@ -90,7 +92,8 @@ describe('Unlock Helpers', function () {
9092
});
9193
it('should dissmiss keyguard via dismiss-keyguard shell command if API level > 21', async function () {
9294
mocks.driver.expects('isKeyboardShown').returns(false);
93-
mocks.adb.expects('keyevent').withExactArgs('224').once();
95+
mocks.driver.expects('pressKeyCode').withExactArgs(224).once();
96+
mocks.driver.expects('pressKeyCode').withExactArgs(26).once();
9497
mocks.adb.expects('shell').onCall(0).returns('');
9598
mocks.adb.expects('back').once();
9699
mocks.adb.expects('getApiLevel').returns(22);
@@ -168,14 +171,13 @@ describe('Unlock Helpers', function () {
168171
mocks.driver.expects('findElOrEls')
169172
.withExactArgs('id', 'com.android.systemui:id/digit_text', true)
170173
.returns(els);
171-
mocks.driver.expects('findElOrEls')
172-
.withExactArgs('xpath', "//*[contains(@resource-id, 'id/key_enter')]", false)
173-
.returns({ELEMENT: 100});
174+
mocks.adb.expects('isScreenLocked').returns(true);
175+
mocks.driver.expects('pressKeyCode').withExactArgs(66).once();
174176
for (let e of els) {
175177
mocks.driver.expects('getAttribute').withExactArgs('text', e.ELEMENT)
176178
.returns(e.ELEMENT.toString());
177179
}
178-
mocks.asyncbox.expects('sleep').withExactArgs(UNLOCK_WAIT_TIME).once();
180+
mocks.asyncbox.expects('sleep').withExactArgs(UNLOCK_WAIT_TIME).twice();
179181
sandbox.stub(driver, 'click');
180182

181183
await helpers.pinUnlock(adb, driver, caps);
@@ -185,7 +187,6 @@ describe('Unlock Helpers', function () {
185187
driver.click.getCall(2).args[0].should.equal(5);
186188
driver.click.getCall(3).args[0].should.equal(7);
187189
driver.click.getCall(4).args[0].should.equal(9);
188-
driver.click.getCall(5).args[0].should.equal(100);
189190

190191
mocks.helpers.verify();
191192
mocks.driver.verify();
@@ -201,9 +202,7 @@ describe('Unlock Helpers', function () {
201202
.withExactArgs('id', `com.android.keyguard:id/key${pin}`, false)
202203
.returns({ELEMENT: parseInt(pin, 10)});
203204
}
204-
mocks.driver.expects('findElOrEls')
205-
.withExactArgs('xpath', "//*[contains(@resource-id, 'id/key_enter')]", false)
206-
.returns({ELEMENT: 100});
205+
mocks.adb.expects('isScreenLocked').returns(false);
207206
mocks.asyncbox.expects('sleep').withExactArgs(UNLOCK_WAIT_TIME).once();
208207
sandbox.stub(driver, 'click');
209208

@@ -214,7 +213,6 @@ describe('Unlock Helpers', function () {
214213
driver.click.getCall(2).args[0].should.equal(5);
215214
driver.click.getCall(3).args[0].should.equal(7);
216215
driver.click.getCall(4).args[0].should.equal(9);
217-
driver.click.getCall(5).args[0].should.equal(100);
218216

219217
mocks.helpers.verify();
220218
mocks.driver.verify();

0 commit comments

Comments
 (0)