Sunday, November 06, 2011

Coded UI tests : AutomationId or how to find the chose one (control) !

Content of the post
  1. AutomationId : what is it ?
  2. How to set it ?
  3. How to use it in automation clients ?
  4. How is it used by the Coded UI tests builder ?
  5. How to find it on existing application ?
  6. When to set the AutomationId ?
  7. AutomationId and ItemsControl or dynamically generated controls.
  8. More to read on the subject

AutomationId : what is it ?^

As stated in the previous post, an application exposes AutomationPeer. They are then found by UI Automation Client as AutomationElement objects. These items are identified uniquely by the AutomationId property.

This identifier is not mandatory but it’s a best practice to set it when you plan to perform UI tests.

To retrieve a control, the automation peer tree is used : first the top element is found, then the automation client iterate on its children, then the children of the children until it finds the aimed element. Each of the children and the final target is find using conditions and usually, one is set on the automation id.

The automation id is a property which helps you to find a specific UI element among others. This is, for example, very useful when you want to find a specific item in a collection (listbox, menus, etc.). Another benefit is that, as the control is uniquely identified, you can move it elsewhere or change its name and it will still be found by a previous code needing it.

To leverage the power of it, there is a rule to respect: it’s expected to be the same in any instance of the same application and ‘unique by instance in its scope‘.

    If you exit the application and launch it again, the value will not change.
    If you change the culture of the PC/Application, it will not change.
    If you launch several instance of the application, it remains the same on each of them.
    If there is several usage of the control in the application, each of them has a different automation id on each scope. A scope is defined by the path in the tree to go to the element. For example each menu item of a menu can be named in a generic way : menuItem1, menuItem2, etc.

This is a rule and you are not forced by the runtime or anything to follow it. As a proof, AutomationPeer returns a default value of string.empty which is really not unique!

By the way, if you want to set a name to a control(for example to improve accesibility) uses the Name property instead of the AutomationId one.

Read more: Yet another blog about…
QR: https://chart.googleapis.com/chart?chs=80x80&cht=qr&choe=UTF-8&chl=www.jonathanantoine.com/2011/11/03/coded-ui-tests-automationid-or-how-to-find-the-chose-one-control/

Posted via email from Jasper-Net