Thursday, February 11, 2010

Selenium 101

So you want to learn about Selenium for web testing.

Selenium comes in 3 parts.


  1. Selenium-IDE
  2. Selenium-Core
  3. Selenium-RC

The IDE is a Firefox plugin. You open the IDE and it starts recording what you do inside the browser. The information is stored in an HTML table with one action per row and three columns per action. The first column is the action, the second column is the locator and the third column is optional parameter.

For example, if I click a link the first column will be 'click', the second column will be some way of uniquely identifying the link and the third column will be blank. Another example, if I enter text into an INPUT the first column will be 'type', the second column will be some way of uniquely identifying the text field and the third column will be the text I entered into the field.

You could actually write Selenium test cases using an HTML edit and creating a TABLE but it would be hard for you to know all the actions and you'd have to have some way of determining locators.

When Selenium IDE is running, you can right click on elements and Selenium actions will be available on the context menu. Things like verifyTitle. When you select these, they are added to the Selenium IDE.

From the Selenium IDE you can save what you have recorded. You could actually record everything you do, save it and then load it and play it back later. By default, it will save an HTML TABLE but you can change the settings for Selenium IDE and save it in different languages. I use Java so I save my recording as jUnit.

The whole idea behind Selenium Core is to use javascript, in the Selenium window, to drive the window displaying your web application. So when you load a script into Selenium IDE and run it, the IDE translates the commands in the table to actions using the Selenium Core. If you install the IDE you automatically have the Core.

Selenium RC (Remote Control) allows you to play the scripts from a remote machine. It doesn't have to be a remote machine. It can be the same machine and usually is for development. The Selenium RC comes in two parts: the client and the server.

If you saved the recording as HTML, you pass the HTML script in on the command line directly to the server. To run the server you use Java. The server is an executable jar. To run the server I would use:

java -jar selenium-server.jar -help

This will display a help message. If I run it with:

java -jar selenium-server.jar -htmlSuite myscript.html

It will run the Selenium IDE recording (assuming the file myscript.html contains a file I saved from Selenium IDE). If I save the file as Java jUnit I will have to create a class to run the file. I would go into my favourite editor (today it is Eclipse), create a project, add the Java file created from Selenium IDE as a jUnit 3 file. To allow the project to talk to the Selenium Server I would add the Selenium Java Client to my Java project. Now when I run the jUnit it will create an instance of Selenium and send the appropriate commands to the Selenium Server. The server needs to be running before you run the test case. To run the server I would just use:

java -jar selenium-server.jar

If I need to change the port or host there are command line switches I can add but the -help will show you those. If you don't give a port or host it will default to port 4444 and host localhost (so the server and client will be on the same machine).

If you don't want to use Java and you want to use say Perl, you can save the recording from the IDE in Perl the use the Selenium Perl Client to talk to the Selenium Server.

That is essentially Selenium in a nutshell.

2 comments:

Note: Only a member of this blog may post a comment.