Selenium Tips: CSS Selectors in Selenium Demystified

January 29th, 2010 by Santiago Suarez Ordoñez

Following my previous TOTW about improving your locators, this blog post will show you some advanced CSS rules and pseudo-classes that will help you move your XPATH locators to CSS, a native approach on all browsers.

Next sibling

Our first example is useful for navigating lists of elements, such as forms or ul items. The next sibling will tell selenium to find the next adjacent element on the page that’s inside the same parent. Let’s show an example using a form to select the field after username.

</input> </input>

Let’s write a css selector that will choose the input field after “username”. This will select the “alias” input, or will select a different element if the form is reordered.

css=form input.username + input

Attribute values

If you don’t care about the ordering of child elements, you can use an attribute selector in selenium to choose elements based on any attribute value. A good example would be choosing the ‘username’ element of the form without adding a class.

</input> </input> </input> </input>

We can easily select the username element without adding a class or an id to the element.

css=form input[name='username']

We can even chain filters to be more specific with our selections.

css=input[name='continue'][type='button']

Here Selenium will act on the input field with name=”continue” and type=”button”

Choosing a specific match

CSS selectors in Selenium allow us to navigate lists with more finess that the above methods. If we have a ul and we want to select its fourth li element without regard to any other elements, we should use nth-child or nth-of-type.

    <p>Heading</p>
  • Cat
  • Dog
  • Car
  • Goat

If we want to select the fourth li element (Goat) in this list, we can use the nth-of-type, which will find the fourth li in the list.

css=ul#recordlist li:nth-of-type(4)

On the other hand, if we want to get the fourth element only if it is a li element, we can use a filtered nth-child which will select (Car) in this case.

css=ul#recordlist li:nth-child(4)

Note, if you don’t specify a child type for nth-child it will allow you to select the fourth child without regard to type. This may be useful in testing css layout in selenium.

css=ul#recordlist *:nth-child(4)

Sub-string matches

CSS in Selenium has an interesting feature of allowing partial string matches using ^=, $=, or *=. I’ll define them, then show an example of each:

^= Match a prefix
$= Match a suffix
*= Match a substring
css=a[id^='id_prefix_']

A link with an “id” that starts with the text “id_prefix_”

css=a[id$='_id_sufix']

A link with an “id” that ends with the text “_id_sufix”

css=a[id*='id_pattern']

A link with an “id” that contains the text “id_pattern”

Matching by inner text

And last, one of the more useful pseudo-classes, :contains() will match elements with the desired text block:

css=a:contains('Log Out')

This will find the log out button on your page no matter where it’s located. This is by far my favorite CSS selector and I find it greatly simplifies a lot of my test code.

Tune in next week for more Selenium Tips from Sauce Labs.

Sauce Labs created Sauce OnDemand, a Selenium-based testing service that allows you to test across multiple browsers in the cloud. With Selenium IDE and Selenium RC compatibilities, you can get complete cross-platform browser testing today.

  • Share/Bookmark

Selenium Tips: HTTPS and self-signed certificate exceptions

January 22nd, 2010 by Santiago Suarez Ordoñez

This week in our support email, help@saucelabs.com, we’ve seen a considerable number of cases of problems with Firefox and the Invalid Certificate warning thrown when a development environment is using the production’s certificate for HTTPS URLs, which causes the browser to wonder about the website’s identity.

Invalid Certificate warning

Warning displayed by Firefox 3.5 in this situation

If you do much browser-based testing, you have surely dealt with this situation more than once. In the manual testing world, you just tell your browser to add an exception for that certificate and ignore further errors, and you can forget about seeing that annoying warning anymore.

But when you move on to automation in the Selenium world, things are not always that simple. In most browsers, adding a certificate exception will work, as Selenium shares the same session as the user and will find the exception as you do. In Firefox, though, Selenium RC creates a special profile each time the browser is started and there’s no trail of the user settings in it.

The workaround for this problem is to create your own Firefox profile, with the specific certificate added on it by hand, and then tell Selenium to launch the browser based on that profile.

Another interesting approach, the one we take at Sauce Labs, where we can’t do this kind of trick, because we just don’t know which certificate the user will need before their test starts, is the use of RCE (Remember Certificate Exception), which is  a plugin that will automatically detect the warning and make the browser go through it, returning the control to Selenium after 4 or 5 seconds. Notice that if you use this approach, you will need to make sure your tests will tolerate this 5 secs additional delay to open the page.

Note: We currently support RCE in our Firefox 3.0 machines, and will be porting this extension to Firefox 3.5 soon.

You can find more info about RCE on its plugin page or in the author’s blog post.

Update: Adam Goucher, one of the big minds in the testing world just wrote a blog post that we couldn’t have written better ourselves about the first and most important advice regarding HTTPS and testing: Do yourself a favour and don’t test using HTTPS

  • Share/Bookmark

Zero day Firefox 3.6 support in Sauce RC, Selenium RC

January 21st, 2010 by Sean McQuillan

Today Sauce Labs released a new version of Sauce RC supporting Firefox 3.6, which was released by Mozilla this morning. Sauce Labs also sponsored work on Selenium RC to support Firefox 3.6. In under 12 hours, Sauce RC has added support for a brand new browser, allowing customers to keep their websites on the cutting edge of browser support.

Sauce RC is Sauce Labs’ commercially supported version of Selenium RC that includes simplified install and administration. Sauce RC is backed by a dedicated team of engineers to ensure bugs are fixed promptly and new browsers are supported.

Congratulations and thanks to Adam Goucher for his effort in making this happen in record time.

  • Share/Bookmark

Ridiculously easy cross-browser testing with Sauce IDE

January 20th, 2010 by Jason Huggins

Riding on the coattails of yesterday’s official Selenium IDE 1.0.4 release, we’re very excited to announce Sauce IDE 1.0 (beta 1). Download it here. Sauce IDE is Sauce Labs’ certified, enhanced and commercially-supported version of Selenium IDE.

With Sauce IDE, we’ve made cross-browser cloud testing ridiculously easy. After you record a test in Sauce IDE, you’re only one click away from replaying that test on any major browser on Windows or Linux. Sauce IDE allows users to tap directly into our Sauce OnDemand cloud-hosted Selenium service while getting access to a sophisticated Selenium test infrastructure without having to build and maintain the infrastructure yourself.

Another really cool feature is video recording and playback. With every browser test you run with Sauce IDE on the Sauce OnDemand cloud, you can view or download a full screen video of your test.

Sauce IDE is a plugin for Firefox, and we’ve also included Firefox 3.6 support in our latest release.

Here’s a quick screencast showing how easy it can be to start running cross-browser tests in the cloud.

- Jason Huggins

  • Share/Bookmark

Selenium IDE 1.0.4 is released!

January 19th, 2010 by Adam Goucher

Since my work on Selenium-IDE was sponsored entirely by Sauce Labs, it makes sense to announce the newest release on this blog.

Download Selenium-IDE 1.0.4 now.

1.0.4 is the first release with new features in quite some time, and hopefully marks a renewal of the project. I’m going to try and push out a new build mid-month so get your bugs, patches and feature requests in.

So what is in the 1.0.4 release? A good question…

  • Firefox 3.6 – The previous release of Se-IDE worked with Firefox releases up to and including the 3.5 series of browsers. This version enables support for the 3.6 series of releases by default so people don’t have to do some of the hacks that are floating around.
  • Removed deprecated formatter – There was a Ruby formatter that had been marked as deprecated for a while in Se-IDE. It has been removed. If you are exporting from Se-IDE to Ruby, you should be using the officially supported Ruby formatter.
  • Update to Ruby formatter – The officially blessed Ruby Se-RC client is the selenium-client gem. The Ruby formatter has been updated to use its format.
  • RSpec – Ruby, ruby and more ruby. RSpec is now part of the standard set of formatters Se-IDE ships with.
  • Reload user-extensions – Jérémy Hérault has solved a problem some more advanced Se-IDE users experience by adding the ability to reload user extensions without having to restart Se-IDE. To enable this feature, check ‘Activate Developer Tools’ in the Options screen.
  • API – In order to help kick-start more companies using Se-IDE as their testing platform, Se-IDE has started to grow an API that other addons can use. Think of this as the Firebug model where Se-IDE will provide the structure, and other groups will provide additional functionality and features. Here are the first two things the new API can do.
    • Extend the Selenium API – There is a community of developers who have extended the Selenium API so new commands can get added to the IDE’s command list. (I’m a big fan of the Random String one.) You can now bundle user extensions up as a plugin to Se-IDE.
    • Formatters – Se-IDE ships with a number of default formatters that it can display or export to. But what if the language you use isn’t listed, or you have a custom test runner that requires different information? The solution to that has always been to install a custom formatter, but making sure that everyone in the group has the current, correct version installed is a pain. Instead, you can now put your custom formats in a plugin that is distributed to people.

    The documentation for this API is still very much a work in progress, but this series of blog posts should be enough to get people started on it. Eventually it will be rolled into the Official Selenium Documentation.

For issues with this release or features you would like to see in future releases, please log them in the Google Code Issue tracker using the label ide.

This release has also been submitted to addons.mozilla.org to be distributed through the normal plugin update system. We do not have any control over how long it will take for that update to be processed and released. For those who need it now, here is the direct download link on SeleniumHQ.

-Adam Goucher

  • Share/Bookmark