lib/calabash-cucumber/keyboard_helpers.rb in calabash-cucumber-0.9.165 vs lib/calabash-cucumber/keyboard_helpers.rb in calabash-cucumber-0.9.166

- old
+ new

@@ -228,18 +228,19 @@ uia_type_string chr else code = UIA_SUPPORTED_CHARS[chr] unless code - raise "Char #{chr} is not yet supported in when typing with Instruments" + raise "typing character '#{chr}' is not yet supported when running with Instruments" end - # on iOS 6, the char code is _not_ \b - # - # as an aside, on iOS 7, the same approach (tap the 'Delete' key) also works - if ios6? and code.eql?(UIA_SUPPORTED_CHARS['Delete']) - uia("uia.keyboard().keys().firstWithName('Delete').tap()") + # on iOS 6, the Delete char code is _not_ \b + # on iOS 7, the Delete char code is \b on non-numeric keyboards + # on numeric keyboards, it is actually a button on the + # keyboard and not a key + if code.eql?(UIA_SUPPORTED_CHARS['Delete']) + uia("uia.keyboard().elements().firstWithName('Delete').tap()") else uia_type_string(code) end end res = {'results' => []} @@ -272,11 +273,11 @@ # # raises an error if the text cannot be entered def keyboard_enter_text(text) _ensure_can_enter_text if uia_available? - text_before = query("textField isFirstResponder:1",:text).first + text_before = _text_from_first_responder() uia_type_string(text, text_before) else text.each_char do |ch| begin keyboard_enter_char(ch, {:should_screenshot => false}) @@ -285,11 +286,10 @@ end end end end - # touches the keyboard +action+ key # # the +action+ key depends on the keyboard. some examples include: # * Return # * Next @@ -299,11 +299,11 @@ # # not all keyboards have an +action+ key # raises an error if the key cannot be entered def tap_keyboard_action_key if uia_available? - uia_type_string '\n' + uia_type_string '\n', '', false else keyboard_enter_char 'Return' end end @@ -698,12 +698,11 @@ # raises an error if the there is no <tt>run_loop</tt> def uia_keyboard_visible? unless uia_available? screenshot_and_raise 'only available if there is a run_loop i.e. the app was launched with Instruments' end - # TODO refactor keyboard detection to use uia() function conventions (instead of UIATarget...) - res = uia('UIATarget.localTarget().frontMostApp().keyboard()')['value'] + res = uia_query_windows(:keyboard) not res.eql?(':nil') end # waits for a keyboard that is not normally visible to calabash # e.g. the keyboard on +MFMailComposeViewController+ @@ -726,9 +725,32 @@ end wait_for(opts) do uia_keyboard_visible? end + end + + + private + + # returns the the text in the first responder + # + # the first responder will be the +UITextField+ or +UITextView+ instance + # that is associated with the visible keyboard. + # + # returns +nil+ if there no +textField+ or +testView+ is found to be + # the first responder. it is extremely unlikely that this will happen. + # + # raises an exception if there is no visible keyboard + def _text_from_first_responder + raise 'there must be a visible keyboard' unless keyboard_visible? + + ['textField', 'textView'].each do |ui_class| + res = query("#{ui_class} isFirstResponder:1", :text) + return res.first unless res.empty? + end + #noinspection RubyUnnecessaryReturnStatement + return nil end end end end