Selenium-RC: make friends with CSS


Of course, XPath-locator is one of the most versatile and most accurate locators. But this universality is added to one of the main disadvantages of this type of sonar – low rate of detection of this element. This is the most well seen by IE, while the same Firefox works fine. This is due to the internal features of the browser, in particular in the way allotsirovaniya page elements. But is not the point. The bottom line is that Selenium tests, which make extensive use of XPath work extremely slow under IE. And this raises a number of problems with both speed test execution, and the quality of these tests, especially when working with dynamic content. As an alternative to XPath can be considered CSS locators. What can be done?

First, CSS-locators can also bind to the object hierarchy. For example, an XPath-locator:

1 xpath = / / div / / span / a

can be described through CSS like this:

1 css = div * span> a

Hence it can clearly be seen analogy items XPath and CSS, such as:

The "/", meaning the next level of the hierarchy of the object corresponds to the operator ">" in the CSS.
Sign "//", mean any element, located in the hierarchy below the current corresponds to the operator "*" in CSS
Secondly, as in XPath, CSS-locators can cling to the values ??of attributes. For example, an XPath-locator:

1 xpath = / / div [@ id = "some_id"]

can be described through CSS like this:

1 css = div [id = some_id]

It is also possible to check the overlap of the attribute value. But CSS is a bit limited. It is possible to verify that the attribute contains multiple words separated by spaces, but one of them clearly correspond to the value. That is, the fact that XPath can be expressed like this:

1 xpath = / / div [contains (@ title = "title")]

expressed through CSS like this:

1 css = div [@ title ~ = title]

Both the locator well catching element of the form <div title="some multiword title" />, at the same time, that’s an element: <div title="my_title" /> CSS-radar does not catch. Also, the CSS has attributes that can have its own definite simplified recording. For example, if we have an element specified by id, for example something like this: <div id=some_id />, then the CSS locator will look like:

1 css = div # some_id

In addition to the CSS separately, you can work with elements that have defined the attribute class. The fact that in XPath is written as follows:

1 xpath = / / div [@ class = "myclass"]

expressed through CSS like this:

1 css = div.myclass

This adds some convenience. More information about CSS can be read at http://www.w3.org/TR/CSS2/selector.html. Thus, a number of things CSS is similar to XPath with one major advantage – CSS running quickly regardless of the browser.

Of course, this type of radar has its limitations: the problems with search indexes, the problems with the transition up the hierarchy and so on. But in any case, if you need speed to run tests, it is desirable at the first opportunity to change the XPath to the CSS. A XPath is used where all other locators are powerless.

See Also

    Advertising

    Archives