Skip to content
Browse files

Make textfield cross platform

There are now two implementations of textfield.
- android/textfield works on text and combines secure and textfield.
- ios/textfield works the same way except in native iOS secure is not the
same as textfield so UIAutomation is used to match the Android behavior.

Removed old docs. Documentation will return once ruby info updates.
  • Loading branch information...
1 parent 9d066d4 commit d68887d6d64078ae5b17b49b8f1700b1380999f9 @bootstraponline bootstraponline committed Mar 7, 2013
Showing with 108 additions and 87 deletions.
  1. +9 −2 lib/console.rb
  2. 0 lib/element/{ → android}/textfield.rb
  3. +76 −0 lib/element/ios/textfield.rb
  4. +0 −40 lib/element/secure.rb
  5. +22 −0 lib/helper.rb
  6. +1 −45 readme.md
View
11 lib/console.rb
@@ -13,8 +13,6 @@
# ruby_console files
require 'helper'
require 'button'
-require 'textfield'
-require 'secure' # secure textfield, iOS only.
require 'text'
require 'window'
require 'patch'
@@ -45,6 +43,15 @@
$os = :android if APP_PATH.end_with?('.apk') || APP_PATH.end_with?('.apk.zip')
end
+# Android combines secure and textfield.
+# iOS differentiates between secure and textfield.
+# combine secure & textfield on iOS to match Android behavior.
+if $os == :ios
+ require 'ios/textfield'
+else
+ require 'android/textfield'
+end
+
# WebDriver capabilities. Must be valid for Sauce to work.
# https://github.com/jlipps/appium/blob/master/app/android.js
def android_capabilities
View
0 lib/element/textfield.rb → lib/element/android/textfield.rb
File renamed without changes.
View
76 lib/element/ios/textfield.rb
@@ -0,0 +1,76 @@
+# encoding: utf-8
+# UIATextField & UIASecureTextField methods
+#
+# Find textfield and then secure elements in one server call
+# to match Android.
+
+# Get an array of textfield texts.
+# @return [Array<String>]
+def textfields
+ find_2_eles_attr :textfield, :secure, :text
+end
+
+# Get an array of textfield elements.
+# @return [Array<Textfield>]
+def e_textfields
+ execute_script textfield_js
+end
+
+# Get the first textfield element.
+# @return [Textfield]
+def first_textfield
+ js = textfield_js 'r = r.length > 0 ? $(r[0]) : r;'
+ execute_script(js).first
+end
+
+# Get the last textfield element.
+# @return [Textfield]
+def last_textfield
+ js = textfield_js 'r = r.length > 0 ? $(r[r.length - 1]) : r;'
+ execute_script(js).first
+end
+
+# Get the first textfield that matches text.
+# @param text [String] the text to match exactly
+# @return [Textfield]
+def textfield text
+ # find_ele_by_text :textfield, text
+ js = %Q(
+ var t = au.getElementsByXpath('textfield[@text="#{text}"]').value;
+ var s = au.getElementsByXpath('secure[@text="#{text}"]').value;
+ t.concat(s)[0];
+ )
+
+ puts js if defined?(Pry)
+
+ execute_script js
+end
+
+# Get the first textfield that includes text.
+# @param text [String] the text the textfield must include
+# @return [Textfield]
+def textfield_include text
+ js = %Q(
+ var t = au.getElementsByXpath('textfield[contains(@text, "#{text}")]').value;
+ var s = au.getElementsByXpath('secure[contains(@text, "#{text}")]').value;
+ t.concat(s)[0];
+ )
+
+ puts js if defined?(Pry)
+
+ execute_script js
+end
+
+private
+
+# Return combined lookup of textfield and secure
+# with an optional filter. $() wrap is required for .each
+def textfield_js filter=''
+%Q(
+ var t = au.lookup('textfield');
+ var s = au.lookup('secure');
+ var r = $(t.concat(s));
+ #{filter}
+ au._returnElems(r);
+)
+end
View
40 lib/element/secure.rb
@@ -1,40 +0,0 @@
-# encoding: utf-8
-# UIASecureTextField methods
-
-# Get an array of secure textfield texts.
-# @return [Array<String>]
-def secures
- find_eles_attr :secure, :text
-end
-
-# Get an array of secure textfield elements.
-# @return [Array<Secure>]
-def e_secures
- find_eles :secure
-end
-
-# Get the first secure textfield element.
-# @return [Secure]
-def first_secure
- first_ele :secure
-end
-
-# Get the last secure textfield element.
-# @return [Secure]
-def last_secure
- last_ele :secure
-end
-
-# Get the first secure textfield that matches text.
-# @param text [String] the text to find exactly
-# @return [Secure]
-def secure text
- find_ele_by_text :secure, text
-end
-
-# Get the first secure textfield that includes text.
-# @param text [String] the text that the tag must include
-# @return [Secure]
-def secure_include text
- find_ele_by_text_include :secure, text
-end
View
22 lib/helper.rb
@@ -56,6 +56,28 @@ def find_eles_attr tag_name, attribute
$driver.execute_script js
end
+# iOS only. Android uses uiautomator instead of uiautomation.
+# Get an array of attribute values from elements exactly matching tag name.
+# @param tag_name_1 [String] the 1st tag name to find
+# @param tag_name_2 [String] the 2nd tag name to find
+# @param attribute [String] the attribute to collect
+# @result [Array<String>] an array of strings containing the attribute from found elements of type tag_name.
+def find_2_eles_attr tag_name_1, tag_name_2, attribute
+ # Use au.lookup(tag_name) instead of $(tag_name)
+ # See https://github.com/appium/appium/issues/214
+ js = %Q(
+ var eles = au.lookup('#{tag_name_1}');
+ eles = $(eles.concat(au.lookup('#{tag_name_2}')));
+ var result = [];
+ for (var a = 0, length = eles.length; a < length; a++) {
+ result.push(eles[a].#{attribute}());
+ }
+ result
+ )
+
+ $driver.execute_script js
+end
+
# Get the first tag that exactly matches tag and text.
# @param tag [String] the tag name to match
# @param text [String] the text to exactly match
View
46 readme.md
@@ -53,51 +53,7 @@ text | UIAStaticText
--
-#### alert
-0. `(void) alert_accept` Accept the alert.
-0. `(String) alert_accept_text` Get the text of the alert's accept button.
-0. `(void) alert_click(value)` Tap the alert button identified by value.
-0. `(void) alert_dismiss` Dismiss the alert.
-0. `(String) alert_dismiss_text` Get the text of the alert's dismiss button.
-0. `(String) alert_text` Get the alert message text.
-
-#### button
-0. `(Button) button(name, number = 0)` Find a button by name and optionally number.
-0. `(Button) button_include(name)` Get the first button that includes name.
-0. `(Button) button_name(name)` Get the first button element that exactly matches name.
-0. `(Button) button_name_num(name, number = 1)` Get the button element exactly matching name and occurrence.
-0. `(Array<String>, Array<Buttons>) buttons(name = nil)` Get an array of button names or button elements if name is provided.
-0. `(Array<Button>) buttons_include(name)` Get all buttons that include name.
-0. `(Array<Button>) e_buttons` Get an array of button elements.
-0. `(Button) first_button` Get the first button element.
-0. `(Button) last_button` Get the last button element.
-
-#### secure
-0. `(Array<Secure>) e_secure` Get an array of secure textfield elements.
-0. `(Secure) first_secure` Get the first secure textfield element.
-0. `(Secure) last_secure` Get the last secure textfield element.
-0. `(Secure) secure(value)` Get the first secure textfield that matches value.
-0. `(Secure) secure_include(value)` Get the first secure textfield that includes value.
-0. `(Array<String>) secures` Get an array of secure textfield values.
-
-#### textfield
-0. `(Array<Textfield>) e_textfields` Get an array of textfield elements.
-0. `(Textfield) first_textfield` Get the first textfield element.
-0. `(Textfield) last_textfield` Get the last textfield element.
-0. `(Textfield) textfield(value)` Get the first textfield that matches value.
-0. `(Textfield) textfield_include(value)` Get the first textfield that includes value.
-0. `(Array<String>) textfields` Get an array of textfield values.
-
-#### text
-0. `(Array<Text>) e_texts` Get an array of text elements.
-0. `(Text) first_text` Get the first text element.
-0. `(Text) last_text` Get the last text element.
-0. `(Text) text(name)` Get the first element that matches name.
-0. `(Text) text_include(name)` Get the first textfield that includes name.
-0. `(Array<String>) texts` Get an array of text names.
-
-#### window
-0. `(Object) window_size` Get the window's size.
+<docs/>
--

0 comments on commit d68887d

Please sign in to comment.
Something went wrong with that request. Please try again.