iOS UI Automation: Typing A String in a UIATextField

When using UIATextField.setValue function to enter a value to a UIATextField, events like UIControlEventEditingChanged will not fire as expected. For it to fire, you need to enter the value using the keyboard. Here’s my code snippet to type the string you pass in the pstrString parameter. You can specify pbClear as either true or false if you want to clear the UIATextField prior to typing your string.

Code Prerequisites:

  1. Clear function in iOS UI Automation: Tapping the Clear Button in UITextField.
  2. UIAKeyboard extension after the typeString code.
UIATextField.prototype["typeString"] = function(pstrString, pbClear)
    if (!this.hasKeyboardFocus())


    if (pbClear || pstrString.length == 0)

    if (pstrString.length > 0)
        var app = UIATarget.localTarget().frontMostApp();
        var keyboard = app.keyboard();
        var intKeyboardType = keyboard.keyboardType();
        var bIsAllCaps = (intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA_CAPS); //Handles autocapitalizationType = UITextAutocapitalizationTypeAllCharacters
        var intNewKeyboardType = intKeyboardType;
        var keys = app.keyboard().keys();
        var buttons = app.keyboard().buttons();
        for (i = 0; i < pstrString.length; i++)
            var strChar = pstrString.charAt(i);
            if ((/[a-z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA_CAPS && !bIsAllCaps)
                intKeyboardType = keyboard.KEYBOARD_TYPE_ALPHA;
            else if ((/[A-Z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA)
                intKeyboardType = keyboard.KEYBOARD_TYPE_ALPHA_CAPS;
            else if ((/[A-z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION)
                buttons.firstWithName("more, letters").tap();
                intKeyboardType = keyboard.KEYBOARD_TYPE_ALPHA;
            else if ((/[0-9.]/.test(strChar)) && intKeyboardType != keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION)
                buttons.firstWithName("more, numbers").tap();
                intKeyboardType = keyboard.KEYBOARD_TYPE_NUMBER_AND_PUNCTUATION;

            if ((/[a-z]/.test(strChar)) && intKeyboardType == keyboard.KEYBOARD_TYPE_ALPHA_CAPS)
                strChar = strChar.toUpperCase();
            if (strChar == " ")
            else if (/[0-9]/.test(strChar)) // Need to change strChar to the index key of the number because strChar = "0" will tap "1" and strChar = "1" will tap "2"
                if (strChar == "0")
                    strChar = "9";
                    strChar = (parseInt(strChar) - 1).toString();

The UIAKeyboard extension code for the keyboard type constants and keyboardType function is as follows:

UIAKeyboard.prototype["KEYBOARD_TYPE_UNKNOWN"] = -1;
UIAKeyboard.prototype["KEYBOARD_TYPE_ALPHA"] = 0;
UIAKeyboard.prototype["KEYBOARD_TYPE_ALPHA_CAPS"] = 1;
UIAKeyboard.prototype["keyboardType"] = function()
        if (this.keys().firstWithName("a").toString() != "[object UIAElementNil]")
            return this.KEYBOARD_TYPE_ALPHA;
        else if (this.keys().firstWithName("A").toString() != "[object UIAElementNil]")
            return this.KEYBOARD_TYPE_ALPHA_CAPS;
        else if (this.keys().firstWithName("1").toString() != "[object UIAElementNil]")
            return this.KEYBOARD_TYPE_UNKNOWN;

Modify the code according to your need. I am sure this code do not cover all keyboard types.

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

4 Responses to iOS UI Automation: Typing A String in a UIATextField

  1. eevenson says:

    In your UIAKeyboard extension, I think you need to replace the this.keys() with something like keyboard.keys(). But I’m no JavaScript expert…

    • Jojit Soriano says:

      @eevenson My apologies. Thanks for pointing that out! 🙂 It’s my mistake when I copied it from my code and modified it to make the snippet independent from my libraries. Since it is a UIAKeyboard extension, the code should instead be UIAKeyboard.prototype rather than UIATextField.prototype. I corrected my post. Thanks again!

  2. eevenson says:

    Ah yes — I see now. My JavaScript is very mediocre, but that make sense and will work better than my “fix.” Best!

  3. eevenson says:

    I did add a conditional (like your clear option) to append a tap on the return key via buttons.firstWithName(“return”).tap();.

Leave a Reply

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

You are commenting using your 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