# Is there a way to mass import object or generate them via script?

**URL:** <https://forum.katalon.com/t/is-there-a-way-to-mass-import-object-or-generate-them-via-script/143117>\
**Category:** Web Testing\
**Tags:** web-testing\
**Created:** [August 24, 2024, 5:07am UTC](https://forum.katalon.com/t/is-there-a-way-to-mass-import-object-or-generate-them-via-script/143117 "2024-08-24T05:07:00Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![kazurayam](https://dub1.discourse-cdn.com/katalon/user_avatar/forum.katalon.com/kazurayam/32/333_2.png) [@kazurayam](https://forum.katalon.com/u/kazurayam)\
**Post date:** [August 25, 2024, 9:30pm UTC](https://forum.katalon.com/t/is-there-a-way-to-mass-import-object-or-generate-them-via-script/143117/6 "2024-08-25T21:30:06Z")

</div>

I presented a sample Test Case script that has a function `makeTestObject()` declared inside the TestCase. This will work. But **this way has a potential problem**. If you call the `makeTestObject()` function repeatedly, say 100 times, you would find quite a lot of lines of DEBUG log in the LogViewer. See the following Test Case:

```auto
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.WebElement

TestObject makeTestObjectXPath(String id, String xPath) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("xpath", ConditionType.EQUALS, xPath)
	return tObj
}

TestObject makeTestObjectCSS(String id, String selector) {
	TestObject tObj = new TestObject(id)
	tObj.addProperty("css", ConditionType.EQUALS, selector)
	return tObj
}

// set the path of Firefox binary
System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox")

String url = 'https://react.dev/learn/typescript#typing-dom-events'

WebUI.openBrowser('')
WebUI.maximizeWindow()
WebUI.navigateToUrl(url)

// in the page, find the 5th div that contains 'App.tsx' 
// which is near the header string "DOM Events", locate the target <iframe> element
TestObject toIframe = makeTestObjectXPath('toIframe',
	"//div[contains(@class,'sandpack') and contains(.,'App.tsx')][5]//iframe")
WebUI.verifyElementPresent(toIframe, 10)

// switch the context into the <iframe>
WebUI.switchToDefaultContent()
boolean b = WebUI.switchToFrame(toIframe, 10, FailureHandling.STOP_ON_FAILURE)
assert b : 'failed to switch to the iframe'

// inside the <iframe>, make sure a HTML document has been loaded
TestObject toRoot = makeTestObjectXPath('toRoot', "/html/body")
WebUI.verifyElementPresent(toRoot, 10, FailureHandling.STOP_ON_FAILURE)

// locate the <input> element
TestObject toInput = makeTestObjectXPath('toInput',
	"//*[@id='root']/input")
WebUI.verifyElementPresent(toInput, 10, FailureHandling.STOP_ON_FAILURE)

WebUI.delay(3)

// try typing a string into the <input> element which was rendered by React.js
WebUI.clearText(toInput)
WebUI.sendKeys(toInput, "Hooah")

WebUI.delay(3)

// make sure that a text was echoed in the sibling <p> element 
TestObject toValue = makeTestObjectXPath('toValue',
	"//*[@id='root']/p")
WebUI.verifyElementText(toValue, 'Value: Hooah')

// Switch back to default content'
WebUI.switchToDefaultContent()

WebUI.closeBrowser()

```

This Test Case emits the following message in the console:

```auto
2024-08-26 06:41:23.542 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2024-08-26 06:41:23.549 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/TC1
2024-08-26 06:41:24.358 DEBUG testcase.TC1 - 1: System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox")
2024-08-26 06:41:24.377 DEBUG testcase.TC1 - 2: url = "https://react.dev/learn/typescript#typing-dom-events"
2024-08-26 06:41:24.383 DEBUG testcase.TC1 - 3: openBrowser("")
2024-08-26 06:41:24.987 INFO c.k.k.core.webui.driver.DriverFactory - Starting 'Chrome' driver
8月 26, 2024 6:41:24 午前 org.openqa.selenium.remote.DesiredCapabilities chrome
情報: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
2024-08-26 06:41:25.047 INFO c.k.k.core.webui.driver.DriverFactory - Action delay is set to 0 milliseconds
Starting ChromeDriver 127.0.6533.119 (bdef6783a05f0b3f885591e7d2c7b2aec1a89dea-refs/branch-heads/6533@{#1999}) on port 10580
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
8月 26, 2024 6:41:29 午前 org.openqa.selenium.remote.ProtocolHandshake createSession
情報: Detected dialect: W3C
2024-08-26 06:41:29.212 INFO c.k.k.core.webui.driver.DriverFactory - sessionId = 618fe0111edc769e77303f4e55b81f20
2024-08-26 06:41:29.255 INFO c.k.k.core.webui.driver.DriverFactory - browser = Chrome 127.0.0.0
2024-08-26 06:41:29.276 INFO c.k.k.core.webui.driver.DriverFactory - platform = Mac OS X
2024-08-26 06:41:29.315 INFO c.k.k.core.webui.driver.DriverFactory - seleniumVersion = 3.141.59
2024-08-26 06:41:29.337 INFO c.k.k.core.webui.driver.DriverFactory - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password= ******** , proxyServerAddress=, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
2024-08-26 06:41:29.382 DEBUG testcase.TC1 - 4: maximizeWindow()
2024-08-26 06:41:29.855 DEBUG testcase.TC1 - 5: navigateToUrl(url)
2024-08-26 06:41:31.253 DEBUG testcase.TC1 - 6: toIframe = makeTestObjectXPath("toIframe", "//div[contains(@class,'sandpack') and contains(.,'App.tsx')][5]//iframe")
2024-08-26 06:41:31.275 DEBUG testcase.TC1 - 1: tObj = new com.kms.katalon.core.testobject.TestObject(id)
2024-08-26 06:41:31.317 DEBUG testcase.TC1 - 2: tObj.addProperty("xpath", EQUALS, xPath)
2024-08-26 06:41:31.347 DEBUG testcase.TC1 - 3: return tObj
2024-08-26 06:41:31.361 DEBUG testcase.TC1 - 7: verifyElementPresent(toIframe, 10)
2024-08-26 06:41:32.912 DEBUG testcase.TC1 - 8: switchToDefaultContent()
2024-08-26 06:41:33.006 DEBUG testcase.TC1 - 9: b = switchToFrame(toIframe, 10, STOP_ON_FAILURE)
2024-08-26 06:41:33.518 DEBUG testcase.TC1 - 10: assert b : "failed to switch to the iframe"
2024-08-26 06:41:33.530 DEBUG testcase.TC1 - 11: toRoot = makeTestObjectXPath("toRoot", "/html/body")
2024-08-26 06:41:33.540 DEBUG testcase.TC1 - 1: tObj = new com.kms.katalon.core.testobject.TestObject(id)
2024-08-26 06:41:33.543 DEBUG testcase.TC1 - 2: tObj.addProperty("xpath", EQUALS, xPath)
2024-08-26 06:41:33.547 DEBUG testcase.TC1 - 3: return tObj
2024-08-26 06:41:33.553 DEBUG testcase.TC1 - 12: verifyElementPresent(toRoot, 10, STOP_ON_FAILURE)
2024-08-26 06:41:33.764 DEBUG testcase.TC1 - 13: toInput = makeTestObjectXPath("toInput", "//*[@id='root']/input")
2024-08-26 06:41:33.767 DEBUG testcase.TC1 - 1: tObj = new com.kms.katalon.core.testobject.TestObject(id)
2024-08-26 06:41:33.769 DEBUG testcase.TC1 - 2: tObj.addProperty("xpath", EQUALS, xPath)
2024-08-26 06:41:33.778 DEBUG testcase.TC1 - 3: return tObj
2024-08-26 06:41:33.785 DEBUG testcase.TC1 - 14: verifyElementPresent(toInput, 10, STOP_ON_FAILURE)
2024-08-26 06:41:34.792 DEBUG testcase.TC1 - 15: delay(3)
2024-08-26 06:41:37.889 DEBUG testcase.TC1 - 16: clearText(toInput)
2024-08-26 06:41:38.053 DEBUG testcase.TC1 - 17: sendKeys(toInput, "Hooah")
2024-08-26 06:41:38.651 DEBUG testcase.TC1 - 18: delay(3)
2024-08-26 06:41:41.698 DEBUG testcase.TC1 - 19: toValue = makeTestObjectXPath("toValue", "//*[@id='root']/p")
2024-08-26 06:41:41.702 DEBUG testcase.TC1 - 1: tObj = new com.kms.katalon.core.testobject.TestObject(id)
2024-08-26 06:41:41.705 DEBUG testcase.TC1 - 2: tObj.addProperty("xpath", EQUALS, xPath)
2024-08-26 06:41:41.707 DEBUG testcase.TC1 - 3: return tObj
2024-08-26 06:41:41.719 DEBUG testcase.TC1 - 20: verifyElementText(toValue, "Value: Hooah")
2024-08-26 06:41:41.841 DEBUG testcase.TC1 - 21: switchToDefaultContent()
2024-08-26 06:41:41.872 DEBUG testcase.TC1 - 22: closeBrowser()
2024-08-26 06:41:42.193 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TC1

```

Please find that the following a section of 3 lines are repeated 4 times = the number of times the TestCase calls the function:

```auto
2024-08-26 06:41:41.702 DEBUG testcase.TC1 - 1: tObj = new com.kms.katalon.core.testobject.TestObject(id)
2024-08-26 06:41:41.705 DEBUG testcase.TC1 - 2: tObj.addProperty("xpath", EQUALS, xPath)
2024-08-26 06:41:41.707 DEBUG testcase.TC1 - 3: return tObj

```

I find this 3 lines give me no useful information. I don’t need it.

This section will be repeated as per the times your TestCase calls the funtion. Consequently the amount of DEBUG logs gets bulky. The **LogViewer becomes less useless. The test runs slower to flush the buffered messages into the UI window and log files.** See the following previous post of mine:

- [Log Viewer slows down your tests. How to prevent it?](http://forum.katalon.com/t/log-viewer-slows-down-your-tests-how-to-prevent-it/60252)

How to get rid of the verbose log messages from the function that creates a TestObject dynamically?

I can show you an alternative. You can create a custom Keyword class which implements the function as its method. The inner steps of custom Keywords won’t be logged by the Katalon Logger.

`"Keywords/my/TestObjectFactory"`

```auto
package my

import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject

public class TestObjectFactory {

	static TestObject makeTestObjectXPath(String id, String xPath) {
		TestObject tObj = new TestObject(id)
		tObj.addProperty("xpath", ConditionType.EQUALS, xPath)
		return tObj
	}

	static TestObject makeTestObjectCSS(String id, String selector) {
		TestObject tObj = new TestObject(id)
		tObj.addProperty("css", ConditionType.EQUALS, selector)
		return tObj
	}
}

```

Imporoved `"Test Cases/TC1`

```auto
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import org.openqa.selenium.WebElement
import my.TestObjectFactory

// set the path of Firefox binary
System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox")

String url = 'https://react.dev/learn/typescript#typing-dom-events'

WebUI.openBrowser('')
WebUI.maximizeWindow()
WebUI.navigateToUrl(url)

// in the page, find the 5th div that contains 'App.tsx' 
// which is near the header string "DOM Events", locate the target <iframe> element
TestObject toIframe = TestObjectFactory.makeTestObjectXPath('toIframe',
	"//div[contains(@class,'sandpack') and contains(.,'App.tsx')][5]//iframe")
WebUI.verifyElementPresent(toIframe, 10)

// switch the context into the <iframe>
WebUI.switchToDefaultContent()
boolean b = WebUI.switchToFrame(toIframe, 10, FailureHandling.STOP_ON_FAILURE)
assert b : 'failed to switch to the iframe'

// inside the <iframe>, make sure a HTML document has been loaded
TestObject toRoot = TestObjectFactory.makeTestObjectXPath('toRoot', "/html/body")
WebUI.verifyElementPresent(toRoot, 10, FailureHandling.STOP_ON_FAILURE)

// locate the <input> element
TestObject toInput = TestObjectFactory.makeTestObjectXPath('toInput',
	"//*[@id='root']/input")
WebUI.verifyElementPresent(toInput, 10, FailureHandling.STOP_ON_FAILURE)

WebUI.delay(3)

// try typing a string into the <input> element which was rendered by React.js
WebUI.clearText(toInput)
WebUI.sendKeys(toInput, "Hooah")

WebUI.delay(3)

// make sure that a text was echoed in the sibling <p> element 
TestObject toValue = TestObjectFactory.makeTestObjectXPath('toValue',
	"//*[@id='root']/p")
WebUI.verifyElementText(toValue, 'Value: Hooah')

// Switch back to default content'
WebUI.switchToDefaultContent()

WebUI.closeBrowser()

```

This improved version will emit the following messages in the LogViewer, which does not repeat the sections of useless 3 lines that I showed above.

```auto
2024-08-26 05:58:21.290 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2024-08-26 05:58:21.294 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/TC1
2024-08-26 05:58:21.990 DEBUG testcase.TC1 - 1: System.setProperty("webdriver.firefox.bin", "/Applications/Firefox.app/Contents/MacOS/firefox")
2024-08-26 05:58:22.006 DEBUG testcase.TC1 - 2: url = "https://react.dev/learn/typescript#typing-dom-events"
2024-08-26 05:58:22.066 DEBUG testcase.TC1 - 3: openBrowser("")
2024-08-26 05:58:22.510 INFO c.k.k.core.webui.driver.DriverFactory - Starting 'Chrome' driver
8月 26, 2024 5:58:22 午前 org.openqa.selenium.remote.DesiredCapabilities chrome
情報: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
2024-08-26 05:58:22.564 INFO c.k.k.core.webui.driver.DriverFactory - Action delay is set to 0 milliseconds
Starting ChromeDriver 127.0.6533.119 (bdef6783a05f0b3f885591e7d2c7b2aec1a89dea-refs/branch-heads/6533@{#1999}) on port 18378
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
8月 26, 2024 5:58:26 午前 org.openqa.selenium.remote.ProtocolHandshake createSession
情報: Detected dialect: W3C
2024-08-26 05:58:26.224 INFO c.k.k.core.webui.driver.DriverFactory - sessionId = 68e0165acd3db7bee4c887039897bea0
2024-08-26 05:58:26.273 INFO c.k.k.core.webui.driver.DriverFactory - browser = Chrome 127.0.0.0
2024-08-26 05:58:26.293 INFO c.k.k.core.webui.driver.DriverFactory - platform = Mac OS X
2024-08-26 05:58:26.315 INFO c.k.k.core.webui.driver.DriverFactory - seleniumVersion = 3.141.59
2024-08-26 05:58:26.338 INFO c.k.k.core.webui.driver.DriverFactory - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password= ******** , proxyServerAddress=, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
2024-08-26 05:58:26.399 DEBUG testcase.TC1 - 4: maximizeWindow()
2024-08-26 05:58:26.833 DEBUG testcase.TC1 - 5: navigateToUrl(url)
2024-08-26 05:58:28.484 DEBUG testcase.TC1 - 6: toIframe = TestObjectFactory.makeTestObjectXPath("toIframe", "//div[contains(@class,'sandpack') and contains(.,'App.tsx')][5]//iframe")
2024-08-26 05:58:28.543 DEBUG testcase.TC1 - 7: verifyElementPresent(toIframe, 10)
2024-08-26 05:58:31.998 DEBUG testcase.TC1 - 8: switchToDefaultContent()
2024-08-26 05:58:32.139 DEBUG testcase.TC1 - 9: b = switchToFrame(toIframe, 10, STOP_ON_FAILURE)
2024-08-26 05:58:32.555 DEBUG testcase.TC1 - 10: assert b : "failed to switch to the iframe"
2024-08-26 05:58:32.564 DEBUG testcase.TC1 - 11: toRoot = TestObjectFactory.makeTestObjectXPath("toRoot", "/html/body")
2024-08-26 05:58:32.590 DEBUG testcase.TC1 - 12: verifyElementPresent(toRoot, 10, STOP_ON_FAILURE)
2024-08-26 05:58:33.192 DEBUG testcase.TC1 - 13: toInput = TestObjectFactory.makeTestObjectXPath("toInput", "//*[@id='root']/input")
2024-08-26 05:58:33.208 DEBUG testcase.TC1 - 14: verifyElementPresent(toInput, 10, STOP_ON_FAILURE)
2024-08-26 05:58:34.575 DEBUG testcase.TC1 - 15: delay(3)
2024-08-26 05:58:37.656 DEBUG testcase.TC1 - 16: clearText(toInput)
2024-08-26 05:58:37.823 DEBUG testcase.TC1 - 17: sendKeys(toInput, "Hooah")
2024-08-26 05:58:38.602 DEBUG testcase.TC1 - 18: delay(3)
2024-08-26 05:58:41.628 DEBUG testcase.TC1 - 19: toValue = TestObjectFactory.makeTestObjectXPath("toValue", "//*[@id='root']/p")
2024-08-26 05:58:41.647 DEBUG testcase.TC1 - 20: verifyElementText(toValue, "Value: Hooah")
2024-08-26 05:58:41.732 DEBUG testcase.TC1 - 21: switchToDefaultContent()
2024-08-26 05:58:41.753 DEBUG testcase.TC1 - 22: closeBrowser()
2024-08-26 05:58:42.085 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/TC1

```

The improved Test Case will run faster than the original because it has less amount of processing (emitting messages) to do.

---

_[View the full topic](https://forum.katalon.com/t/is-there-a-way-to-mass-import-object-or-generate-them-via-script/143117)._
