Can i loop over an object repository folder?

Hi,

i’m running the same 2 simple tests on few items on the same page.

  1. verify element is present.
  2. verify specific text is present.

can i loop on the folder that contains all the test objects?

say something like that:
for each item in ‘target folder name’
{
verifyElementPresent
verifyTextPresent
}

Thanks,
udi.

Hi Udi,

I am not aware of any similar functionality in Katalon. I wrote a quick workaround-script which should work for you. Try this:

// specify a path to the folder with TestObjects in your Object Repository
// please keep backslashes at the end of this string, otherwise it won't work
String objectSubFolder = "SomeFolder\\SomeSubfolder\\"

String pathToObjRep = RunConfiguration.getProjectDir() + "\\Object Repository\\" + objectSubFolder

File yourObjects = new File(pathToObjRep)

// get all files from the directory
String[] fileNames = yourObjects.list()
List<String> testObjects = new ArrayList<>()

// get only test objects, not other subdirectories if there are any (with .rs extension)
for(int i = 0; i < fileNames.length; i++) {
	if(fileNames[i].contains(".rs")) {
		fileNames[i] = fileNames[i].replace(".rs", "")
		// add to a list a relative path to TestObject within Object Repository
		testObjects.add(objectSubFolder + fileNames[i])
	}
}

// foreach test object, do anything you want
for(String obj in testObjects) {
	WebUI.verifyElementVisible(findTestObject(obj))
}

9 Likes

Kudos, Marek. Very nice, indeed.

Thank you very much!
sounds exactly like what i needed!

i ran your code and got this error:
03-28-2018 04:56:34 PM - [ERROR] - Test Cases/Status FAILED because (of) Variable ‘RunConfiguration’ is not defined for test case.

i also tried to save it as a custom Keyword and use it, and got the same error.

Oh, forgot to mention - it is only missing import. :slight_smile: There might be also other missing imports, you can add them directly when you put text cursor at the end of class name and press Ctrl + Space (proposal menu appears)

import com.kms.katalon.core.configuration.RunConfiguration

3 Likes

@Marek Melocik, thanks a lot for your quick workaround-script given above. It helped me as well to resolve similar issues.

1 Like

I am using above script but my test case is failing with “Object Object Repository/\Objects\android.widget.TextView2 - AM not found” message. Please help.

Marek Melocik said:

Hi Udi,

I am not aware of any similar functionality in Katalon. I wrote a quick workaround-script which should work for you. Try this:

// specify a path to the folder with TestObjects in your Object Repository

// please keep backslashes at the end of this string, otherwise it won’t work
String objectSubFolder = “SomeFolder\SomeSubfolder\”

String pathToObjRep = RunConfiguration.getProjectDir() + “\Object Repository\” + objectSubFolder

File yourObjects = new File(pathToObjRep)

// get all files from the directory
String fileNames = yourObjects.list()
List testObjects = new ArrayList<>()

// get only test objects, not other subdirectories if there are any (with .rs extension)
for(int i = 0; i < fileNames.length; i++) {
if(fileNames[i].contains(“.rs”)) {
fileNames[i] = fileNames[i].replace(“.rs”, “”)
// add to a list a relative path to TestObject within Object Repository
testObjects.add(objectSubFolder + fileNames[i])
}
}

// foreach test object, do anything you want
for(String obj in testObjects) {
WebUI.verifyElementVisible(findTestObject(obj))
}


  

  

  

Hello Marek, i am facing issue with above code. Folder path appears to be incorrect.Can you please provide your comments on same.

Hi Pooja,

this is still working for me. Can you share what error you get?

Marek Melocik said:

Hi Pooja,

this is still working for me. Can you share what error you get?

Hi Marek,

I have attached my object repository folder structure image and the script image which i am using. Also i have attached error image (image 4) which gives me error as " Object Object Repository/Objects\android.widget.TextView2 - AM not found"
I can see that the object path appears to be incorrect and due to this may be its giving me error as “Object Not found”
Please let me know if my workflow is incorrect.

1.png

Marek Melocik said:

Hi Pooja,

this is still working for me. Can you share what error you get?

I am getting object not found error and its showing incorrect path of objects in error message.I have attached error images for reference

4.png

3.png

This error - Object Object Repository/Objects\android.widget.TextView2 - AM not found - looks like that Katalon cannot find the object on your page, not in repository.

You’d get other error for invalid object path, like this:
Unable to click on object (Root cause: java.lang.IllegalArgumentException: Object is null)

Marek Melocik said:

This error - Object Object Repository/Objects\android.widget.TextView2 - AM not found - looks like that Katalon cannot find the object on your page, not in repository.

You’d get other error for invalid object path, like this:
Unable to click on object (Root cause: java.lang.IllegalArgumentException: Object is null)

Right. But if i look for this object individually i am able to identify it. Its showing incorrect path for my object in repository as : path **Object Object Repository/Objects\android.widget.TextView2 - AM
Object correct path is : Object Repository/Objects/android.widget.TextView1 - A
you can see there is difference between forward and backward slash. **

I know, but it doesn’t matter if you use forward slash or backslash. In both cases, TestObject is returned.

println findTestObject('Misc\\Test').getActiveProperties().get(0).getName()
println findTestObject('Misc/Test').getActiveProperties().get(0).getName()
//console
07-11-2018 03:28:09 PM - [START]  - Start action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc\test).getActiveProperties().get(0).getName())
07-11-2018 03:28:09 PM - [INFO]   - Finding Test Object with id 'Object Repository/Misc\test'
xpath
07-11-2018 03:28:09 PM - [END]    - End action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc\test).getActiveProperties().get(0).getName())
07-11-2018 03:28:09 PM - [START]  - Start action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc/test).getActiveProperties().get(0).getName())
07-11-2018 03:28:09 PM - [INFO]   - Finding Test Object with id 'Object Repository/Misc/test'
xpath
07-11-2018 03:28:09 PM - [END]    - End action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc/test).getActiveProperties().get(0).getName())

Marek Melocik said:

I know, but it doesn’t matter if you use forward slash or backslash. In both cases, TestObject is returned.

println findTestObject('Misc\\Test').getActiveProperties().get(0).getName()

println findTestObject(‘Misc/Test’).getActiveProperties().get(0).getName()
//console
07-11-2018 03:28:09 PM - [START] - Start action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc\test).getActiveProperties().get(0).getName())
07-11-2018 03:28:09 PM - [INFO] - Finding Test Object with id ‘Object Repository/Misc\test’
xpath
07-11-2018 03:28:09 PM - [END] - End action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc\test).getActiveProperties().get(0).getName())
07-11-2018 03:28:09 PM - [START] - Start action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc/test).getActiveProperties().get(0).getName())
07-11-2018 03:28:09 PM - [INFO] - Finding Test Object with id ‘Object Repository/Misc/test’
xpath
07-11-2018 03:28:09 PM - [END] - End action : Statement - println(com.kms.katalon.core.testobject.ObjectRepository.findTestObject(Misc/test).getActiveProperties().get(0).getName())


  

  

Ok Thanks Marek. I dont know whats the issue. Its not working for me.I can identify same objects using other keywords but not working with this one.Let me know in case you find any other workaround/solution for this.