Monday 6 May 2013

QTP- Best Coding Practices

Naming Conventions


The following guidelines would be applicable to all - file, action and variables names –
  • Each name will use a predefined prefix to identify the object or variable type. 
  • The first character following the prefix in a name will be an upper case letter.
  • The use of upper and lower case letters, numbers, and underscores in names are encouraged to improve readability.
  • While names may be up to 40 characters in length, developers are encouraged to use the minimum length possible without sacrificing readability. Name lengths in the range of 9 to 15 characters are considered optimal.
  • Names should be chosen that are clear and unambiguous.
  • Names should reflect the real world nature of the object rather than use computer or data processing terms.
          Prefix of the variable or object should be as below-

Subtype
Description
Prefix
Example
Boolean
Contains either True or False.
Bln
blnIsUSCititzen
Integer
Contains an integer in the range -32,768 to 32,767.
Int
intNumberOfDirectReports
String
Contains a variable-length string. Strings can be made up of any alphanumeric characters.
Str
strUserLastName
Object
Contains an object reference. An object variable represents an Automation object.
Obj
objExcelSpreadsheet
Array
Contains an array of variables. Because arrays are designed to hold multiple objects, array names should always be plural. To maintain consistency with the other naming conventions, arrays should have two prefixes: arr to indicate the array, and a second prefix to indicate the data type.
Arr
arrstrUserAccountNames
Collection
Technically, a collection is not a variable subtype. However, it is listed in this table because you should use the col prefix to indicate collections. Collections are used extensively in system administration scripting.
Col
colInstalledApplications


























Script Naming Conventions
Test cases should be named in a manner that enables easy identification of the corresponding use case (or the scenario) for which the Script is written. The onlooker should be able to trace the script by the name of the test case.

For example: 
Naming convention is <Module Name>_<Test Case ID>_<Test Case number> 
Without grouping: DC_XY_1 (If script is mapped to one test case)
Grouping: - DC_XY_3_4 (If script is mapped to more than one test case)


Function Library Naming Conventions

Function name should be prefix with Func_(relevant name). This will provide clarity and difference between a Script and a Function.
Function abbreviation (Func) starts with ‘F’ in uppercase letter then suffixed by the logical name of function.  Suffixed function name will start with first letter in uppercase followed by underscore “_” and then followed by second word (starting first letter in uppercase again). 

For example:- Func_Search_In_Pdf, Func_Object_Wait
Library files which are stored as .vbs extension in QTP follows the same naming convention as described for functions.


Coding Practices


  • For each validation, a Reporter Event (Pass or Fail) should be used. In Fail condition, Actual and Expected value should be print in the result.
  • If the script does not require further execution after the failed step, use the ‘ExitAction’ or ‘ExitTest’ statements.
  • For failed step, script should capture screen shots.
  • Even if there is just one row in data sheet for the script, script should have ‘For loop’ that will execute the script to the number of row count in test data sheet.
  • Always use ‘On Error Resume Next’ at the start of the function for preventing any run time error.
  • Always write code in ‘Function ..End Function’ block as Function can return a value while Sub block cannot return a value.
  • All the variables those have been used throughout the Framework should be declared and defined in one common library.
  • Each declared COM objects should be released to avoid memory leak.
  • Each variable declared in library should have a significant name with proper commenting.
      
      
     Script Header

     All the script and functions should contain a header block of comments such as the examples below.  Header should have following information-

Author
Action Name
Test Case Description
Input Parameters
Output Parameters
Creation Date
Modified Details




Function Header

The function header is placed above a user-defined function. Like the Test Header, the function begins with the "‘" symbol and stores information about the function:
Functions should describe the purpose of the function. It should also describe I/O arguments. Functions are saved in an external .vbs file. Name the functions in such a way that they indicate their functionality.



Variable Declaration

The most important thing in choosing a name is to establish a clear, easily recognizable pattern to your script so that others will be able to understand your implementation and intent as quickly and reliably as possible. All declared variables must be used. Unnecessary variables lead to memory wastage.

Option Explicit-

In all of the Quick Test automation test scripts, the starting statement should always be “Option Explicit”. This statement forces the automation tester to declare all the variables that are used in the test script. Declaration of Option Explicit will save programming time by reducing the number of bugs caused by typos.
You declare variables explicitly in your script using the Dim statement, the Public statement, and the Private statement. For example: Dim strExpected, intNumber.

Indentation
Indent styles assist in identifying control flow and blocks of code. Indentation is used to delimit logical blocks of code and increases readability. The use of white space around operators and keywords, the capitalization of keywords and variable names, the style and spelling of user-defined identifiers such as function, procedure and variable names and style of comments should be pre-defined.
Use of white spaces for example tab, enter and space bar should be consistent throughout the development.

Relative Paths
All the reusable actions, resources and object repository associated with the script should have relative paths.




Synchronization
Avoid using hard coded wait(x) statement. Wait statement waits for full x seconds, even if the event has already occurred. Instead use .sync or exist statement. For ex: .Exist(10), here QTP will wait max till 10 seconds and if it finds the object at (say) 3 secs , it will resume the execution immediately thereby saving your precious time.

No comments:

Post a Comment