WinRunner Tutorial: 6 Programming Concepts You Need To Know By Danna Henderson In order to create robust, efficient, and reusable WinRunner scripts, you will need to know these basic programming concepts. The six topics covered in this WinRunner tutorial should give you a basic understanding of what each is used for, the benefits of using it, and how it might be used with WinRunner. This is not a comprehensive WinRunner tutorial. It's simply designed to introduce these these concepts and give you a general idea of how to apply them with TSL (Test Script Language). Variables Variables are one of the most basic programming concepts and are heavily used in all programs. A variable is essentially a character or string of characters that represents another string. For example, let's say your data table's path is: "C://WinRunner//MortgageApplications//Mort1//DataTables//Apply_Online.xls". If you had to specify the full path each time you reference the data table, your WinRunner code might look something like this:
Notice how the script looks cluttered? The table paths are so long that you have to scroll around to view the code. Now, lets change the table path into a variable and see how it affects the code.
The new code looks cleaner and is more easy to read. It's also easier to maintain. If you table path changes, you only need to update the path in one place, rather than throughout the entire script. The above example also contained another variable. In the "for loop", which will be explained later, the variable "i" is used to represent the current table row. A variable can be any character or string of characters. It's best to pick a variable that describes it's purpose. When you do this, your code becomes more readable and can be modified by others without a lot of research. The different types of variables are beyond the scope of this WinRunner tutorial. For more information on variables, refer to Mercury Interactive's WinRunner manuals or online help. If/Else Another common programming concept is the if/else. It works exactly like it sounds. It tells your program or script "if this condition exists, do this or else, do that". Let's look at how this works with WinRunner:
As you can see, if/else checks are simple, but can be very useful. They don't need to be used together. You can use an "if" by it's self. Or you can use an "if" and then an "else if". When you run into a scenario, where you need multiple if checks in a row, consider using a switch statement, which is more efficient. Arrays Arrays are used to split a string into subsections. Let's look at an example of how WinRunner can use arrays. In this example, we'll split a string of colors, separated by a comma. Then we'll take components of the array and use them to populate our application, which has a field for each color.
As you can image, Red will be entered into the Color1 field, Blue into the Color2 field and so on. Arrays are very useful and can help you create advanced WinRunner scripts. Switch A switch or case statement is useful if you have a variety of conditions that may be true and need to execute a different block of code for each condition. Let's look at an example of how you might use a switch statement in WinRunner.
In this block of code, we are using the action variable to determine if we should click on the Save, Update, or Delete button. A switch statement can help you avoid multiple if checks. Loops Loops are essential essential for efficient and maintainable code. Let's say you are working on an insurance application and need to populate the driver information for 5 drivers. You could repeat a slightly modified block of code five times, or you could create a for loop and use the same block of code for all drivers. To give you a good idea of the value of loops let's look an example of redundant code and then see how a loop can help create efficient code. First, this is how WinRunner might populate the driver data for each of the five drivers (keep in mind, there may be test cases with more or less drivers).
Did you notice the redundancy? Now, let's take a look at how WinRunner can accomplish the same task by using a simple "for loop".
The second example is easier to read and maintain. If a field changes, it can be updated in one place. The script can accommodate any number of drivers without code changes. In addition to "for loops", you can also use "while loops". The details instructions on on the different types of loops and how to use each one is beyond the scope of this WinRunner tutorial. This WinRunner tutorial is simply designed to introduce you to the use of loops. Compiled Modules and Functions Functions are essential to creating robust scripts. They allow you to reuse code, which saves development and maintenance time. A great example would be a login function. A login function can
be used by all scripts that must first login to the application, before testing a specific functionality. First, let's look at an example of a login function and then we'll see how it's used by a script.
Now we have created a login function, let's look at how a WinRunner script might use this function.
Rather than including the login code at the beginning of each script, this small block of code will use the function to login to your application. If the login functionality of your application changes, you will modify the function and not have to make any changes to the dozens (or hundreds) of scripts that use this function. There are many ways to use functions. They can be included in a scrip or separated into function libraries. For more information on how to use functions in WinRunner, refer to the Mercury Interactive's WinRunner manuals. Note: As a general rule, it's not good to use a pause statement in your scripts, we are simply using them for the purpose of demonstrating a concept without the need for additional explanations. Summary These are not the only programming concepts that can be applied to WinRunner scripts, but they are the most important concepts necessary to create robust, efficient, and reusable automated testing scripts. None of these concepts were covered in detail. The purpose of this WinRunner tutorial is simply to show you how each can be used in WinRunner and how they can be used to create more efficient code. You can find detailed instructions on how to use each of these function in your WinRunner user manuals. Or, if you need a experienced WinRunner professional to assist you in applying these concepts, please contact us.