Selenium IDE Tutorial – Part 2

In part 1 of this tutorial you have seen how to write a simple web test using Selenium. In this part we will see how to use the test generated using the IDE with Selenium RC and PHPUnit.

In this part we will use Delicious as our test target. I’ve already created a small Selenium test that you can use. You can download the HTML source from here.

Starting up.
Restart the Selenium IDE. In the source tab replace the default HTML with the one from the above file. Set the Base URL to After doing this your IDE should like below.

selenium ide tutorial 2

If you are on a slow internet connection than it may help to slow the test speed.

So what exactly does this test do? Simple:

1. First it opens the delicious home page
2. Then it searches by the keyword ‘selenium’
3. Reads the total number of bookmarks from the results page as shown below

Now this may not look like a test for you , but it is just an exercise in using Selenium, you will surely apply it to a good test case. Onwards…

Before running this test make sure that you are logged out of Delicious.
Now run the selenium IDE test. After the test is successfully completed this is what you should see in the log pane.

Is that all?
If this was all there was I wouldn’t be really writing this post. The interesting part comes now – running the same test from PHP. From the file menu select ‘Export Test Case As…’ and as we are using PHP, select ‘PHP – Selenium RC’. Save the file by the name ‘Example.php’.

This is what we will get in the ‘Example.php’ file.

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';

class Example extends PHPUnit_Extensions_SeleniumTestCase
  function setUp()

  function testMyTestCase()
    $this->type("homepage-searchinput", "selenium");

A short notice.
Before we proceed a few points to consider. Make sure that the file name and the class name are the same, here ‘Example’. Say if you change the class name to ‘Delicious’ make sure that you also change the file name to ‘Delicious.php’.

What the code basically does is it initializes the browser to ‘chrome’ (you can set the browser to your preference by changing to ‘*iexplore’ or ‘*firefox’); sets the baseurl to ‘’ and runs the test.

Whats the complex looking line!?
Everything may look fine to you except maybe the last line – ‘$this->getText…’. The function ‘getText’ returns the text found at the element specified in the expression. The complex looking line is an xpath expression to the results element:

You don’t have to rake your brains to find an xpath to an element. An easy way is to use the XPather Firefox addon to get the xpath of any element on a page. After you download and install the addon, restart Firefox, right-click on any element and from the context menu select ‘Show in XPather’. XPather retrieves the xpath to the specified element, which you can than use in your test code.

Note: This site uses ‘delicious’ as an example, so even some minor changes made by them to their site will render the xpath invalid, throwing an ‘element not found’ error in selenium. If this happens please notify me or you can find the new xpath using the Xpather plugin.

Downloading and installing Selenium RC
Selenium RC is a Java based command line server that starts browsers and runs commands you pass from your tests.

1. First make sure you have a Java runtime installed on your machine.
2. Download Selenium RC from here.
3. After extracting the files from the archive copy the ‘selenium-server.jar’ file to any directory you feel appropriate. I copied it to my PHP installations bin directory.
4. Start the Selenium RC server from the command-line by issuing the following command:

java -jar selenium-server.jar

This will start the server on port 4444.
5. Now the server is ready to accept test commands from your PHP script. Make sure you keep this server running till you finish testing.

Installing PHPUnit
1. An easy way to install PHPUnit is to use the PEAR installer. The PEAR channel ( is used to distribute PHPUnit so make sure that it is registered with your local PEAR environment:

pear channel-discover

After the channel is registered install PHPUnit:

pear install phpunit/PHPUnit

Actual testing
Now that PHPUnit is installed and the Selenium RC server is up and running, its time to run our test we saved before in our ‘Example.php’ file. Type the following on your command-line:

phpunit Example

This will start the test. The PHPUnit Selenium driver will execute each test command from your file and send it to the Selenium server, which does the job of launching the appropriate browser, opening web pages, and performing various specified actions; and closing the browser after the test completes.

Now the above test does basically nothing important. So we will add some simple conditional statements to the code.

    $bookmarks = $this->getText("xpath=/html/body[@id='index']/div
    echo ($bookmarks > 3000)? "On the top" : "Still not there";


This is what PHPUnit has to say about the above test.

Or we can use an assertion as below, deliberately rigged to fail the test:

    $bookmarks = $this->getText("xpath=/html/body[@id='index']/div

    $this->assertGreaterThan(33000, $bookmarks);



Now PHPUnit will shout failure:

You can look at more functions to implement in your tests here and here.

This concludes the second part of the tutorial.

61 thoughts to “Selenium IDE Tutorial – Part 2”

  1. hello Sameer…

    Thanks for this post.I have tried it and it is running perfactlly well.
    Could you plz write a same post for java(JUNIT)-selenium Rc.
    I am eagerly waiting for that.

    Thanks & regards

  2. Hi Sameer

    Can you please tell me, how can I generate unique email address through selenium.

    I have to use this for placing orders on a shopping cart system as new users. So i need new email address to be entered at each and every time when i play the recorded test.

    Thanks a lot

  3. helpppppp please ı dont understand because my english not very good but ı must learn selenium please help me

  4. Is it possible to automate share point people picker control through selenium.
    When i record through IDE it is detecting the people picker sharepoint control through ID, but while play back it is not detecting the people picker sharepoint control. please let me know if there is a solution.

  5. Ohh, ok no problem, here the actual issue is with IE, when i play back in firefox it is working fine, but its not working in IE.

  6. Hi Sameer,

    I try now work with Selenium IDE, but I have one problem. On the main page is button/link which create a new panel – and I need to work/check with information in new panel, but I have a big problem with SelectWindow – better when i replay test in Selen IDE it didnt work (I tried to set correct name of the page, but didnt help) …

Comments are closed.