Latest post from this blog

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

In real-time automation projects, test execution is never limited to a single browser or a single environment . Applications must be validated across multiple browsers (Chrome, Firefox, Edge) and multiple environments such as QA, UAT, and Staging before going live. A well-designed Selenium + Java + Cucumber automation framework should allow testers to switch browsers and environments easily without changing test scripts . This blog explains how to manage test execution efficiently across different browsers and environments using best practices followed in real projects. Why Multi-Browser and Multi-Environment Testing Is Important Different users use different browsers QA, UAT, and Staging environments have different configurations Bugs may appear only in specific environments or browsers Same test cases must be validated everywhere before production release Common Challenges Testers Face Hardcoded browser names and URLs Maintaining separate test scripts for each environment Browse...

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

How to Manage Test Execution Across Different Browsers and Environments (QA, UAT, Staging)

Purpose of the StepData Class in Selenium + Java Cucumber Automation Framework

Ensuring Thread Safety in Parallel Test Execution with Selenium, Cucumber, and Java