# Object Repository Garbage Collector

**URL:** <https://forum.katalon.com/t/object-repository-garbage-collector/170062>\
**Category:** Web Testing\
**Tags:** katalon-studio\
**Created:** [April 1, 2025, 1:56pm UTC](https://forum.katalon.com/t/object-repository-garbage-collector/170062 "2025-04-01T13:56:05Z")\
**Posts on this page:** 1\
**Showing post:** 3

<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:** [April 28, 2025, 2:10pm UTC](https://forum.katalon.com/t/object-repository-garbage-collector/170062/3 "2025-04-28T14:10:30Z")

</div>

I have published the new [v0.4.12](https://github.com/kazurayam/KS_ObjectRepositoryGarbageCollector/releases/tag/0.4.12). I will show you new reports.

### List of _Locators_ with container Test Objects

Another Test Case script generates a JSON titled “LocatorIndex”

```auto
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import com.kazurayam.ks.reporting.Shorthand
import com.kazurayam.ks.testobject.combine.ObjectRepositoryGarbageCollector
import com.kms.katalon.core.configuration.RunConfiguration

import groovy.json.JsonOutput
import internal.GlobalVariable

/**
 * ObjectRepositoryGarbageCollector#jsonifyCombinedLocatorIndex() demonstration
 */

ObjectRepositoryGarbageCollector gc =
	new ObjectRepositoryGarbageCollector.Builder()
		.includeObjectRepositoryFolder("**/Page_CURA*")
		.build()

String json = gc.jsonifyCombinedLocatorIndex()

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path classOutputDir = projectDir.resolve("build/tmp/testOutput/demo/ObjectRepositoryGarbageCollector")
Path outDir = classOutputDir.resolve("ORGC_jsonifyCombinedLocatorIndex")
Files.createDirectories(outDir)
File outFile = outDir.resolve("garbage.json").toFile()

outFile.text = JsonOutput.prettyPrint(json)

```

This script produced this (trimmed):

```auto
{
    "CombinedLocatorIndex": {
        "Number of Locators": 13,
        "Number of Suspicious Locators": 3,
        "Locators": [
            {
                "Locator": {
                    "value": "(.//*[normalize-space(text()) and normalize-space(.)='Sa'])[1]/following::td[31]",
                    "selectorMethod": "XPATH"
                },
                "Number of Container TestObjects": 2,
                "Container TestObjects": [
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/td_28",
                        "is used": false
                    },
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/xtra/td_28",
                        "is used": false
                    }
                ]
            },
            {
                "Locator": {
                    "value": "//a[@id='btn-make-appointment']",
                    "selectorMethod": "XPATH"
                },
                "Number of Container TestObjects": 1,
                "Container TestObjects": [
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/a_Make Appointment",
                        "is used": true,
                        "References from TestCase": [
                            {
                                "TestObjectId": "main/Page_CURA Healthcare Service/a_Make Appointment",
                                "Number of ForwardReferences": 2,
                                "ForwardReferences": [
                                    {
                                        "TestCaseId": "main/TC0",
                                        "DigestedLine": {
                                            "line": "WebUI.click(findTestObject('Object Repository/main/Page_CURA Healthcare Service/a_Make Appointment'))",
                                            "lineNo": 12,
                                            "pattern": "main/Page_CURA Healthcare Service/a_Make Appointment",
                                            "matchAt": 47,
                                            "matchEnd": 99,
                                            "matched": true,
                                            "regex": false
                                        },
                                        "TestObjectId": "main/Page_CURA Healthcare Service/a_Make Appointment"
                                    },
                                    {
                                        "TestCaseId": "main/TC1",
                                        "DigestedLine": {
                                            "line": "WebUI.click(findTestObject('Object Repository/main/Page_CURA Healthcare Service/a_Make Appointment'))",
                                            "lineNo": 11,
                                            "pattern": "main/Page_CURA Healthcare Service/a_Make Appointment",
                                            "matchAt": 47,
                                            "matchEnd": 99,
                                            "matched": true,
                                            "regex": false
                                        },
                                        "TestObjectId": "main/Page_CURA Healthcare Service/a_Make Appointment"
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },
...

```

See the full file at [here](https://github.com/kazurayam/KS_ObjectRepositoryGarbageCollector/blob/develop/docs/assets/testOutput/demo/ObjectRepositoryGarbageCollector/ORGC_jsonifyCombinedLocatorIndex/locatorIndex.json)

The CombinedLocatorIndex JSON tells me the following points:

1. this project contains 12 “Locators” declared in the Object Repository.
2. A Locator could be found in one or more Test Objects. If “Number of Container TestObjects” has value 2 or more, it means that the Locator is duplicating. You may want to avoid duplication.
3. A TestObject may be used by zero or more Test Cases. If the number of references from TestCase" of a TestObject is 0, it means the TestObject is unused. `"is used": false` denotes this. You may want to remove the unused TestObject.
4. This JSON could be large. As large as mega-bytes. The size depends on the number of TestObjects in your Object Repository. A too-large JSON won’t be very useful.
5. The ObjectRepositoryGarbageCollector supports `includeObjectRepositoryFolder(pattern)` and `excludeObjectRepositoryFolder(pattern)`. For example:

```auto
ObjectRepositoryGarbageCollector gc =
	new ObjectRepositoryGarbageCollector.Builder()
		.includeObjectRepositoryFolder("**/Page_CURA*")
		.build()

```

You can specify from which sub-folders of the “Object Repository” to select Test Objects as target. Thus you can make the JSON much smaller and forcused.

### Concise list of _Suspicious Locators_

Final Test Case script generates a JSON titled “Suspicious Locator Index” which would be most useful.

```auto
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths

import com.kazurayam.ks.testobject.combine.ObjectRepositoryGarbageCollector
import com.kms.katalon.core.configuration.RunConfiguration

import groovy.json.JsonOutput

/**
 * ObjectRepositoryGarbageCollector#jsonifySuspiciousLocatorIndex() demonstration
 */

ObjectRepositoryGarbageCollector gc =
	new ObjectRepositoryGarbageCollector.Builder()
		.includeObjectRepositoryFolder("**/Page_CURA*")
		.build()

String json = gc.jsonifySuspiciousLocatorIndex()

Path projectDir = Paths.get(RunConfiguration.getProjectDir())
Path classOutputDir = projectDir.resolve("build/tmp/testOutput/demo/ObjectRepositoryGarbageCollector")
Path outDir = classOutputDir.resolve("ORGC_jsonifySuspiciousLocatorIndex")
Files.createDirectories(outDir)
File outFile = outDir.resolve("garbage.json").toFile()

outFile.text = JsonOutput.prettyPrint(json)

```

This script generated a JSON as follows:

```auto
{
    "SuspiciousLocatorIndex": {
        "Number of Locators": 3,
        "Number of Suspicious Locators": 3,
        "Locators": [
            {
                "Locator": {
                    "value": "(.//*[normalize-space(text()) and normalize-space(.)='Sa'])[1]/following::td[31]",
                    "selectorMethod": "XPATH"
                },
                "Number of Container TestObjects": 2,
                "Container TestObjects": [
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/td_28",
                        "is used": false
                    },
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/xtra/td_28",
                        "is used": false
                    }
                ]
            },
            {
                "Locator": {
                    "value": "//body",
                    "selectorMethod": "XPATH"
                },
                "Number of Container TestObjects": 1,
                "Container TestObjects": [
                    {
                        "TestObjectId": "misc/dummy1",
                        "is used": false
                    }
                ]
            },
            {
                "Locator": {
                    "value": "//section[@id='summary']/div/div/div[7]/p/a",
                    "selectorMethod": "XPATH"
                },
                "Number of Container TestObjects": 3,
                "Container TestObjects": [
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/a_Foo",
                        "is used": false
                    },
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/a_Go to Homepage",
                        "is used": true,
                        "References from TestCase": [
                            {
                                "TestObjectId": "main/Page_CURA Healthcare Service/a_Go to Homepage",
                                "Number of ForwardReferences": 1,
                                "ForwardReferences": [
                                    {
                                        "TestCaseId": "main/TC1",
                                        "DigestedLine": {
                                            "line": "WebUI.click(findTestObject('Object Repository/main/Page_CURA Healthcare Service/a_Go to Homepage'))",
                                            "lineNo": 36,
                                            "pattern": "main/Page_CURA Healthcare Service/a_Go to Homepage",
                                            "matchAt": 47,
                                            "matchEnd": 97,
                                            "matched": true,
                                            "regex": false
                                        },
                                        "TestObjectId": "main/Page_CURA Healthcare Service/a_Go to Homepage"
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "TestObjectId": "main/Page_CURA Healthcare Service/xtra/a_Go to Homepage",
                        "is used": false
                    }
                ]
            }
        ]
    },
    "Run Description": {
        "Project name": "katalon",
        "includeScriptsFolder": [
            "main",
            "misc"
        ],
        "includeObjectRepositoryFolder": [
            "main",
            "misc"
        ],
        "Number of TestCases": 5,
        "Number of TestObjects": 16,
        "Number of unused TestObjects": 5
    }
}

```

Let me explain about this JSON.

1. A Locator is _suspicious_ if it is declared by a Test Object which is unused (referred by none of Test Cases)
2. The “SuspiciousLocatorIndex” JSON contains only suspicious Locators. It doesn’t contain trustworthy Locators. Therefore the JSON file would be far smaller than the full list of Locators.
3. As you work on cleaning your Object Repository, the “SuspiciousLocatorIndex” will become smaller. When the index has got 0 entries, you can be sure you finished cleaning up.

---

_[View the full topic](https://forum.katalon.com/t/object-repository-garbage-collector/170062)._
