From e728f3d32016f2b51b1750c4c2acfa4006ed3a9c Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sun, 7 May 2017 18:10:31 +0900 Subject: [PATCH 1/3] clean hide_keybaord for ios --- lib/appium_lib/device/device.rb | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/lib/appium_lib/device/device.rb b/lib/appium_lib/device/device.rb index 1eb15630..e83713e9 100644 --- a/lib/appium_lib/device/device.rb +++ b/lib/appium_lib/device/device.rb @@ -279,12 +279,10 @@ def hide_keyboard(close_key = nil) 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 + close_key ? execute(:hide_keyboard, {}, key: close_key) : execute(:hide_keyboard, {}) else + close_key ||= 'Done' # default to Done key. $driver.hide_ios_keyboard close_key end end From 82d8adde26e63acc8a345823f4b95b13a8dc6569 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sun, 7 May 2017 18:17:09 +0900 Subject: [PATCH 2/3] update a comment --- lib/appium_lib/device/device.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/appium_lib/device/device.rb b/lib/appium_lib/device/device.rb index e83713e9..468392df 100644 --- a/lib/appium_lib/device/device.rb +++ b/lib/appium_lib/device/device.rb @@ -64,7 +64,7 @@ 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(not XCUITest). # ```ruby # hide_keyboard # Close a keyboard with the 'Done' button # hide_keyboard('Finished') # Close a keyboard with the 'Finished' button From 96ce9feb0e78c40eeec3ace7ef43c008fc8c4df8 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sun, 7 May 2017 19:14:12 +0900 Subject: [PATCH 3/3] only call json wired protocol to hide_keyboard --- lib/appium_lib/device/device.rb | 28 ++++++++++------- lib/appium_lib/ios/helper.rb | 56 --------------------------------- 2 files changed, 17 insertions(+), 67 deletions(-) diff --git a/lib/appium_lib/device/device.rb b/lib/appium_lib/device/device.rb index 468392df..920995db 100644 --- a/lib/appium_lib/device/device.rb +++ b/lib/appium_lib/device/device.rb @@ -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' for iOS(not XCUITest). + # 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 @@ -273,18 +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 - - if $driver.automation_name_is_xcuitest? - close_key ? execute(:hide_keyboard, {}, key: close_key) : execute(:hide_keyboard, {}) + 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 - close_key ||= 'Done' # default to Done key. - $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 diff --git a/lib/appium_lib/ios/helper.rb b/lib/appium_lib/ios/helper.rb index bb675f3a..e32bd0c2 100644 --- a/lib/appium_lib/ios/helper.rb +++ b/lib/appium_lib/ios/helper.rb @@ -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 #