Generate Test Objects from Command-line

For our Katalon workflow we have had success generating test objects from the command line, outside of Katalon Studio. We wrote a Java utility to do this, although you could use any scripting language, such as Python, Groovy, Powershell etc.

It is based on the assumption that all of the test objects are identified by HTML ID attribute. We try to insist that all page elements have unique IDs in order to avoid identifying objects using complex XPath that are likely to change over time.

We use this utility to generate the test objects for a web feature prior to writing the Katalon test cases.

Our command line utility takes two arguments, the path to the new test object, and the HTML ID. So, for example, this is how it runs.

c:\git\my-katalon-project>makekto "Object Repository\MyFeature\Form1\BtnSave" form1SaveBtn

It generates a BtnSave.ts file like this in the specified path.

<?xml version="1.0" encoding="UTF-8"?>
<WebElementEntity>
   <description></description>
   <name>BtnSave</name>
   <tag></tag>
   <elementGuidId>1788ef27-3483-45bc-b0d5-b792dcb31e74</elementGuidId>
   <selectorCollection>
      <entry>
         <key>BASIC</key>
         <value>//*[@id = 'form1BtnSave']</value>
      </entry>
   </selectorCollection>
   <selectorMethod>BASIC</selectorMethod>
   <useRalativeImagePath>false</useRalativeImagePath>
   <webElementProperties>
      <isSelected>true</isSelected>
      <matchCondition>equals</matchCondition>
      <name>id</name>
      <type>Main</type>
      <value>form1BtnSave</value>
   </webElementProperties>
</WebElementEntity>

Notice that the name matches the filename, and that the HTML ID is inserted twice, once in the selectorCollection entry value and once in the webElementProperties value. Also, our utility generates a unique elementGuidId.

The generated objects work fine in Katalon Studio with no apparent issues.

3 Likes

Thank you for the idea. Do you have any plan to open source the tool? This would be a handy feature for the Gradle plugin.

1 Like

Alex, I would love to contribute to the gradle plugin project. I may not get to it right away. In the mean time you are welcome to use this code or improve upon it.

import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.UUID;

/**
 * Make a Katalon test object.
 */
public class MakeKto {

    private static final String TEMPLATE =
        "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<WebElementEntity>\n"
            + "   <description></description>\n"
            + "   <name>REPLACE_NAME</name>\n" + "   <tag></tag>\n"
            + "   <elementGuidId>REPLACE_GUID</elementGuidId>\n"
            + "   <selectorCollection>\n" + "      <entry>\n"
            + "         <key>BASIC</key>\n"
            + "         <value>//*[@id = 'REPLACE_ID']</value>\n"
            + "      </entry>\n" + "   </selectorCollection>\n"
            + "   <selectorMethod>BASIC</selectorMethod>\n"
            + "   <useRalativeImagePath>false</useRalativeImagePath>\n"
            + "   <webElementProperties>\n"
            + "      <isSelected>true</isSelected>\n"
            + "      <matchCondition>equals</matchCondition>\n"
            + "      <name>id</name>\n" + "      <type>Main</type>\n"
            + "      <value>REPLACE_ID</value>\n"
            + "   </webElementProperties>\n" + "</WebElementEntity>";

    public static void main(String[] args) throws Exception {

        if (args.length != 2) {
            System.out.println("makekto <path> <id>");
        } else {
            String path = args[0];

            path = path.replace("\\", "/");

            final String id = args[1];
            new MakeKto().writeKtoFile(path, id);
        }

    }

    private String parseName(String path) {

        int pos = path.lastIndexOf("/");
        if (pos < 1) {
            throw new RuntimeException(
                String.format(
                    "Path does not look like a test object path, %s",
                    path));
        }
        return path.substring(pos + 1);
    }

    private void writeKtoFile(String path, String id) throws Exception {

        final String name = parseName(path);
        System.out.println("Test object name is " + name);
        System.out.println("Test object id=" + id);

        final UUID uuid = UUID.randomUUID();
        final String randomUUIDString = uuid.toString();

        path = path + ".rs";
        System.out.println("Writing " + path + "...");
        
        PrintWriter writer = null;
        try {
            final boolean isAppend = false;
            final FileWriter fw = new FileWriter(path, isAppend);
            writer = new PrintWriter(fw);
            String template = TEMPLATE;
            template = template.replaceAll("REPLACE_NAME", name);
            template = template.replaceAll("REPLACE_ID", id);
            template = template.replaceAll("REPLACE_GUID", randomUUIDString);
            writer.println(template);
        } finally {
            if (writer != null) {
                writer.close();
                System.out.println("Done.");
            }
        }
    }

}