Skip to content

Commit

Permalink
refactor: Move keyboard dismissal logic to WDA (#1267)
Browse files Browse the repository at this point in the history
  • Loading branch information
mykola-mokhnach committed Jan 21, 2021
1 parent 9fd67ca commit ebd9d93
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 63 deletions.
47 changes: 5 additions & 42 deletions lib/commands/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,49 +155,12 @@ commands.getWindowRect = async function getWindowRect () {
};

commands.hideKeyboard = async function hideKeyboard (strategy, ...possibleKeys) {
if (!(this.opts.deviceName || '').includes('iPhone')) {
// TODO: once WDA can handle dismissing keyboard for iphone, take away conditional
try {
await this.proxyCommand('/wda/keyboard/dismiss', 'POST');
return;
} catch (ign) {}
}

log.debug('Cannot dismiss the keyboard using the native call. Trying to apply a workaround...');

let keyboard;
try {
keyboard = await this.findNativeElementOrElements('class name', 'XCUIElementTypeKeyboard', false);
} catch (err) {
// no keyboard found
log.debug('No keyboard found. Unable to hide.');
return;
}
possibleKeys.pop(); // last parameter is the session id
possibleKeys = possibleKeys.filter((element) => !!element); // get rid of undefined elements
if (possibleKeys.length) {
for (const key of possibleKeys) {
let el = _.last(await this.findNativeElementOrElements('accessibility id', key, true, keyboard));
if (el) {
log.debug(`Attempting to hide keyboard by pressing '${key}' key.`);
await this.nativeClick(el);
return;
}
}
} else {
// find the keyboard, and hit the last Button
log.debug('Finding keyboard and clicking final button to close');
if (await this.getNativeAttribute('visible', keyboard) === 'false') {
log.debug('No visible keyboard found. Returning');
return;
}
let buttons = await this.findNativeElementOrElements('class name', 'XCUIElementTypeButton', true, keyboard);
if (_.isEmpty(buttons)) {
log.warn(`No button elements found. Unable to hide.`);
return;
}
await this.nativeClick(_.last(buttons));
// last parameter is the session id
const keyNames = _.compact(possibleKeys.slice(0, -1)).map((x) => `${x}`);
if (!keyNames.includes('done')) {
keyNames.push('done');
}
await this.proxyCommand('/wda/keyboard/dismiss', 'POST', {keyNames});
};

commands.getStrings = iosCommands.general.getStrings;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"appium-ios-simulator": "^3.25.1",
"appium-remote-debugger": "^8.13.2",
"appium-support": "^2.47.1",
"appium-webdriveragent": "^2.33.1",
"appium-webdriveragent": "^2.34.0",
"appium-xcode": "^3.8.0",
"async-lock": "^1.0.0",
"asyncbox": "^2.3.1",
Expand Down
22 changes: 2 additions & 20 deletions test/functional/basic/element-e2e-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,26 +342,8 @@ describe('XCUITestDriver - element(s)', function () {
});
});
describe('hide keyboard', function () {
it('should be able to hide the keyboard', async function () {
// pause for a second, or else some systems will falsely fail on this test
await B.delay(1000);
let el = await driver.elementByClassName('XCUIElementTypeTextField');
await el.click();

// pause to make sure the keyboard comes up
await B.delay(500);

let db = await driver.elementByAccessibilityId('Done');
(await db.isDisplayed()).should.be.true;

await driver.hideKeyboard();

// pause for a second to allow keyboard to go out of view
// otherwise slow systems will reject the search for `Done` and
// fast ones will get the element but it will be invisible
await B.delay(1000);

db = await driver.elementByAccessibilityId('Done').should.eventually.be.rejected;
it('should pass if the keyboard is already hidden', async function () {
await driver.hideKeyboard().should.eventually.be.fulfilled;
});
});
});
Expand Down

0 comments on commit ebd9d93

Please sign in to comment.