Telerik blogs

Greetings, fellow testers. There's been a lot of talk recently about testers learning to code. I'll say this much here — I think it's important for those of us who are testing software to understand how software works. For some, that means being able to read code. For some, that will turn into an interest or passion for writing code. Some of that code will be automating existing tests, some of it will be automating other tasks that we perform every day — creating data, cleaning up after tests are complete, reporting on our findings, etc.

With regard to automating User Interface tests, there will often be tests that you find yourself wanting to perform that just aren't reasonably done using record-and-playback alone. Test Studio gives you the flexibility you need in these cases with Coded Steps. Let's look at an example, and how a little bit of code can make testing much easier.

Here's a fairly standard view for most of us, a view into our email. We'd like to test multi-select and delete functionality, and we want to do more than just blindly select the first items from the list. We've decided that we'll delete all the messages that were received on Wednesday.

table from an email application, showing the inbox

If the items are always displayed in the same order, that's easy to automate. Any record-and-playback tool can be used to pick the first, fifth and ninths items in this list. That's unrealistic and exactly the sort of thing that causes automation to be considered "brittle." Any change in sort order or the data itself will quickly lead to changes being required in the tests, exactly what we don't want.

What we'd really rather do is something like this: look at each row in the table displayed; if the row has a date of Wednesday, check the box. As it turns out, that's an easy set of steps to turn into instructions for the computer. Test Studio can tap into the power of the .NET framework using either C# or VisualBasic; Here I'm going to use C#.

"look at each row in the table displayed" - this becomes a foreach loop. The specific page name will vary depending on your application's page names. Test Studio gives you and your developers the same sort of Intellisense as Visual Studio, showing you the methods and properties of each of your objects as you need them.

foreach (HtmlTableRow r in Pages.TelerikSample0.InboxTable.Rows)

"if row has a date of Wednesday" - since we know how our application is built, we know that the date will always be in the third column. Counting from 0, that's cell number two:

HtmlTableCell c = r.Cells[2];

"check the box" - this one is takes a little more work, as we need to get to the checkbox inside the table's cell:

HtmlTableCell c1 = r.Cells[0]; // look at the first cell in the row
HtmlControl checkb = c1.ChildNodes[0].As<HtmlControl>(); // find the checkbox within the cell
checkb.MouseClick(); // check the box. 

Broken down like that, it's pretty simple. If you've got experience with a programming language and objects, this should look somewhat familiar. If not, it's easy to show a developer the steps you need and ask for a little assistance. Testers can learn a bit of programming, and programmers can learn a bit of what testing looks like.

the coded step, shown in Test Studio

In just a few lines, we've given our test considerable flexibility and power — it can check off as few or as many email messages as exist on the page. If you look closely at the test in the screenshot here, you'll see that I need to make the verification in step 5 more flexible as well, but I think this gives you an idea of what can be done. We've also got a number of code samples in the documentation, to whet your appetite.

Of course, if your developers get the itch to get deeper into UI automation, Test Studio's Visual Studio Plug-in will let them work in the environment with which they're most familiar.


About the Author

Steven Vore

Steven Vore

Steven Vore is an Evangelist for Telerik's Test Studio. He has worked in software support and testing for the better part of two decades, and enjoys exploring ways to make software easier to use. He is a fan of movies and music, and can often be found on Twitter as @StevenJV.


Comments are disabled in preview mode.