Selenium has no way of knowing what information displayed in the browser is in terms of its type. Is ABC a string or a hex number? So its all string data, or more accurately, its all unicode data. This can cause problems when trying to do asserts between things. Here is a quick example.

<pre lang="bash">Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = u'12345'
>>> y = 12345
>>> if (x == y):
...   print "sale!"
... else:
...   print("different!")
... 
different!
>>>

This leads to a lot of casting of variables from one format to another. Which is often just noise in your scripts. Treating data as an Element (see Elements and Actions) can solve this by doing the cast in only one spot only once.

<pre lang="python">class SearchResultsCount(Text):
    def __init__(self):
        self.locator = locators["total_search_results_count"]

    def __set__(self, obj, val):
        pass

    def __get__(self, obj, cls=None):
        s = super(SearchResultsCount, self).__get__(obj, cls)
        return int(s)

This particular example uses the Text super class provided by Saunter to illustrate this. In the overridden __get__ method the text value is fetched from the browser and is cast into an integer before every hitting the script (or other Action in the Page Object).

An even cleaner way to do this is to use the Number super class so the code would just be.

<pre lang="python">class SearchResultsCount(Number):
    def __init__(self):
        self.locator = locators["total_search_results_count"]

    def __set__(self, obj, val):
        pass

As for the __set__ method here, this particular element is read-only so should someone accidentally try to send data to it (which a user cannot do…) nothing happens. Though I suppose it could also be written to throw an exception of some sort but that is more of a user style thing.

Anyhow, once you have your data being returned in the right type you can easily start to do things like

<pre lang="python">self.assertEquals(12345, search_results_page.search_results_count)

and not have to deal with false reports saying that

12345 is not equal to 12345

which can hurt one’s brain the first couple dozen times you see it. A trick I’ve learned to debug that situation is wrap things in a list which will show its type when printed to the console.