There are a lot of articles, blog posts and discussions on the web about Coded UI best practices.
Is recording better? Or is it better to write your own code? Or a combination of both? These are just some of the common questions which we can run into, especially from newcomers to Coded UI.
Main question you should ask yourself is – What is my goal?
If your goal is to create simple automation which will be executed only from your machine and no need to be consistent in any way (something like “one-time” testing), then my answer is recording.
Why? Because in half an hour or less, you will have created your automated test cases.
If your goal is to create complex and reliable automation which will be executed from various VMs or local machines, various resolutions, screen sizes, etc. … then you should definitely write own code.
Why? Because in that way you will have complete control over your automation. You will decide which control(s) will be searched (my suggestion is to look for ID first, because it should always be unique). When you cannot find ID for any specific control (submit button or whatever), my suggestions is to use parent – child relationships. Of course, you first need to find a parent with ID or some unique control. Will spend more time on this in separate post, but please look to the following example for better understanding.
public void EnterValueFriendlyNamebyParentDiv(UITestControl parent, string id, string Fname, string value)
{
// first we search for parent div that have ID
var parentdiv = new HtmlDiv(parent);
parentdiv.SearchProperties.Add(HtmlDiv.PropertyNames.Id, id);
// now searching for input field inside of that div with specific friendly name
var enterv = new HtmlEdit(parentdiv);
enterv.SearchProperties.Add(HtmlEdit.PropertyNames.FriendlyName, Fname);
// enter whatever is defined as value
enterv.Text = value;
}
One more powerful fact that supports hand-coding is C#. Every method/assertion/whatever-you-need that doesn’t exist as part of Coded UI, can be programmed with C#. Basically there is no way that you cannot find any control, or handle any error, or solve any problem, if you know C# basics, you can solve it. Otherwise consider Google as you best friend. C# has a big and open community and will offer you a lot of answers for possible problems.
Why is recording not reliable? When you use recording and Coded UI cannot find any unique controls, it will use points (X, Y coordinates). That is the worst possible way to find controls, because on different resolutions/screen sizes Coded UI will hit the wrong point on the screen. Only if you’re lucky, during execution it will find controls on your machine (machine on which test is recorded). Also, beside points, another bad way to find controls is Tag Instance, which Coded UI like to collect during recordings. Tag Instance is bad because it can be generic.
Combination of recording and hand coding.
Again, whether this is the best choice for you depends on what you want to achieve. One thing that I would like to point out is to be careful with editing of recorded actions. When you record some action and you want to edit something related to that action in UIMap (add search control, for example) you need to know that after your next recording all your editing will be lost, because Coded UI reverts everything to its originally generated state after every recording.
Conclusion
No, Recording is not useless, it has its own uses over hand-coding. It takes less time for creation, and doesn’t require any coding background. However, as I said, if you want reliable automation which will not break without reason in more than 50% of cases, you need to go deeper and learn some basic coding.