iOS UI Automation: Tapping the Clear Button in UITextField


Here’s a code snippet when you want to add a function “clear” that will tap the Clear button in a UITextField.


UIATextField.prototype["clear"] = function()
{
     if (!this.hasKeyboardFocus())
          this.tap();
     this.tapWithOptions({tapOffset:{x:0.8, y:0.5}});
};

this.hasKeyboardFocus() checks whether the current UIATextField object has the keyboard focus. If not, then you have to tap the UIATextField to activate the Clear button. UIATextField.tapWithOptions is then called with the option specifying the tapOffset. The Apple documentation on the tapOffset option of  UIAElement.tapWithOptions says:

You can use offsets to achieve finer precision in specifying the hitpoint within the rect for the specified element. The offset comprises a pair of x and y values, each ranging from 0.0 to 1.0. These values represent, respectively, relative horizontal and vertical positions within the rect, with {x:0.0, y:0.0} as the top left and {x:1.0, y:1.0} as the bottom right. Thus, {x:0.3, y:0.6} specifies a position just below and to the left of center, and {x:1.0, y:0.5} specifies a position centered vertically at the far right.

Since the Clear button is located on the right portion of the UITextField, you can set the tapOffset to x:0.8 and y:0.5. This means that the position of the tap is centered vertically at the right portion of the UITextField.

Advertisements
This entry was posted in Uncategorized and tagged , , . Bookmark the permalink.

One Response to iOS UI Automation: Tapping the Clear Button in UITextField

  1. Pingback: iOS UI Automation: Typing A String in a UIATextField | Jojit Soriano's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s