![]() |
T-Plan
Robot Enterprise Plugin Home |
24 April 2014 |
robot.jar
file from the
T-Plan Robot Enterprise installation folder onto the project class
path.The plugin requires T-Plan Robot 3.2 or higher. To install it:
OPTION 1:
tprdb.jar
file to
the plugins/
directory under the Robot
installation directory. This will make Robot load the classes on
the start up.
OPTION 2:
Include
"<location>/tprdb.jar"
tprdb.jar
file onto the Java class path.
Script Name
Description
Return Value
com.tplan.db.Connect
Create a connection to the specified database.
0 on success
1 when the JDBC driver is not available
2 when the connection fails
com.tplan.db.Execute
Execute a SQL statement.
0 on success
1 when no connection has been created
2 when the statement fails to execute
com.tplan.db.Commit
Commit the transaction. 0 on success
1 when no connection has been created
2 when the commit operation fails
com.tplan.db.Rollback
Roll back the transaction. 0 on success
1 when no connection has been created
2 when the rollback operation failscom.tplan.db.Disconnect
Disconnect from the database.
Always returns 0
The plugin scripts are to be called from TPR test scripts using
the Run
command. The command instances may be easily created using
the Command
Wizard tool. To edit an existing Run command right click it
and select Properties in the context menu.
To establish a database connection you will need to
download the JDBC driver and find out the driver class name and
the database URL:
"C:\Program Files\Microsoft JDBC
Driver 4.0 for SQL
Server\sqljdbc_4.0\enu\help\default.htm"
ojdbc6.jar
file for your DB
versionThe driver is typically distributed in form of a JAR file. To
make it visible to Robot use one of the following alternatives:
plugins/
folder under the Robot
installation directory (requires Robot restart).The plugin by default pools the database connections. It
means that any created connection will exist until it gets closed
explicitly through a call of com.tplan.db.Disconnect
or until Robot terminates. When another script calls the Connect
to the same database it receives the pooled connection instead of
a new one. This has two distinct advantages:
The pooling mechanism is not synchronized. It means that
if two test scripts executing in
parallel connect to the same database they may end up using
the same connection. That's why the pooling mode is not intended
to be used in environments executing multiple automated testing
processes in parallel inside one JVM. To set off pooling pass the
pool=false
parameter to the
call. The connection will not be pooled and it will be closed
automatically after the script finishes unless it gets closed
explicitly earlier.com.tplan.db.
Connect
The JDBC framework by default creates every new connection with
the activated auto-commit mode. This means that each
individual SQL statement is treated as a transaction and is
automatically committed right after it is executed. To suppress
this behavior pass the autocommit=false
parameter to
the
call and then
manage the transactions from the script using the com.tplan.db.
Connectcom.tplan.db.Commit
and com.tplan.db.Rollback
calls.
All plugin scripts except for the Disconnect
one
handle any experienced DB errors as follows:
_DB_ERROR
variable is populated with the
error description.makefail=false
parameter. When using this
mode it is recommended to test the return value of each DB call
and take any appropriate action when a failure is detected.To query data using the SQL SELECT statement use the com.tplan.db.Execute
script. The resulting data is exposed to the calling test script
in form of variables:
Variable Name
Description
_DB_COLUMN_COUNT
Number of columns resulting from the SQL SELECT statement.
_DB_ROW_COUNT
Number of rows resulting from the SQL SELECT statement. _DB_VALUE_<row>_<column>
The value at the specified row an column. For example, the _DB_VALUE_2_3 variable
will contain the third value of the second row of data.
derby.jar
correctly.# Load the JAR file with the JDBC driver class for Java DB
Include "C:\Program Files\jdk1.6.0_31\db\lib\derby.jar"
# Connect to the database
Run "com.tplan.db.Connect" driver="org.apache.derby.jdbc.EmbeddedDriver" url="jdbc:derby:testdb;create=true"
# Create the "dummy" table and populate it with data
Run "
com.tplan.
db.Execute" statement="create table dummy (pid int, Name varchar(20))"
Run "
com.tplan.
db.Execute" statement="insert into dummy values (1, 'Value')"
Run "
com.tplan.
db.Execute" statement="insert into dummy values (2, 'Another value')"
# Query the table content
Run "
com.tplan.
db.Execute" statement="select * from dummy"
# Iterate over the data and write the values into the execution log
for (row=1; {row} < {_DB_ROW_COUNT}+1; row={row}+1) {
for (column=1; {column} < {_DB_COLUMN_COUNT}+1; column={column}+1) {
Log "Value at [row, column] = [{row}, {column}]: \"{_DB_VALUE_{row}_{column}}\""
}
}
# Close the database connection.
Run "
com.tplan.
db.Disconnect"
The following example connects to a MS SQL Server database:
# Load the JAR file with the JDBC driver class for SQL Server DB
Include "C:\Program Files\Microsoft JDBC Driver 4.0 for SQL Server\sqljdbc_4.0\enu\sqljdbc4.jar"
# Connect to the database
Run "com.tplan.db.Connect" driver="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://localhost\SQLEXPRESS;DatabaseName=<dbName>;UserName=<SQLusername>;Password=<SQLpassword>
The following example connects to the default Oracle database
(orcl) running on the local machine and queries names of all
tables:
After the scrips gets executed successfully the variables can be viewed in the Execution Vars table:
// The Include commands presume that the JAR files are in the
// same folder as this script
Include "tprdb.jar"
Include "ojdbc6.jar"
// Connect to locally running Oracle as system
Run "com.tplan.db.Connect" driver="oracle.jdbc.driver.OracleDriver" password="oracle12" user="system" url="jdbc:oracle:thin:@//localhost:1521/orcl"
// List all table names
Run "com.tplan.db.Execute" statement="select table_name from tabs"