Tuesday, August 5, 2014

XPath and case insensitive string comparison

Yo, test engineers!

Say we have a web page with a list of genres in a book shop. And we want to make sure that "Sci-Fi" genre is in the list.
Create a test that finds an element with "Sci-Fi" text on it.

 XPath for the test could look like:  "//*[@id='BookGenres'][ . = 'Sci-Fi']"

Bingo. Test works t works!

Let's say we have another shop with it's own web pages, but genres are in capital letters - i.e. "SCI-FI".

The test here stops working which makes test engineer upset.

To make test engineer we could compare strings case-insensitive. With XPath 1.0 it could be difficult.

Let's use "translate" function which takes following arguments:
(
string_to_transform, 
what_element_of_string_to_transform, 
how_to_transform_elements
)

Here is a new XPath and test will work on both "Sci-Fi" and "SCI-FI"

"//*[@id='BooksGenre'][translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = 'Sci-Fi']"

Happy testing!

No comments:

Post a Comment