Test case creation in script mode

In this tutorial, we are going to learn, test script creation using Katalon Studio's script mode. In script mode, we use Groovy language to create test scripts. User can directly start writing the test script by clicking the Script tab.

ArtofTesting katalon studio test case
Also, user can record the test script or create test script in manual mode and then convert it into script mode by just clicking the Script tab. As stated in our previous tutorials, Katalon studio inherently supports Keyword-driven framework. It provides inbuilt keywords for WebUI, mobile app and web service automation. Let's first see some commonly used keywords provided by Katalon Studio for automating web applications.

Browser related keywords

KeywordDescription
WebUI.openBrowser('') Opens browser with the URL provided, if no URL is provided then it will just open a blank browser.
WebUI.closeBrowser() Closes a browser.
WebUI.navigateToUrl('https://google.com') Navigates to a url provided in the Input cell.
WebUI.deleteAllCookies() Deletes browser cookies.
WebUI.waitForPageLoad(5) Waits for page to load with the timeout value in seconds.
WebUI.refresh() Refreshes the browser.
WebUI.back() Goes back to previous URL in the browser history.
WebUI.forward() Goes to next URL in the browser history.

Web element related keywords

KeywordDescription
WebUI.click(findTestObject('Page_home/btn_submit')) Simulates mouse left-click on a web element located by the findTestObject method.
WebUI.doubleClick(findTestObject('Page_home/btn_submit')) Simulates mouse double-click on a web element.
WebUI.rightClick(findTestObject('Page_home/btn_submit')) Right clicks on a web element.
WebUI.check(findTestObject('Page_home/chk_box1')) Checks a check-box or radio button.
WebUI.uncheck(findTestObject('Page_home/chk_box1')) Unchecks a check-box or radio button.
WebUI.setText(findTestObject('Page_login/txt_username'), username) Writes a text on a textbox or any input field (similar to sendKeys() in selenium)
WebUI.selectOptionByValue(findTestObject('Page_login/gender'), 'Male', false) Selects a dropdown option by its value.
WebUI.selectOptionByLabel(findTestObject('Page_login/gender'), 'M', false) Selects a dropdown option by its visible text.
WebUI.selectOptionByIndex(findTestObject('Page_login/gender'), 1) Selects a dropdown option by its index value.

Common wait related keywords

KeywordDescription
WebUI.waitForElementClickable(findTestObject('Page_home/btn_1'), 10) Waits for an element to be clickable with timeout value in seconds.
WebUI.waitForElementVisible(findTestObject('Page_home/btn_2'), 10) Waits for an element to be visible with timeout value in seconds.
WebUI.waitForElementAttributeValue(findTestObject('Page_home/btn_3'), 'id', 'submit3', 10) Waits till an element is not visible.

Commonly used validation related keywords

KeywordDescription
WebUI.verifyElementPresent(findTestObject('Page_home/btn_1'), 20) Returns a boolean value of true/false based on the presence of an element.
WebUI.verifyElementNotPresent(findTestObject('Page_home/btn_1'), 20) Returns a boolean value of true/false based on the absence of an element.
WebUI.verifyElementText(findTestObject('Page_home/btn_1'), 'Submit') Return true if the element's text is same as expected text else false.
WebUI.verifyEqual(var1, 20) Return true if the actual value is same as expected value.
WebUI.verifyNotEqual(var2, 20) Return false if the actual value is same as expected value.

Other commonly used keywords

KeywordDescription
WebUI.acceptAlert() Simulates user action of accepting an alert or clicking 'Ok' on an alert dialog box.
WebUI.dismissAlert() Simulates user action of dismissing an alert or clicking 'Cancel' on an alert dialog box.
WebUI.getAttribute(findTestObject('Page_home/btn_1'), 'class') Used to get a particular attribute of a web element e.g. name, type etc.
WebUI.getText(findTestObject('Success_msg')) Used to get the element's text.
WebUI.dragAndDropToObject(findTestObject('Page1/sourceElement'), findTestObject('Page1/targetElement')) Simulates drag and drop action.
WebUI.switchToFrame(findTestObject('iframe_bqx'), 10) Switches to a particular iframe with timeout value of 10 seconds.
WebUI.switchToWindowTitle('ArtOftesting demo') Swicthes to a particular window with the given title.
WebUI.closeWindowTitle('ArtOftesting demo') Return false if the actual value is same as expected value.
WebUI.switchToDefaultContent() Switches to the parent window, called after performing some function on an iframe or a different window/tab.
WebUI.takeScreenshot('D:\\screenshot.jpg') Takes screenshot of the browser when called.
WebUI.executeJavaScript("{javascript code}", null) Used to execute javascript command.
WebUI.uploadFile(findTestObject('fileUpload'), 'D:\\screenshot.png') Used to perform file upload operation on a file type element.

Google calculator test in script mode

Instead of directly creating test case in script mode, we can use the test case we created using manual mode in our last tutorial - Create test case in manual mode and convert them to script mode by clicking the script tab. In case, you are creating test script from scratch you can use above WebUI keywords. We can create following script that check google calculator feature, just make sure you have added all the elements in the object repository by using object spy.

WebUI.openBrowser('')

WebUI.navigateToUrl('https://www.google.co.in/')

WebUI.setText(findTestObject('Page_Google/input_q'), '2+2')

WebUI.click(findTestObject('Page_Google/input_lsb'))

WebUI.verifyElementText(findTestObject('Page_22 - Google Search (1)/div_4 (function()var adocument'), '4')

WebUI.closeBrowser()


This completes the test script creation part. Now, we can execute our test script by clicking 'Run' button. You can see the test steps getting executed in the selected browser and the result of the test as passed. Also, you can change the expected value to '5' instead of the correct value '4' and run the test to see it getting failed.
This concludes our post on test script creation using script mode feature. In the next tutorial, we will study creation of test script in script mode of Katalon Studio.