Reputation: 151
How do you dismiss the keyboard in an Appium test on iOS? I get this entry in the console after entering text
[INSTSERVER] Sending command to instruments: if (!au.mainApp().keyboard().isNil()) {
var key = au.mainApp().keyboard().buttons()['Done']
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);
}
I can see it is assuming the button is 'Done' on my keyboard it is 'return'. Looking at the documentation I should be able to do this in the following way https://github.com/appium/ruby_lib/blob/ef20cdd09cdb3dac32b789144b17ed2daf745a8d/docs/ios_docs.md#hide_keyboard
I have tried this using the following code:
When(/^I enter the text "(.*?)"$/) do |user_text|
textfield( "My Textbox" ).type user_text
hide_keyboard( "Return" )
end
Despite this it still hangs looking for the 'Done' button. How do you override which key Appium looks for. I have uploaded a Git repository with my code here: GitHub Repository
When I use 'Done' as the keyboard button to Return it works. Problem is my app doesn't use 'Done'.
Upvotes: 1
Views: 2493
Reputation: 151
I managed to fix this issue in the end by resorting to patching the keyboard dismissal code in Appium like this.
features/support/env.rb
module Appium
module Ios
def patch_webdriver_element
Selenium::WebDriver::Element.class_eval do
# Enable access to iOS accessibility label
# accessibility identifier is supported as 'name'
def label
self.attribute('label')
end
# Cross platform way of entering text into a textfield
def type text
$driver.execute_script %(au.getElement('#{self.ref}').setValue('#{text}');)
end
end
end
def hide_ios_keyboard close_key='Done'
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();
}
}
JS
ignore do
wait_true(5) do
execute_script '!au.mainApp().keyboard().isNil()'
end
# dismiss keyboard
execute_script dismiss_keyboard
end
wait_true(5) do
execute_script 'au.mainApp().keyboard().isNil()'
end
end
end
end
Then within my step definitions I can manually dismiss the keyboard after entering text allowing me to specify what the 'Return' button is called on the keyboard.
def enter_postcode( postcode )
textfield( "postcode" ).type postcode
hide_ios_keyboard('Return')
end
Upvotes: 1