< All Topics

Katalon studio is an automation testing tool which is created as a wrapper around various famous open source tools like Selenium and Appium. Katalon is capable of automating Web, Mobile, Desktop Apps and API’s etc. but it does not have an inbuilt support for Visual Testing. For Visual Testing, external tools like Imagium are needed. In this article you will be able to learn on how Imagium can be integrated with Katalon for free AI powered Visual testing.

Test Case

import groovy.swing.factory.ComboBoxFactory
import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testng.keyword.TestNGBuiltinKeywords as TestNGKW
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import internal.GlobalVariable as GlobalVariable
import java.nio.file.Files as Files

//Open Browser 
WebUI.openBrowser('')

//Navigate to URL 
WebUI.navigateToUrl('https://www.google.com')

//Get base64 Image 
WebUI.takeFullPageScreenshot([], FailureHandling.STOP_ON_FAILURE) 
String path = WebUI.takeFullPageScreenshot('D:\\Tools\\test.png')

//Get the image in base64 format 
String base64 = CustomKeywords.'com.blobai.imagium.ai.getBase64'(path);

//Disable SSL verification - This is optional
CustomKeywords.'com.blobai.imagium.ai.disableSSLVerification'();

//Imagium Integration// //Get UID - For details on API's check
//Arguments: Testname, Project Key, GetUID endpoint
String testRunID = CustomKeywords.'com.blobai.imagium.ai.getUID'("Test Katalon", "03c0a45b-82e8-4510-bac2-291b1fa9b63d", "https://localhost:44356/api/GetUID");

//Make a validation 
def message = '{ "StepName": "Step 1", "TestRunID": "'+testRunID+'", "ImageBase64": "'+base64+'"}';
String tempUID = CustomKeywords.'com.blobai.imagium.ai.PostRequest'(message, 'https://localhost:44356/api/Validate');

Add following library under Keyword (ai.groovy)

package com.blobai.imagium

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import static com.kms.katalon.core.testobject.ObjectRepository.findWindowsObject
import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import com.kms.katalon.core.windows.keyword.WindowsBuiltinKeywords as Windows
import groovy.ui.Console
import internal.GlobalVariable
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.cert.X509Certificate;
import java.nio.file.Files as Files;

public class ai {
  
	@Keyword
	public static String getUID(String TestName, String ProjectKey, String UIDEndPoint) {

		def message = '{  "TestName" : "' + TestName + '", "ProjectKey" : "' + ProjectKey +  '"}';
		String tempUID = PostRequest(message, UIDEndPoint);
		return tempUID.replaceAll("\"", "");
	}

	@Keyword
	public static String disableSSLVerification() {
		TrustManager[] trustAllCerts = [
			new X509TrustManager() {
				public java.security.cert.X509Certificate[] getAcceptedIssuers() {
					return null;
				}
				public void checkClientTrusted(X509Certificate[] certs, String authType) {
				}
				public void checkServerTrusted(X509Certificate[] certs, String authType) {
				}
			}
		];

		// Install the all-trusting trust manager
		SSLContext sc = SSLContext.getInstance("SSL");
		sc.init(null, trustAllCerts, new java.security.SecureRandom());
		HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

		// Create all-trusting host name verifier
		HostnameVerifier allHostsValid = new HostnameVerifier() {
					public boolean verify(String hostname, SSLSession session) {
						return true;
					}
				};

		// Install the all-trusting host verifier
		HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
	}

	@Keyword
	public static String PostRequest(String Payload, String Endpoint)
	{
		def post = new URL(Endpoint).openConnection();
		def message = Payload;
		post.setRequestMethod("POST")
		post.setDoOutput(true)
		post.setRequestProperty("Content-Type", "application/json")
		post.getOutputStream().write(message.getBytes("UTF-8"));
		def postRC = post.getResponseCode();
		println(postRC);
		return post.getInputStream().getText();

	}

	@Keyword
	public static String getBase64(String ImagePath)
	{
		File file = new File(ImagePath);
		byte[] bytes = Files.readAllBytes(file.toPath());
		String base = Base64.getEncoder().encodeToString(bytes);
	}

}

Add following Library

Previous 11. Jira integration with Imagium
Next 13. Storybook Integration For Component Testing
Table of Contents