Skip to content

Commit 2ff78b1

Browse files
authored
clean hide_keybaord for ios (#572)
* clean hide_keybaord for ios * update a comment * only call json wired protocol to hide_keyboard
1 parent 27e10f8 commit 2ff78b1

File tree

2 files changed

+17
-69
lines changed

2 files changed

+17
-69
lines changed

lib/appium_lib/device/device.rb

+17-13
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,14 @@ module Device
6464
# @!method hide_keyboard
6565
# Hide the onscreen keyboard
6666
# @param [String] close_key The name of the key which closes the keyboard.
67-
# Defaults to 'Done'.
67+
# Defaults to 'Done' for iOS(except for XCUITest).
68+
# @param [Symbol] strategy The symbol of the strategy which closes the keyboard.
69+
# XCUITest ignore this argument.
70+
# Default for iOS is `:pressKey`. Default for Android is `:tapOutside`.
6871
# ```ruby
6972
# hide_keyboard # Close a keyboard with the 'Done' button
7073
# hide_keyboard('Finished') # Close a keyboard with the 'Finished' button
74+
# hide_keyboard(nil, :tapOutside) # Close a keyboard with tapping out side of keyboard
7175
# ```
7276

7377
# @!method press_keycode
@@ -273,20 +277,20 @@ def set_context(context = null)
273277
end
274278

275279
add_endpoint_method(:hide_keyboard) do
276-
def hide_keyboard(close_key = nil)
277-
# Android can only tapOutside.
278-
if $driver.device_is_android?
279-
return execute :hide_keyboard, {}, strategy: :tapOutside
280-
end
281-
282-
close_key ||= 'Done' # default to Done key.
283-
if $driver.automation_name_is_xcuitest?
284-
# strategy is not implemented in the following
285-
# https://github.com/appium/appium-xcuitest-driver/blob/v2.2.0/lib/commands/general.js#L51
286-
execute :hide_keyboard, {}, strategy: :grouped, key: close_key
280+
def hide_keyboard(close_key = nil, strategy = nil)
281+
option = {}
282+
283+
if $driver.device_is_android? # Android can only tapOutside.
284+
option[:key] = close_key if close_key
285+
option[:strategy] = strategy || :tapOutside # default to pressKey
286+
elsif $driver.automation_name_is_xcuitest?
287+
option[:key] = close_key if close_key
288+
option[:strategy] = strategy if strategy
287289
else
288-
$driver.hide_ios_keyboard close_key
290+
option[:key] = close_key || 'Done' # default to Done key.
291+
option[:strategy] = strategy || :pressKey # default to pressKey
289292
end
293+
execute :hide_keyboard, {}, option
290294
end
291295
end
292296

lib/appium_lib/ios/helper.rb

-56
Original file line numberDiff line numberDiff line change
@@ -527,62 +527,6 @@ def eles_by_json_visible_exact(element, value)
527527
eles_by_json string_visible_exact element, value
528528
end
529529

530-
# @private
531-
# For Appium(automation name), not XCUITest
532-
# If there's no keyboard, then do nothing.
533-
# If there's no close key, fallback to window tap.
534-
# If close key is present then tap it.
535-
# @param close_key [String] close key to tap. Default value is 'Done'
536-
# @return [void]
537-
def hide_ios_keyboard(close_key = 'Done')
538-
#
539-
# TODO: there are many various ways to hide the keyboard that work in different
540-
# app specific circumstances. webview keyboard will require a window.tap for example.
541-
#
542-
# Find the top left corner of the keyboard and move up 10 pixels (origin.y - 10)
543-
# now swipe down until the end of the window - 10 pixels.
544-
# -10 to ensure we're not going outside the window bounds.
545-
#
546-
# Swiping inside the keyboard will not dismiss it.
547-
#
548-
# If the 'Done' key exists then that should be pressed to dismiss the keyboard
549-
# because swiping to dismiss works only if such key doesn't exist.
550-
#
551-
# Don't use window.tap. See https://github.com/appium/appium-uiauto/issues/28
552-
#
553-
dismiss_keyboard = <<-JS.strip
554-
if (!au.mainApp().keyboard().isNil()) {
555-
var key = au.mainApp().keyboard().buttons()['#{close_key}']
556-
if (key.isNil()) {
557-
var startY = au.mainApp().keyboard().rect().origin.y - 10;
558-
var endY = au.mainWindow().rect().size.height - 10;
559-
au.flickApp(0, startY, 0, endY);
560-
} else {
561-
key.tap();
562-
}
563-
au.delay(1000);
564-
}
565-
JS
566-
567-
ignore do
568-
# wait 5 seconds for a wild keyboard to appear. if the textfield is disabled
569-
# then setValue will work, however the keyboard will never display
570-
# because users are normally not allowed to type into it.
571-
wait_true(5) do
572-
execute_script '!au.mainApp().keyboard().isNil()'
573-
end
574-
575-
# dismiss keyboard
576-
execute_script dismiss_keyboard
577-
end
578-
579-
# wait 5 seconds for keyboard to go away.
580-
# if the keyboard isn't dismissed then wait_true will error.
581-
wait_true(5) do
582-
execute_script 'au.mainApp().keyboard().isNil()'
583-
end
584-
end
585-
586530
#
587531
# predicate - the predicate to evaluate on the main app
588532
#

0 commit comments

Comments
 (0)