Skip to content

clean hide_keybaord for ios #572

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions lib/appium_lib/device/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,14 @@ module Device
# @!method hide_keyboard
# Hide the onscreen keyboard
# @param [String] close_key The name of the key which closes the keyboard.
# Defaults to 'Done'.
# Defaults to 'Done' for iOS(except for XCUITest).
# @param [Symbol] strategy The symbol of the strategy which closes the keyboard.
# XCUITest ignore this argument.
# Default for iOS is `:pressKey`. Default for Android is `:tapOutside`.
# ```ruby
# hide_keyboard # Close a keyboard with the 'Done' button
# hide_keyboard('Finished') # Close a keyboard with the 'Finished' button
# hide_keyboard(nil, :tapOutside) # Close a keyboard with tapping out side of keyboard
# ```

# @!method press_keycode
Expand Down Expand Up @@ -273,20 +277,20 @@ def set_context(context = null)
end

add_endpoint_method(:hide_keyboard) do
def hide_keyboard(close_key = nil)
# Android can only tapOutside.
if $driver.device_is_android?
return execute :hide_keyboard, {}, strategy: :tapOutside
end

close_key ||= 'Done' # default to Done key.
if $driver.automation_name_is_xcuitest?
# strategy is not implemented in the following
# https://github.com/appium/appium-xcuitest-driver/blob/v2.2.0/lib/commands/general.js#L51
execute :hide_keyboard, {}, strategy: :grouped, key: close_key
def hide_keyboard(close_key = nil, strategy = nil)
option = {}

if $driver.device_is_android? # Android can only tapOutside.
option[:key] = close_key if close_key
option[:strategy] = strategy || :tapOutside # default to pressKey
elsif $driver.automation_name_is_xcuitest?
option[:key] = close_key if close_key
option[:strategy] = strategy if strategy
else
$driver.hide_ios_keyboard close_key
option[:key] = close_key || 'Done' # default to Done key.
option[:strategy] = strategy || :pressKey # default to pressKey
end
execute :hide_keyboard, {}, option
end
end

Expand Down
56 changes: 0 additions & 56 deletions lib/appium_lib/ios/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -527,62 +527,6 @@ def eles_by_json_visible_exact(element, value)
eles_by_json string_visible_exact element, value
end

# @private
# For Appium(automation name), not XCUITest
# If there's no keyboard, then do nothing.
# If there's no close key, fallback to window tap.
# If close key is present then tap it.
# @param close_key [String] close key to tap. Default value is 'Done'
# @return [void]
def hide_ios_keyboard(close_key = 'Done')
#
# TODO: there are many various ways to hide the keyboard that work in different
# app specific circumstances. webview keyboard will require a window.tap for example.
#
# Find the top left corner of the keyboard and move up 10 pixels (origin.y - 10)
# now swipe down until the end of the window - 10 pixels.
# -10 to ensure we're not going outside the window bounds.
#
# Swiping inside the keyboard will not dismiss it.
#
# If the 'Done' key exists then that should be pressed to dismiss the keyboard
# because swiping to dismiss works only if such key doesn't exist.
#
# Don't use window.tap. See https://github.com/appium/appium-uiauto/issues/28
#
dismiss_keyboard = <<-JS.strip
if (!au.mainApp().keyboard().isNil()) {
var key = au.mainApp().keyboard().buttons()['#{close_key}']
if (key.isNil()) {
var startY = au.mainApp().keyboard().rect().origin.y - 10;
var endY = au.mainWindow().rect().size.height - 10;
au.flickApp(0, startY, 0, endY);
} else {
key.tap();
}
au.delay(1000);
}
JS

ignore do
# wait 5 seconds for a wild keyboard to appear. if the textfield is disabled
# then setValue will work, however the keyboard will never display
# because users are normally not allowed to type into it.
wait_true(5) do
execute_script '!au.mainApp().keyboard().isNil()'
end

# dismiss keyboard
execute_script dismiss_keyboard
end

# wait 5 seconds for keyboard to go away.
# if the keyboard isn't dismissed then wait_true will error.
wait_true(5) do
execute_script 'au.mainApp().keyboard().isNil()'
end
end

#
# predicate - the predicate to evaluate on the main app
#
Expand Down