Skip to content

Instantly share code, notes, and snippets.

@jmoody
Created June 23, 2012 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmoody/2978017 to your computer and use it in GitHub Desktop.
Save jmoody/2978017 to your computer and use it in GitHub Desktop.
cucumber steps for interacting with native keyboard using calabash-cucumber
def should_see_keyboard
res = element_exists("keyboardAutomatic")
unless res
screenshot_and_raise "Expected keyboard to be visible."
end
end
def should_not_see_keyboard
res = element_exists("keyboardAutomatic")
if res
screenshot_and_raise "Expected keyboard to not be visible."
end
end
Then /^I should see the keyboard$/ do
should_see_keyboard
end
Then /^I should not see the keyboard$/ do
should_not_see_keyboard
end
#UITextAutocapitalizationTypeNone,
#UITextAutocapitalizationTypeWords,
#UITextAutocapitalizationTypeSentences,
#UITextAutocapitalizationTypeAllCharacters
# is it possible to find what view the keyboard is responding to?
def autocapitalization_type ()
if !query("textView index:0").empty?
query("textView index:0", :autocapitalizationType).first.to_i
elsif !query("textField index:0").empty?
query("textField index:0", :autocapitalizationType).first.to_i
else
screenshot_and_raise "could not find a text view or text field"
end
end
def is_capitalize_none (cap_type)
cap_type == 0
end
def is_capitalize_words (cap_type)
cap_type == 1
end
def is_capitalize_sentences (cap_type)
cap_type == 2
end
def is_capitalize_all (cap_type)
cap_type == 0
end
def prepare_text_for_input (text)
cap_type = autocapitalization_type
if is_capitalize_none cap_type
text.downcase
elsif is_capitalize_words cap_type
text.split(' ').map {|w| w.capitalize }.join(' ')
elsif is_capitalize_sentences cap_type
text.split(/[.]\s*/).map {|w| w.capitalize }.join('. ')
elsif is_capitalize_all cap_type
text.upcase
else
screenshot_and_raise "did not recognize capitalization type #{autocapitalization_type}"
end
end
def use_keyboard_to_enter text
text.each_char { |ch| keyboard_enter_char ch; sleep(0.4) }
end
Then /^I use the keyboard to enter "([^"]*)"$/ do |text|
@text_entered_by_keyboard = prepare_text_for_input text
wait_for_animation
should_see_keyboard
use_keyboard_to_enter (prepare_text_for_input text)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment