Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In addition to built-in keywords, users can define custom keywords to extend the capability of Katalon Studio. Once created, custom keywords can be used when implementing test cases just like other built-in keywords. 

Create a Package

A package allows you to group custom keywords into different categories. 

  1. Select File > New > Package from the main menu. The New Keyword Package dialog will be displayed. Enter the name for your package and click click OK.


  2. A new package is created under Keywords of Katalon Studio accordingly.

 

Create a Custom Keyword

  1. Select File > New > Keyword from the main menu. The New Keyword dialog will be displayed. Enter the name for your keyword and specify

    a

    package

    for

     for the keyword.

    Click

    Click OK.

    Image Removed
    Note

    Since version 5.4, Katalon Studio provides user option to generate sample custom keywords for Web, Mobile and API Testing.

    Refer to this guide: https://docs.katalon.com/x/3QHR


    Image Added

  2. A new keyword is created under the specified specified package accordingly.


  3. Enter the following block of code in your class to define one custom keyword:

    Code Block
    languagegroovy
    themeRDark
    @Keyword
    def keywordName(parameters…) {
       // enter your code here
       // you can use either Groovy or Java      
    }

    where:

    ItemDescription
    @KeywordThe annotation to indicate that the block of code below is the definition of a keyword.
    keywordNameThe name that you would like to use for your custom keyword
    parametersThe list of parameters that would be used in the custom keyword

    For example:

  4. Save the Keyword file when you're done.

Custom keywords in Manual view

Follow the steps below in order to use your defined custom keywords in in Manual view of  of a Test Case:

  1. Open a test case in in Manual view, then select to add add Custom Keyword from  from command toolbar.


  2. A new test step is added with a list of all defined custom keywords. Select your recently created keyword here.

 

Custom keywords in Scripting view

Follow the steps below in order to use your defined custom keywords in in Scripting view of  of a test case:

  • The The Class CustomKeywords of Katalon Studio allows you to access all custom keywords. Enter the following syntax into the script editor:

    Code Block
    languagegroovy
    themeRDark
    CustomKeywords.
  • The The Content Assist function  function will be invoked immediately after you type the the dot character character. Content Assist provides users with the context-sensitive suggestions for code completion. Therefore, all the custom keywords defined in your test project will be displayed as below:

  • Select the recently created keyword and provide all parameters as required.

The following API docs may prove useful when working with custom keywords:

ClassMethodDescription
Driver FactorygetWebDriver()Get the current active web driver.
Test ObjectaddProperty(String name, ConditionType condition, String value)Add a new property to the test object
setProperties(List<TestObjectProperty> properties)Set the properties of the test object
getObjectId()Get object ID.
findPropertyValue(String name, boolean caseSensitive)Find the value of a property using the property name
Keyword UtillogInfo(String message)Log message as info
markError(String message)Mark a keyword to be error
markErrorAndStop(String message)Mark a keyword to be error and stop execution
markFailed(String message)Mark a keyword to be failed and continue execution
markFailedAndStop(String message)Mark a keyword to be failed and stop execution
markPassed(String message)Mark a keyword to be passed
markWarning(String message)Mark a keyword to be warning

 

Handle Databases

With custom keywords, you can connect to a database as well as perform other data queries. 

Below is a sample demonstrating how to create custom keywords for:

  • Establishing database connection
  • Executing data query
  • Closing the connection

 

Code Block
languagegroovy
themeRDark
private static Connection connection = null;

	/**
	 * Open and return a connection to database
	 * @param dataFile absolute file path  
	 * @return an instance of java.sql.Connection
	 */
	@Keyword
	def connectDB(String dataFile){
		//Load driver class for your specific database type
		Class.forName("org.sqlite.JDBC")
		String connectionString = "jdbc:sqlite:" + dataFile
		if(connection != null && !connection.isClosed()){
			connection.close()
		}
		connection = DriverManager.getConnection(connectionString)
		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
	 */
	@Keyword
	def executeQuery(String queryString) {
		Statement stm = connection.createStatement()
		ResultSet rs = stm.executeQuery(queryString)				
        return rs
	}

	@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
	}

You can add the sample code above to your keyword file and modify the details as appropriated. 

 

Table of Contents
classkat_toc