Photo from Chile

Very nice xpath and css selector locator cheat sheet for Selenium

I was recently asked by a colleague for some advice on locators and I shared this cheat sheet with him. It contains just about everything you need to know for formulating locators for use with Selenium (and also Marionette), and includes syntax for both xpath and css selectors.

I hope you find it as useful as I do.

Selenium Locators Cheat Sheet

Please note that I am not the author of the cheat sheet, just someone who finds it very useful.

CFSelenium 1.6 - Upgrades for Selenium 2.2 and Firefox 5

A new version of CFSelenium was recently released which upgrades the bundled version of Selenium Server to version 2.2.0, and also provides Firefox 5 compatibility for the bundled Firefox plugin.

As always, the latest version is available via the CFSelenium RIAForge page and via the CFSelenium GitHub Project. The latter also includes a detailed Readme file which can help you get started using CFSelenium.

CFSelenium 1.5 - With Some Handy Helper Methods

A new version of CFSelenium was recently released which includes a few helper methods that I've found useful. First off, I'd like to thank the fine folks on the Mozilla Web QA team, from whom I borrowed the idea for these methods. I wrote a couple of tests for them in Python, and their Python base page object contains these methods. While I agree that it makes sense to include these methods in a base page object, not everyone using CFSelenium is using the Page Object Model, and as the creator and co-maintainer of CFSelenium I figured I'd just make everyone's life simpler and include them directly in the client library.

The three methods, which are waitForElementPresent, waitForElementVisible, and waitForElementNotVisible, are helpful when testing applications that make use of AJAX, or even just simple JavaScript. When you want to check that a page has responded to a user interaction, and the response is generated by JavaScript, you can often run into timing issues in your Selenium tests. If you issue a click command and then immediately include an assert with a getText command in it, it may be that the element that you are attempting to get hasn't been created by the browser yet. The same can be true when elements are shown or hidden. To address this issue the three methods mentioned above can be used, each of which is described in more detail below.

Arguments

Each of these methods accepts one required and one optional argument. The first argument, locator, is just that; the locator of the element that you want to wait for. The second, optional, argument is timeout, which is a number, in milliseconds, that you want CFSelenium to wait for the element before it throws an exception. The default value for timeout is 30000 (or 30 seconds), and this can be overridden in one of two ways. You can override the default for your entire test case by passing a value into the waitTimeout argument of CFSelenium's init method. You can also override the default on any call to any of these methods by passing a value into the second argument of the methods, which is timeout.

waitFor Methods

In case it isn't completely obvious, here is what the waitFor methods do:

  • waitForElementPresent - Waits for the specified element to be added to the DOM. If the element is not present by the end of the timeout interval an exception is thrown.
  • waitForElementVisible - Waits for the specified element to become visible. If the element is not visible by the end of the timeout interval an exception is thrown. Note that the element must be present in the DOM or the Selenium server will throw an exception immediately.
  • waitForElementNotVisible - Waits for the specified element to become hidden. If the element is still visible by the end of the timeout interval an exception is thrown.

As always, the latest version is available via the CFSelenium RIAForge page and via the CFSelenium GitHub Project. The latter also includes a detailed Readme file which can help you get started using CFSelenium.

CFSelenium 1.4 - Starts and Stops Selenium-RC Java Server For You

A new version of CFSelenium was recently released which includes an enhancement contributed by Marc Esher. In previous versions you had to manually start and stop the Selenium-RC Java server, but in this latest version CFSelenium is smart enough to start it for you, in the background, if it is not already started. You can also shut down the java server by calling the stopServer() method on the selenium.cfc component. This will only stop the Java server if CFSelenium was the one that started it.

Marc also added a base test case for MXUnit which includes code to start and stop the server. The base test case is called CFSeleniumTestCase.cfc and the code to both start the Selenium Java server, and to start a browser session is in the beforeTests() method. There is also code to stop the browser and stop the server in the afterTests() method. This will allow all of your tests to run using the same browser session, which is much faster that starting a new browser session for each individual test.

To create a test case which is based on this new base test case, just extend it and add your own beforeTests() method which sets a variable for the base url for your tests and then calls the beforeTests() method in the parent. It will look something like this:

view plain print about
1component extends="cfselenium.CFSeleniumTestCase" {
2
3    function beforeTests(){
4        // set the base url for your tests
5        browserURL = "http://github.com/";
6        super.beforeTests();
7    }
8
9    function myTest() {
10        // note that the variable selenium is available to you which refers to an instance of CFSelenium
11        selenium.open("/bobsilverberg/CFSelenium");
12        assertEquals("bobsilverberg/CFSelenium - GitHub", selenium.getTitle());
13        selenium.click("link=readme.md");
14        selenium.waitForPageToLoad("30000");
15        assertEquals("readme.md at master from bobsilverberg/cfselenium - github", selenium.getTitle());
16    }
17    
18}

If you want to use a browser other than the default (which is Firefox), you can also specify a value for the variable browserCommand which will control which browser is used.

The latest version is available via the CFSelenium RIAForge page and via the CFSelenium GitHub Project. The latter also includes a detailed Readme file which can help you get started using CFSelenium.

Slides for Introduction to Browser Automation and Testing with Selenium

cf.Objective() 2011 has come and gone, and by all accounts it was an awesome conference. I presented an Introduction to Browser Automation and Testing with Selenium, and I'm making my slides available via this blog post. You can download the PDF here.

The real meat of the session consisted of demos of Selenium-IDE, Selenium-RC and CFSelenium, so I'm not sure that you'll get much from the slides alone. If you're interested in using Selenium via ColdFusion, please visit the CFSelenium project and look over the readme file. It should give you all you need to get started writing your own functional tests with ColdFusion and Selenium.

Export Selenium-IDE Scripts in CFSelenium (ColdFusion) Format

Hot on the heels of the release of CFSelenium, I have created a Firefox plugin that will allow you to export scripts from Selenium-IDE in CFSelenium format, wrapped inside an MXUnit test case. The plugin is dead simple to use, simply add the extension to your installation of Firefox and you'll see a new menu item in Selenium-IDE under File - Export Test Case As..., Options - Format and Options - Clipboard Format. The menu item is labelled CFML (ColdFusion) Selenium RC (MXUnit). Choosing this menu item will translate your current test case into CFSelenium format, wrapped in an MXUnit test case.

The Firefox extension is available in the distribution at both GitHub and RIAForge, and will hopefully be made available at SeleniumHQ once it's found to be stable. I'm sure it could use some improvements, so please give it a try and let me know what you think of it.

I was able to create the Firefox extension by basically copying and modifying the source code for the Grails Formatters created by Rob Fletcher, so a big thank you goes out to him for making the job simple and painless.

Introducing CFSelenium - A Native ColdFusion Client Library for Selenium-RC

We were very lucky to have Adam Goucher, a member of the core Selenium team and the maintainer of Selenium-IDE, as a speaker at the Toronto ColdFusion User Group this month. Adam delivered an awesome presentation about automated testing in general and Selenium in particular.

During the session I asked a bunch of questions about the architecture of the Selenium components, in particular Selenium-RC and WebDriver, which is now part of Selenium 2.0. Through our conversation I came to understand that it would be fairly simple to create a native ColdFusion client library for Selenium-RC, so I decided to give it a try. Thankfully it was as simple as I expected and I decided to complete the library so that the full API of Selenium-RC is supported.

Thus CFSelenium was born and was given a home at GitHub. If you have no idea what I'm talking about, fear not, I will provide some more background information on Selenium and Selenium-RC in the rest of this post.

[More]

More Entries