User defined functions (UDF) are the functions which are created by users as per their own requirement. In order to use a UDF, it is necessary to call the appropriate package file at the beginning of the program. The header file informs the program of the name, type, and number and type of arguments, of all of the functions contained.

An UDF function is accessed by simply writing the function name, followed by a list of arguments, which represent the information being passed to the function. The arguments must be enclosed in parentheses, and separated by commas: they can be constants, variables, or more complex expressions.

Automation Best Practices: Avoid logics when writing automation test scripts. Every logic is to be maintained in the function libraries and only be called with their name in the test scripts. Every arithmetic calculation, date calculation, string manipulation, etc. should be avoided in the Test scripts rather put them into the functions and use them.

Benefits of User-Defined Functions

  • – It can be used in a number of places without rewriting the code.
  • – The code can be made less complex and easier to write.
  • – Parameters can be passed to the function.
  • – Simpler to invoke.

For example:  It is a three steps process to open a URL. First Instantiate a New driver, second Apply an Implicit wait on the driver and third Navigate to URL.  Browser can be any browser; it can be Mozilla, IE or any. It makes sense to create a function for opening a browser which will accept an argument (Browser Type) and it will open that particular browser. This ‘Browser Type’ argument will be driven from the Test Data sheet. To achieve this few more functions are required.

  1. Function One: openBrowser(int iTestCaseRow), it will return a WebDriver
  2. Function Two: getTestCaseName(String sTestCase), it will return refined Test case name
  3. Function Three: getRowContains(String sTestCaseName, int colNum), it will return the row number of the Test case name from the test data sheet.

How to use it…

1) Create a new column (Browser) in the Test Data sheet.


Make some entries in the Constant class for the column numbers:


2) Create a ‘New Class‘ by right click on the ‘utility‘ package then select New > Class and name it as Utils. Now create a Static Method for Initiate Browser in the ‘Utils’ class. This method will have an Argument (TestCase Row) and a Return value (WebDriver).


3) To get the Test Case row from the Test data sheet, it is required to get Test Case name, so that it can be searched in the Test Data sheet. Write a function in ‘Utils‘ class to get the Test Case name. Test case name can be easily get by using “this.toString()“. This function will return the package name and the class name for e.g. ‘automationFramework.UDF_TC@2550036c’. Another function is required to refine the long test case name into UDF_TC.


4) Once Test Case name is captured, it can be used as an Argument for a function that will return the Test case row from the Excel sheet.


5) Create a ‘New Class‘ by right click on the ‘automationFramework‘ package then select TestNG > Create a TestNG Class and name it as UDF_TC.

Note: Take previous executed test case ‘TestNG_Framework‘ and modify its Before Method only. The new test script will look like this:

Isn’t it easy to call functions rather than writing them again and again and increase code complexity.