Latest post from this blog

Test Scenarios vs. Test Cases: Understanding the Basics

What Are Test Scenarios Test scenarios represent high-level ideas or conditions that need to be validated to ensure the application works as expected. They provide a broader perspective and are typically used during the test planning phase. Purpose: To capture the "what to test" without going into granular details. Example of a Test Scenario: Verify that a user can successfully log in to the application using valid credentials. Verify the behavior of the login page when invalid credentials are entered. Verify the application behavior when the login button is clicked without entering any credentials. What Are Test Cases Test cases are detailed documents that define the specific steps to execute a test. They cover inputs, execution steps, expected results, and actual outcomes. Purpose: To guide the tester step-by-step on "how to test." Example of a Test Case (for the first scenario): Test Case ID TC_01_Login_Valid_Credentials Test Scenario Verify user login with val...

Normalize-space Function In Xpath

Introduction 
When working with XPath, handling whitespace in text nodes can sometimes be a challenge. The normalize-space() function is a powerful tool to simplify text processing by removing unnecessary spaces and ensuring consistent string formatting.

In this blog, we’ll explore what normalize-space() does, its syntax, and practical use cases in automation.

What is the normalize-space() Function:
The normalize-space() function in XPath trims leading and trailing whitespaces and reduces multiple spaces between words to a single space. It is useful for matching text nodes with inconsistent whitespace formatting.

Syntax:
normalize-space(string)
  • string: The input text to be normalized. If omitted, it defaults to the current node’s text.
Key Features:
  • Removes leading and trailing spaces.
  • Converts multiple spaces between words into a single space.
  • Works seamlessly with text nodes and string attributes.
Example:
 <div id="example">   Hello World. </div>

Without normalize-space():
//div[@id='example' and text()='Hello World']

This XPath would fail because of the extra spaces.

With normalize-space():

//div[@id='example' and normalize-space(text())='Hello World']

This XPath correctly identifies the element by normalizing the text content.

When to Use normalize-space() in Automation
  • To handle inconsistent spacing in text nodes during web scraping or UI testing.
  • When comparing text values where whitespace variations may cause false negatives.
  • To clean up text before extracting or asserting its value.
Advantages
  • Simplifies handling of irregular whitespace.
  • Makes text comparisons more robust.
  • Reduces the need for manual text cleanup in automation scripts.
Conclusion
The normalize-space() function is an invaluable tool in XPath for handling text nodes with inconsistent spacing, making it easier to write robust and reliable locators for automation and data extraction tasks.

Comments

Popular posts from this blog

Common Exception in Selenium

Test Scenarios vs. Test Cases: Understanding the Basics

Handling Shadow DOM in Selenium WebDriver

Handling HTTPS Websites and SSL Certificate Errors in Selenium