Skip to main content

Running Groovy Script In Katalon With LambdaTest Selenium Grid


As you perform testing with Katalon Studio, you can write or edit test automation scripts in the Script mode using Groovy programming language. This tutorial will help you run your Groovy script in Katalon with LambdaTest Selenium Grid.

Step 1: Open your Katalon Studio instance & go to Tests Explorer from the left side-bar.

Step 2: Right-click on the Test Listener folder & click on the new listener.

Step 3: Name the new listener & paste the below code in the new listener.

import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpointimport static com.kms.katalon.core.testcase.TestCaseFactory.findTestCaseimport static com.kms.katalon.core.testdata.TestDataFactory.findTestDataimport static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.checkpoint.Checkpoint as Checkpointimport com.kms.katalon.core.model.FailureHandling as FailureHandlingimport com.kms.katalon.core.testcase.TestCase as TestCaseimport com.kms.katalon.core.testdata.TestData as TestDataimport com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WSimport com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUIimport com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.annotation.BeforeTestCaseimport com.kms.katalon.core.annotation.BeforeTestSuiteimport com.kms.katalon.core.annotation.TearDownimport com.kms.katalon.core.annotation.AfterTestCaseimport com.kms.katalon.core.annotation.AfterTestSuiteimport com.kms.katalon.core.context.TestCaseContextimport com.kms.katalon.core.context.TestSuiteContext
import com.kms.katalon.core.webui.driver.ExistingRemoteWebDriver
import com.kms.katalon.core.configuration.RunConfiguration
class LambdaListener {
    TestSuiteContext suiteContext;
    /**     * Executes before every test case starts.     * @param testCaseContext related information of the executed test case.     */    @BeforeTestCase    def sampleBeforeTestCase(TestCaseContext testCaseContext) {                        //To set the test name at LambdaTest.        RunConfiguration.setDriverPreferencesProperty("Remote", "name", testCaseContext.getTestCaseId());        if (suiteContext != null){            RunConfiguration.setDriverPreferencesProperty("Remote","build", suiteContext.getTestSuiteId());        }        println testCaseContext.getTestCaseId();        println RunConfiguration.getDriverPreferencesProperties();    }    /**     * Executes after every test case ends.     * @param testCaseContext related information of the executed test case.     */    @com.kms.katalon.core.annotation.TearDown    @AfterTestCase    def sampleAfterTestCase(TestCaseContext testCaseContext) {        //To set the status of test at LambdaTest.        String result="failed";        if(testCaseContext.getTestCaseStatus().equalsIgnoreCase("PASSED")){            result="passed"        }        try{            WebUI.executeJavaScript("lambda-status="+result,null)        }catch (Exception e)        {            println e.toString()        }        finally {            WebUI.closeBrowser()        }        println testCaseContext.getTestCaseId()        println testCaseContext.getTestCaseStatus()    }
    /**     * Executes before every test suite starts.     * @param testSuiteContext: related information of the executed test suite.     */    @BeforeTestSuite    def sampleBeforeTestSuite(TestSuiteContext testSuiteContext) {        suiteContext=testSuiteContext        //To Set the build Name at LambdaTest.        RunConfiguration.setDriverPreferencesProperty("Remote","build", suiteContext.getTestSuiteId());        println testSuiteContext.getTestSuiteId()    }
    /**     * Executes after every test suite ends.     * @param testSuiteContext: related information of the executed test suite.     */    @AfterTestSuite    def sampleAfterTestSuite(TestSuiteContext testSuiteContext) {        println testSuiteContext.getTestSuiteId()    }}

That is all. Now, you can effortlessly run Groovy Scripts in Katalon for triggering your tests over LambdaTest cloud-based cross browser testing platform. Happy testing! ๐Ÿ™‚

Last updated on