Blog to understand automation concepts in QTP, Selenium Webdriver and Manual Testing concepts

Coding Standards: Best Practices in QTP

 Any good automation code needs guidelines or coding standards to be followed to be readable, presentable and at the same time ensure it is executable. Coding standard brings in the good rules being followed across projects into the project and hence adds value to the project. It also decreases the maintenance cost, as it is well documented and readable.


General Guidelines

·         Use Option Explicit.
·         Use an ampersand (&) for concatenating strings instead of ‘+’.
·         Set the objects to nothing for cleaning the memory.
·         Declare only one variable in a line.
·         The code should be properly indented.
·         Framework code should be designed and developed with minimal possible loops and conditions for reduced complexity and enhanced maintainability.
·         Modular code for increased re-usability and reduced redundancy.
·         Code should be well indented with tabs. (Tab width should be 4).
·         Values passed and returned to the functions should use simple variables.
·         Reduce the use of global variables within the function. The scope of the variable should be decided based on the standards.

Commenting standards

Comments help to make the code more maintainable. Comments should be used extensively to explain the logic behind most statements. One space should be used between the two slashes. For extensive explanations, an indented section with a meaningful name should be used. Comments for each line are not required.  Instead, use comments for groups of closely related lines or whenever the code is confusing or may be difficult to understand. Comments on blocks of code are preferable and should express insights and explanations of the code that may not be obvious by looking at the code.  

Code comments

·         Comments need to be included for all complex pieces of code
·         Comments inside a code should follow the same indentation as the code
·         If the code block inside braces is long, the ending brace should carry comments indicating closure of which loop
·         Significant lines in the code should be provided with inline comments to better explain the line of code's purpose and make it easier for subsequent developers to understand the code faster and more thoroughly

Indentation

·          Indentation should be used to clearly define and allow easy access to different sections of the scripts, loops, conditional statements and Functions/Sub Routines, etc. 
·         One Tab (4 spaces) spacing should be maintained in Loops.
·         Comments inside a code should follow the same indentation as the code

Hard coding

Hard coding has to be avoided at all possible scenarios. Particularly hard-coded paths and machine names should be avoided in test scripts. This makes them very difficult to move from one machine or environment to another. Instead all those will be kept into excel file or environment variables and can be used in the scripts as variable. This would be very helpful when we have many environments where the script has to be run.

Suppress user messages

While coding we have a tendency to use message boxes and input boxes for the purpose of debugging. We should ensure that unnecessary user messages should be suppressed. – Delete or comment out all debug user messages, trial code routines. This will ease the batch runs for scripts and ensure proper execution of script.