Table of contents
1.
Introduction
2.
XPath
3.
Absolute XPath
4.
Relative XPath
5.
Nested element
6.
Identify nested elements using XPath
7.
Deal with dynamically changing elements
7.1.
Dynamic element
7.2.
Deal with dynamic elements using XPath
8.
Frequently Asked Questions
8.1.
Which framework is used in Katalon Studio?
8.2.
What is the purpose of Katalon Studio?
8.3.
Is Java supported by Katalon?
9.
Conclusion
Last Updated: Mar 27, 2024
Medium

Detecting objects with XPath in Katalon

Author SHIVANGI MALL
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

The identification of UI elements is essential to automated testing for any application under test (AUT). However, doing it manually takes a lot of time and HTML expertise.

When dealing with elements that lack distinguishing characteristics, change frequently, or are buried deep within another element, this task becomes considerably more difficult (nested elements). This article demonstrates how to work with nested elements and dynamic elements in Katalon Studio using XPath.

Detecting objects with XPath in Katalon

XPath

XML Path Language is also known as XPath. XPath uses path expressions to browse among elements and attributes and choose nodes or node sets in an XML document. The position of any element on a webpage with the HTML DOM (Document Object Model) structure can also be determined using XPath.  Absolute XPath and relative XPath are the two forms of XPath (smart XPath).

Absolute XPath

XML documents are viewed as node-based trees, with the root element at the top of each tree. The path that begins at the root is an absolute XPath. HTML and the forward-slash (/) mark the beginning of an absolute XPath.
 

For instance,

/html/body/div[1]/div[1]/div/div[3]/div[2]/div
 

The absolute XPath capture strategy in Katalon Studio is xpath:position. When working with dynamic elements, a straightforward solution is to use absolute XPath. This XPath could, however, fail if the structure of your web page changes for any reason. Therefore, it is not advised to use absolute XPath to find dynamic items.
 

Absolute XPath and relative XPath are the two forms of XPath (smart XPath).

Relative XPath

If you want to see a pattern in the values of a web element's attributes like ID or Class, you might wish to utilize the relative XPath approach. The path that begins in the center of the HTML DOM structure is known as a relative XPath (smart XPath). Relative XPath begins with the double forward-slash (//), as opposed to absolute XPath, which starts at the root and can search wherever on the webpage for the element. As a result, you don't need to write a lengthy XPath when starting from the middle of the HTML DOM structure. An illustration would be /a[contains(text(), 'Katalon Studio')].
 

The document Configure XPath contains a collection of XPath capture techniques that use relative XPath in Katalon Studio.

Nested element

Element combinations are possible. The children of nested elements are the parent container.
 

For instance, the download link is a nested element in the script below.
 

<div class="container">
    <div class="navbar-collapse navbar-right">
        <div class="nav-bar-decorated">
            <ul>
                <li><a class="pbtn-download" href="#katalon-download">Download</a> <!-- Deeply nested element  --></li>
            </ul>
        </div>
    </div>
</div>

A nested element, like the <a> element in the script above, is challenging to recognize. Manually defining the XPath requires thorough familiarity with the web page's DOM structure.

Identify nested elements using XPath

Finding nested items with XPath is an efficient approach to locating them when common characteristics like ID, name, or class cannot be used to locate them. Whatever the level of nesting, Katalon Studio can produce and optimize XPath for HTML components. These XPaths allow you to find elements without looking through the DOM tree.
 

When you spy on the Make Appointment object in Katalon Studio, it automatically generates and optimizes XPath, as seen in the sample below (a nested element).

Deal with dynamically changing elements

Dynamic element

Modifying test scripts when the AUT changes is one of the difficult and time-consuming activities involved with test automation, particularly in the early stages of software development. From one build to the next, developers may modify IDs and items rather often. Additionally, the AUT components may vary dynamically while being executed.
 

Web elements that are dynamic have IDs and any additional attributes, like class names or values, that change each time you refresh the page. Database-driven or session-driven aspects are dynamic. For instance, changing one piece in a database can affect several different parts of the program that is being tested.

Deal with dynamic elements using XPath

You might not want to use absolute XPaths for these components in test cases if they are dynamic elements. Use relative XPaths based on specified patterns as an alternative.
 

From the current node context, various nodes in the XML document are searched for using XPath axes. All XPath axes are supported by Katalon Studio. Some typical techniques you can employ to find dynamic elements are shown in the table below:

Deal with dynamic elements using XPath

Frequently Asked Questions

Which framework is used in Katalon Studio?

Groovy is a Java-based language used by Katalon Studio. It requires the loading of numerous libraries in order to parse test data, test objects, and logging.

What is the purpose of Katalon Studio?

Without requiring coding, Katalon enables the creation and reuse of automated UI test scripts.

Is Java supported by Katalon?

To operate a Katalon Studio instance and create test applications. Katalon Studio makes use of the standard embedded Java Runtime Environment (JRE) version 8.

Conclusion

We have extensively discussed Detecting objects with XPath in Katalon in this article. We hope this blog has helped you enhance your knowledge. If you would like to learn more. Check out our articles on Advanced Guides in Katalon. Practice makes a man perfect. To practice and improve yourself in the interview. You can check out the Top 100 SQL problems and Interview experienceCoding interview questions and the Ultimate guide path for interviews.

ThankYou

 

Live masterclass