HP Unified Functional Testing (UFT) is an automated testing tool to automate actions against GUI’s or API’s.
In order to connect to a database you either have to request a checkpoint or output value for database type in the Design menu as shown below:
However, what if we want to save the value in a temporary variable to insertion into a text-element on a GUI or submit to an API function?
We can use Visual Basic (psuedo at least)!
We start with declaring our variable in Editor view of our Action by toggling this button:
Then using VB we can declare the variable and call a function to set it’s value:
Dim VariableX VariableX = GetDBValue(inputX)
Where GetDBValue() will be the function shown below to initiate a connection and execute our string sql statement. The variable declaration above must be placed before the function below, and before you run something like:
The following image is what the function looks like whilst completed in UFT (with XXX to be replaced with your details):
If you want to copy & paste without the formatting, here is the code snippet of the image above.
Function GetDBValue(inputX) Set connX=CreateObject("ADODB.Connection") Set recSetX = CreateObject("ADODB.Recordset") 'Insert MachineDataSource(e.g DB2 ODBC),Username,Password,Alias connX.open ="DSN=xxx;UID=xxx;PWD=xxx;DBALIAS=xxx;" 'Construct our query, reminder to use '' for added security sqlStatementString = "select xxx from XXX.xxx where xxx = '" + inputX + "'" recSetX.open sqlStatementString, connX 'Execute query! Value = recSetX.fields.item(0) 'Select first element found (you can interate this!) recSetX.Close 'Close the result listener connX.Close 'Close the database connection GetDBValue=Value 'Output function End Function
I have added comments so you understand the functions of the call, the only difference to Visual Basic is there is no return but the return is calling the function as a variable at the end with the output value you want, UFT will read it as the GET result of the function. Simply call the function with FunctionName(Inputs,Inputs2) etc and you can GET,SET or simply execute void like any other Visual Basic function.
Hope this helps someone looking for a quick easy way to talk to a database within UFT.
Stay tuned for more software tips, reviews, coding projects and the like.