Reusable action with query database
There are two actions. To implement the logic shown in, that is, call querydb in Action1 to implement business operations in Action1 through parameters, the common method is written into querydb. Similar querydb can be called either in this script or in different scripts. To implement such a function, you can use the reusable Action Function of action. I used the Oracle database and table c_cons for the experiment. You can call querydb by entering the user code cons_no to query the user ID: cons_id.
1. Create two actions, named Action1 and querydb respectively. If the querydb reusable action box is selected, the querydb action can be reused.
2. Define the input and output parameters in the querydb action. :
3. Switch to Action1 and call querydb. Right-click and select insert call to existing... in the pop-up box ....", The Select action dialog box is displayed, as shown in. Select the action to be inserted in the action, that is, querydb, and select the "after the current step" option in the location option.
Click OK to insert. At the same time, the script displayed in the experts' attempt is:
Runaction "querydb", oneiteration
To implement input parameters to query results, you need to define a variable that retains the returned value, returnconsid, and enter the cons_no parameter as 3095646333. The script is as follows:
Dim returnconsid
Runaction "querydb", oneiteration, "3095646333", returnconsid
Msgbox returnconsid
4. After the call is inserted, we can write related business logic operations in querydb. The specific implementation code is as follows:
Dim res, CMD, SQL
Set res = Createobject ("ADODB. recordset ")
Set cmd = Createobject ("ADODB. Command ")
Rem Connection database
Dim connstring
Connstring = "provider = oraoledb. oracle.1; Password = sgpm31; persist Security info = true; user id = SGPM; Data Source = cis31; server = 172.19.2.31"
Cmd. activeconnection = connstring
Rem query statement
SQL = "select cons_id from c_cons where cons_no = '" & parameter ("cons_no ")&"'"
Cmd. commandtext = SQL
Set res = cmd. Execute ()
If res. EOF and res. bof then
Res. Close
Reporter. reportevent micfail, "test", "query failed"
Parameter ("cons_id") = "unknown"
Else
Rem CSTR to string
If CSTR (RES (0) <> "" then
Dim returnconsid
Returnconsid = CSTR (RES (0 ))
Reporter. reportevent micpass, "test", "query succeeded." & parameter ("cons_no") & "indicates" & returnconsid
Parameter ("cons_id") = CSTR (RES (0 ))
End if
End if
Set res = nothing
Set cmd. activeconnection = nothing
Set cmd = nothing
In this way, the entire script meets our expected requirements and runs the script to get the results we need.
Note: actions like querydb can be used as a public action. Of course, the scripts in querydb can be called as a public function, this is just to introduce the reusable Action Function of action in qtp. To call querydb in different scripts, you can select the position of the querydb script in the select action dialog box. As shown in: