Special thanks to Katalon Software Engineer @Thong for sharing these tips and best practices.
Working with test objects and maintaining them can be painful for beginners since the web elements can change very frequently. Below are some best practices and tips for capturing test objects and using Self-healing more effectively.
1. Use Custom Attributes for Identifying Web Elements
When you use the Web Recorder or Spy Web utility in Katalon to capture a test object, it automatically generates several locators and uses the best-fit option (ID, for example) to identify such object.
But in some cases, web applications don’t use the standard attributes for object identification. Since the attributes can change from release to release, such applications use custom attributes for testing purposes (a good practice to follow). Here’s an example:
In this example, the custom attribute “data-testid” is the one we want to use for the default locator to identify the username input element.
Here’s how you can configure Katalon Studio to prioritize the (custom) attributes you want to use for element locators.
Step 1. Go to Project > Settings > Test Design > WebUI.
Step 2. Change the top priority locator (to XPath:customAttributes in this case).
Step 3. Add the custom attribute(s) you want to use to identify the object. Then click Apply and Close.
Here’s the result of capturing the input element using Spy Web after the configuration:
Learn the best-fit locators to identify the test objects on your application under test (AUT) and change the locator priority accordingly.
2. Disable Self-Healing Where It’s Needed
Self-healing is an excellent solution for beginners to solve the problem of test failures due to broken locators. When a default locator is broken, the Self-healing feature looks at the predefined alternatives (generated when you capture the object using Spy Web or Web Recorder) and uses a working locator to identify the object, preventing the test from failing.
For more information about Self-healing:
However, in some cases, you don’t want your tests to “self-heal.” For example, you want your test to verify if a web element has a specific attribute and value. If the element cannot be found with the default locator, the test should fail immediately instead of passing thanks to self-healing using an alternative locator.
Katalon allows you to exclude keywords from the self-healing mechanism. Go to Project > Settings > Self-healing > Web UI. Then, add the keywords you don’t want to apply to self-healing.
Think of a list of test steps that don’t need self-healing and configure to exclude them to keep your test results reliable.
You can also prioritize the element locators for self-healing. The more reliable ones should always have a higher priority. This will optimize the self-healing test execution speed.
3. Use the Image Selection Method Effectively
You can use the image selection method to identify test objects. Katalon applies machine learning to find a web element based on a base image.
The problem is that many users don’t know how to capture a proper screenshot to use as a base image. Typically, they manually take screenshots of web elements using a third-party tool like Lightshot Screenshot (or CMD + Shift + 4 for Mac). The screenshots are inconsistent.
Here’s documentation on the correct way to do it: Capture screenshots for object recognition.
The size of the captured screenshots should be the same as the element shown on the AUT.
For the best results, we recommend using the image selection method when you capture the screenshots and execute the tests on the same device or in an environment where you can configure the same window size and screen resolution.
4. Learn the Basics of Web Elements
Using Spy Web utility and self-healing without prior knowledge can cause beginners to end up with a mess of unorganized and easily broken test objects (yes, this happens all the time).
The best practice is to understand the principles and basics of web elements. Then, read the documentation to understand all the related features’ functionalities in Katalon.
Although self-healing can be very helpful for beginners, don’t rely heavily and solely on it. You should always focus on getting the right and stable locators to identify the objects on your AUT, and know how to maintain them. This will also help you to configure and use self-healing more effectively.
If you want more articles like this, visit our Product Insights corner.