[Sharing is learning] [Get rewards] Join Us Now to Become Katalon Champions. Learn more
udi
Katalon Apprentice
03/28/2018

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.
Upvote
Quote

Comments

  • Marek Melocik
    Katalon Expert
    03/28/2018
    edited March 28
    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))
    }



    Best Answer
    Upvote
    Quote
  • Russ Thomas
    Katalon Evangelist
    03/28/2018
    Kudos, Marek.  Very nice, indeed.
    Upvote
    Quote
  • udi
    Katalon Apprentice
    03/28/2018
    edited March 28
    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.
    Upvote
    Quote
  • Marek Melocik
    Katalon Expert
    03/28/2018
    edited March 28
    Oh, forgot to mention - it is only missing import. :) 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



    Upvote
    Quote
  • Sayed Asgar Ali
    Katalon Apprentice
    05/24/2018
    Marek Melocik, thanks a lot for your quick workaround-script given above. It helped me as well to resolve similar issues.
    Upvote
    Quote
  • Pooja
    Katalon Apprentice
    07/04/2018
    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.

    Upvote
    Quote
  • Pooja
    Katalon Apprentice
    07/11/2018
    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))
    }



    Hello Marek, i am facing issue with above code. Folder path appears to be incorrect.Can you please provide your comments on same.
    Upvote
    Quote
  • Marek Melocik
    Katalon Expert
    07/11/2018
    Hi Pooja,

    this is still working for me. Can you share what error you get?
    Upvote
    Quote
  • Pooja
    Katalon Apprentice
    07/11/2018
    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 83.1K
    Upvote
    Quote
  • Pooja
    Katalon Apprentice
    07/11/2018
    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 189.5K
    3.png 181.2K
    Upvote
    Quote
  • Marek Melocik
    Katalon Expert
    07/11/2018
    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)
    Upvote
    Quote
  • Pooja
    Katalon Apprentice
    07/11/2018
    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. 
    Upvote
    Quote
  • Marek Melocik
    Katalon Expert
    07/11/2018
    edited July 11
    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())


    Upvote
    Quote
  • Pooja
    Katalon Apprentice
    07/11/2018
    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.
    Upvote
    Quote
Sign In or Register to comment.
Not the answer you're looking for? Consider subscribing to Business support.