3

I don't understand very well the purpose of normalize-space(). I think it's very useful. In fact, I always use it when I am doing tests, but I am not sure the principal purpose.

For example, in these two cases, what are the differences? What is the advantage of using it?

Example 1

WebElement seleccionLabelCabecera 
  = findBy(xpath("(//div[contains(normalize-space(@class), 
                                  'windowViewMode-maximized active')"]

Example 2

WebElement seleccionLabelCabecera 
  = findBy(xpath("(//div[@class, 'windowViewMode-maximized active')"]
kjhughes
  • 106,133
  • 27
  • 181
  • 240

2 Answers2

2

The normalize-space() function simplifies specification of tests against strings for which whitespace variations are insignificant.

In your examples, consider that additional whitespace before, between, or after the two class values ought not have bearing on whether your targeted div is found. By removing leading and trailing whitespace and consolidating internal whitespace to a single space, normalize-space() simplifies tests where whitespace variations do not matter.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
1

normalize-space xpath function will remove leading and trailing white space.

[source msdn] White space is normalized by stripping leading and trailing white space and replacing sequences of white space characters with a single space. If the argument is omitted, the string-value of the context node is normalized and returned.

Example:

<text>
    This is a
  test, with a   lot    of 
    irregular  spacing and
    waiting to be   normalizaed.


</text>

Result with out normalize-space

"

This is a

test, with a lot of

irregular spacing and

waiting to be normalizaed.

"

Result with normalize-space

"This is a test, with a lot of irregular spacing and waiting to be normalized."

You can get more detailed explanation in the below link

https://learn.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/ms256063(v=vs.100)

supputuri
  • 13,644
  • 2
  • 21
  • 39
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/22851646) – MG_Bautista Apr 25 '19 at 20:01
  • @MG_Bautista Thanks for the review and input. Added more details. – supputuri Apr 25 '19 at 20:09