Mastering Xpath for Web App Test Automation
Those who are working on test automation projects & and willing to start working in automation testing may have come across the word “Xpath. In today’s article, we will see the need for Xpath, what Xpath is, why it plays an important role in automation testing, and how to find a dynamic robust Xpath without using third-party plugins.
Everything on the web page (like text fields, headers, titles, buttons, scroll bar, drop-down fields, etc.) are elements. These elements are known as web elements. Web elements are specified in HTML by the tag, attributes, and contents. Few elements can have CSS applied (like colors, size, positions, etc.)
Being human users, we can see all these elements present on the webpage and can perform operations (like click, select, getText, scroll, etc.) manually. But when we need to interact with these elements or to perform any operation(action) like inserting input value in a text box, or text area, selecting a checkbox, selecting an option from a drop-down, etc. using test automation scripts, the web elements need to be identified uniquely and can be used to perform the required action events. The web element locator finds and returns the Web elements.
As per the Selenium official documentation below are the locators available,
Locators like id, name, and class name are generated dynamically at run time with some static and random data appended. The developer follows this strategy to make the web pages more robust. Due to this dynamic content test automation developer ID, name, and class name locating the web element will not be the right choice.?
Test automation developers have two best options, one is Xpath, and another is CSS Selector. Generally, in automation projects test automation developer prefers to use the XPath locator to find the elements on the webpage using HTML DOM structure because every element has its unique XML path, and using XML path we create dynamic Xpath ().
Xpath :
Understanding these concepts is vital to excel in web app testing, ensuring reliable and efficient test automation.
Types of XPath:
Begins with the root element and includes the complete path to the desired element. While it provides an exact location. Absolute XPaths are not preferred in automation testing, as they fail with a single change in the DOM.
Starts from the node you want to target and uses concise expressions to locate elements based on their relationships. It is preferred for better maintainability. Relative Xpath is preferred over Absolute, as it consists of the relative path that directly navigates to the desired element without having the entire path.
?Let’s take an example of a Google search page for a better understanding of the difference between two Xpath types:
Absolute XPath : /html/body/div[1]/div[3]/form/div[1]/div[1]/div[1]/div/div[2]/textarea
Relative XPath : //textarea[@title='Search']
Xpath syntax in Selenium:
领英推荐
//Tagname[@AttributeName=’AttributeValue’]
?Note: Tagname, attribute name, values, etc. are case-sensitive and must be the same as given in the DOM structure.
Axes methods in XPath and how to use them:
These axes help you navigate and target specific nodes within an XML or HTML structure when using XPath expressions for querying or extracting information.
XPath Methods:
XPath provides several useful methods to handle attribute values and text content. Some key methods include:
Logical Operators:
XPath supports logical operators to combine multiple conditions. Commonly used operators are:
With Text:
Indexing:
XPath allows selecting elements by their position using indexing.
e.g.-
1] (//li)[1]?-?Selects the first <li> element in the document.
2] (//table)[last()]?- Selects the last <table> element in the document.
Working with input, checkbox, dropdown, etc.:
When working with different types of elements like the input box, check box, dropdowns, etc., the xpath must navigate to the element with its respective tagName.
Ref link:
Simple and best way to find a Locator by using free chrome extension tool https://chromewebstore.google.com/detail/find-my-locator/dcbgkkkekedfhfkopmcpgekglckkgmgj?hl=en&authuser=0