Skip to content

Commit 1b160b4

Browse files
Fix typing on iOS
1 parent 44eb53b commit 1b160b4

File tree

2 files changed

+44
-5
lines changed

2 files changed

+44
-5
lines changed

ios_tests/lib/ios/specs/ios/element/textfield.rb

+26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# encoding: utf-8
2+
# rake ios[ios/element/textfield]
23
describe 'ios/element/textfield' do
34
before_first { go_to_textfields }
45
after_last { set_wait 30 }
@@ -41,6 +42,31 @@ def enter_password
4142
textfield_exact(enter_password).text.must_equal enter_password
4243
end
4344

45+
def keyboard_exists?
46+
!! ignore { wait_true(3) { execute_script 'au.mainApp().keyboard().type() !== "UIAElementNil"' } }
47+
end
48+
49+
def keyboard_must_not_exist
50+
keyboard_exists?.must_equal false
51+
end
52+
53+
def keyboard_must_exist
54+
keyboard_exists?.must_equal true
55+
end
56+
57+
t 'textfield type' do
58+
# Regular send keys triggers the keyboard and doesn't dismiss
59+
keyboard_must_not_exist
60+
textfield(1).send_keys 'ok'
61+
keyboard_must_exist
62+
63+
# type will dismiss the keyboard
64+
message = 'type test type'
65+
textfield(1).type message
66+
keyboard_must_not_exist
67+
textfield(1).text.must_equal message
68+
end
69+
4470
def must_raise_no_element &block
4571
proc { block.call }.must_raise Selenium::WebDriver::Error::NoSuchElementError
4672
end

lib/appium_lib/ios/patch.rb

+18-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,23 @@ def type text
2222
-10 to ensure we're not going outside the window bounds.
2323
2424
Swiping inside the keyboard will not dismiss it.
25+
26+
var startY = au.mainApp().keyboard().rect().origin.y - 10;
27+
var endY = au.mainWindow().rect().size.height - 10;
28+
au.flickApp(0, startY, 0, endY);
29+
30+
The above logic has been accepted as part of appium's au.hideKeyboard
31+
https://github.com/appium/appium-uiauto/blob/dbeb4eedbdea2104751a0d547ac9b2894e0dc567/uiauto/appium/app.js#L902
32+
33+
If the 'Done' key exists then that should be pressed to dismiss the keyboard
34+
because swiping to dismiss works only if such key doesn't exist.
2535
=end
2636
# type
2737
$driver.execute_script %(au.getElement('#{self.ref}').setValue('#{text}');)
2838

2939
$driver.ignore {
30-
# wait 5 seconds for keyboard. if the textfield is disabled then
31-
# setValue will work, however the keyboard will never display
40+
# wait 5 seconds for a wild keyboard to appear. if the textfield is disabled
41+
# then setValue will work, however the keyboard will never display
3242
# because users are normally not allowed to type into it.
3343
$driver.wait_true(5) do
3444
$driver.execute_script %(au.mainApp().keyboard().type() !== 'UIAElementNil')
@@ -37,13 +47,16 @@ def type text
3747
# dismiss keyboard
3848
js = <<-JS
3949
if (au.mainApp().keyboard().type() !== "UIAElementNil") {
40-
var startY = au.mainApp().keyboard().rect().origin.y - 10;
41-
var endY = au.mainWindow().rect().size.height - 10;
42-
au.flickApp(0, startY, 0, endY);
50+
au.hideKeyboard('Done');
4351
}
4452
JS
4553

4654
$driver.execute_script js
55+
56+
# wait 5 seconds for keyboard to go away
57+
$driver.wait_true(5) do
58+
$driver.execute_script %(au.mainApp().keyboard().type() === 'UIAElementNil')
59+
end
4760
}
4861
end # def type
4962
end # Selenium::WebDriver::Element.class_eval

0 commit comments

Comments
 (0)