Katalon Studio enables users to create custom keywords. The primary purpose of this is to address individual/ specific needs. Moreover, with the help of custom keywords, you can connect to databases and perform database testing. Subsequently, this tutorial includes a description of the details on how to create custom keywords for database testing in Katalon Studio. In addition to that, we will discuss the below points in this article, which will help us comprehend the procedure of Database Testing using Katalon Studio:
- How to establish a database connection?
- How to execute a query?
- Finally, how to close the connection?
- Using defined Keywords in Test Cases for DB Testing
Below is a code sample demonstrating how to
- Firstly, establish a database connection.
- Secondly, execute a query.
- Finally, close the connection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
package com.database import java.sql.DriverManager import java.sql.ResultSet import java.sql.Statement import com.kms.katalon.core.annotation.Keyword import com.mysql.jdbc.Connection public class DemoMySql { private static Connection connection = null; /** * Open and return a connection to database * @param dataFile absolute file path * @return an instance of java.sql.Connection */ //Establishing a connection to the DataBase @Keyword def connectDB(String url, String dbname, String port, String username, String password){ //Load driver class for your specific database type String conn = "jdbc:mysql://" + url + ":" + port + "/" + dbname //Class.forName("org.sqlite.JDBC") //String connectionString = "jdbc:sqlite:" + dataFile if(connection != null && !connection.isClosed()){ connection.close() } connection = DriverManager.getConnection(conn, username, password) return connection } /** * execute a SQL query on database * @param queryString SQL query string * @return a reference to returned data collection, an instance of java.sql.ResultSet */ //Executing the constructed Query and Saving results in resultset @Keyword def executeQuery(String queryString) { Statement stm = connection.createStatement() ResultSet rs = stm.executeQuery(queryString) return rs } //Closing the connection @Keyword def closeDatabaseConnection() { if(connection != null && !connection.isClosed()){ connection.close() } connection = null } /** * Execute non-query (usually INSERT/UPDATE/DELETE/COUNT/SUM...) on database * @param queryString a SQL statement * @return single value result of SQL statement */ @Keyword def execute(String queryString) { Statement stm = connection.createStatement() boolean result = stm.execute(queryString) return result } } |
Tips: Press Ctrl + Shift + o to import missing libraries in test scripts automatically. In other words, this will automatically import the missing libraries.
Consequently, the Custom Keywords file will look like the following:
Additionally, you can insert/ add the sample code above to your keyword file. Moreover, you can also modify the details as appropriate. In addition to this, you can also refer to the links mentioned below, to understand the formats of database connection strings:
- For MSSQL: https://www.connectionstrings.com/sql-server/
- For Oracle: https://www.connectionstrings.com/oracle/
Using Defined Keywords in Test Cases for DB Testing
- First, create new custom keywords for the database connection. It is also explained above.
- Second, copy the DB script provided above. After that, paste it into the new keyword editor.
Congrats! You have just created your first project on how to use Custom Keywords for database testing. Conclusively, now you have a fair understanding of database testing using Katalon Studio. Additionally, you are also now equipped to establish a database connection, execute a query, and finally close the connection.