(MS) UIAutomation: putting text into a control


Task: demonstrate how to write in text into a text box field.

Requirements: use UI Automation pattern or Win32.

Solution: the typical code is as follows:

Get-UIAWindow -Name $formTitle | `
 Get-UIAEdit -AutomationId UsernameBox | `
 Set-UIAEditText -Text "text";

Despite the name, the Set-UIAEditText cmdlet uses ValuePattern. This is because that Set-UIAEditText as well as any Set-UIA[ControlType]Text cmdlet is an alias of the Invoke-UIAValuePatternSet cmdlet.
the same story with the Set-UIATextBoxText cmdlet, which is the alias for Set-UIAEditText.
Not every time controls that are supposed to support ValuePattern really support it. This may depend on the developer’s code or on a control’s state. For example, a combo box supports ValuePattern if it has a field to write text in.

As promised, accessing such controls programmatically should raise the ModifiedChanged event.

Sometimes, the ValuePattern can be inaccessible. Due to many reasons, it happens, though rarely. Under these circumstances, you may use the Set-UIAControlText cmdlet, the pure Win32 text setter.

Get-UIAWindow -Name $formTitle | `
 Get-UIAEdit -AutomationId UsernameBox | `
 Set-UIAControlText -Text "text";

Notice, however, that it works slightly differently: it puts text before the text that is already in the text field. What means that you need first to clean it up, the control, before putting a new text in.

Advertisements

2 responses

  1. > you need first to clean it up, the control
    But how?
    Invoke-UIAControlClick -DoubleClick?
    [System.Windows.Forms.SendKeys]::SendWait(“{DELETE}”)?

    1. Hi Alex,
      first of all, how to do this task in an easy way (i.e., using ValuePattern). I created a simple Windows Forms application called ‘testeditbox’ with a label and a textbox (the label is not used in the test, just a typical case textbox + label).
      We can write in and, after, clean it the following way:
      Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAEditText texttexttext;
      Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAEditText “”;
      Now, we are going to perform this without using the pattern. There are two ways to put text in: Set-UIAControlText and Set-UIAControlKeys.
      The first method can’t send special characters (it’s just my fault, I need to add the possibility), it works through the SendMessage API call. It should work pretty well as it sends to a handle.
      The second is .NET SendKeys.
      The problem is that SendKeys sends only to the focused control. Well, I achieved the result this way:
      Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAFocus;
      Get-UIAEdit | Set-UIAControlKeys “text2”;
      Get-UIAWindow -n testeditbox | Get-UIAEdit | Set-UIAFocus;
      Get-UIAEdit | Set-UIAControlKeys “^(A){DELETE}”;
      Step by step guide:
      1) get a control
      2) set focus
      3) get a control again (the Set-UIAControlKeys requires input), the control is foreground now
      4) put keys to the control (Ctrl+A and Delete).

      If your control of some other sort, you may need to do other tricks. Nonetheless, the idea behind is as I demonstrated.

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

%d bloggers like this: