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 http://delicious.com/. 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.

<?php
 
require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
 
class Example extends PHPUnit_Extensions_SeleniumTestCase
{
  function setUp()
  {
    $this->setBrowser("*chrome");
    $this->setBrowserUrl("http://delicious.com/");
  }
 
  function testMyTestCase()
  {
    $this->open("/");
    $this->type("homepage-searchinput", "selenium");
    $this->click("homepage-searchsubmit");
    $this->waitForPageToLoad("30000");
    $this->getText("xpath=/html/body[@id='index']/div
                         [@id='doc3']/div[@id='bd']/div
                         [@id='yui-main']/div[@id='content']/h3/div/em");
  }
}
?>

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 ‘http://delicious.com/’ 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 (pear.phpunit.de) is used to distribute PHPUnit so make sure that it is registered with your local PEAR environment:

pear channel-discover pear.phpunit.de

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
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em");
 
    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
                       [@id='doc3']/div[@id='bd']/div
                       [@id='yui-main']/div[@id='content']/h3/div/em");
 
    $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.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

61 Responses

1

Sameer Borate’s Blog: Selenium IDE Tutorial - Part 2 : WebNetiques

November 25th, 2008 at 4:02 am

[...] Borate has part two of his look at using the Selenium IDE in testing PHP [...]

2

Tims Blog » Blog Archive » Fantabulous idea

November 25th, 2008 at 2:36 pm

[...] selenium ide tutorial [...]

3

R. M. Shiblee Mehdi

December 6th, 2008 at 1:52 am

I was expecting more on IDE tutorial.

4

Pooja

December 8th, 2008 at 4:36 am

Hi, I m not able to playback the script involving a YUI object using Selenium IDE, Do I need to use CCS Selector? Can you please guide me with an example

sameer

December 9th, 2008 at 9:27 pm

You need to use the Selenium UI-Element Locator extension, a good resource is located here:

http://ttwhy.org/home/blog/2007/05/12/selenium-ui-element-locator/

6

Nguyen

January 16th, 2009 at 1:57 am

Hi,

I did the same as your guide. However, I always receive message “Class PHPUnit_Extensions_SeleniumTestCase could not be found in Example.php”.

I have still stuck with it. Please help me asap.

Thanks so much for your time.

sameer

January 16th, 2009 at 2:46 am

Its probable that some of the phpunit files didn’t get copied, ‘SeleniumTestCase.php’ here. Why don’t you try a manual install.

Got to ‘http://pear.phpunit.de/get/’ and download version 3.0.0 from there. Extract them and copy the files to your PHPUnit directory which must be located in your PHP includes path. Make sure you make a backup copy of your original PHPUnit directory first.

8

sree

January 29th, 2009 at 5:38 am

can we have one more tutorial ,where u can demonstrate the same test from Java/Junit?

9

Scal

March 4th, 2009 at 4:07 am

Hey;
found your article very good; I was wondering if you can tell if it’s not possible to call a local host (127.0.0.1) in HTTP mode to launch Selenium test instead of manual command prompt calls?
Thanks

10

Jeffy

March 9th, 2009 at 12:07 am

When I run phpunit Example

I get this message

RuntimeException: Could not connect to the Selenium RC server.

Do you know why it is ?

sameer

March 9th, 2009 at 12:23 am

Are you using Windows. If yes then which version; is the Windows personal firewall turned on? It could be blocking port 4444.

12

Jeffy

March 9th, 2009 at 1:58 am

I am using Windows xp and firewall was turned on and I gave the allow option when the pop up asked to allow or block..

13

saujanya

April 13th, 2009 at 3:21 am

Could you please demonstrate with an example where selenium. can we test .NET application with selenium. I really wanted to for .NET application selenium RC can be used or not?

sameer

April 13th, 2009 at 5:53 am

15

saujanya

April 14th, 2009 at 4:12 am

Thanks sameer, the links were really very helpful. Hope to see some more blogs on selenium from you.

16

phpdeveloper

April 28th, 2009 at 3:47 am

Your blog is very useful to learn the testing.

But when I followed same steps I’m facing error as below:

(at server side)
14:48:07.057 WARN – GET /selenium-server/driver/?cmd=getNewBrowserSession&1=%2Ac
hrome&2=https%3A%2F%2Fwww.sitename.com%2F HTTP/1.0
java.lang.RuntimeException: Firefox refused shutdown while preparing a profile
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.wai
tForFullProfileToBeCreated(FirefoxChromeLauncher.java:290)
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.lau
nch(FirefoxChromeLauncher.java:143)
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.lau
nchRemoteSession(FirefoxChromeLauncher.java:329)
at org.openqa.selenium.server.BrowserSessionFactory.createNewRemoteSessi
on(BrowserSessionFactory.java:312)
at org.openqa.selenium.server.BrowserSessionFactory.getNewBrowserSession
(BrowserSessionFactory.java:113)
at org.openqa.selenium.server.BrowserSessionFactory.getNewBrowserSession
(BrowserSessionFactory.java:78)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.getNewBrowse
rSession(SeleniumDriverResourceHandler.java:653)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.doCommand(Se
leniumDriverResourceHandler.java:410)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handleComman
dRequest(SeleniumDriverResourceHandler.java:388)
at org.openqa.selenium.server.SeleniumDriverResourceHandler.handle(Selen
iumDriverResourceHandler.java:135)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1530)
at org.mortbay.http.HttpContext.handle(HttpContext.java:1482)
at org.mortbay.http.HttpServer.service(HttpServer.java:909)
at org.mortbay.http.HttpConnection.service(HttpConnection.java:816)
at org.mortbay.http.HttpConnection.handleNext(HttpConnection.java:982)
at org.mortbay.http.HttpConnection.handle(HttpConnection.java:833)
at org.mortbay.http.SocketListener.handleConnection(SocketListener.java:
244)
at org.mortbay.util.ThreadedServer.handle(ThreadedServer.java:357)
at org.mortbay.util.ThreadPool$PoolThread.run(ThreadPool.java:534)
Caused by: org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher$Fil
eLockRemainedException: Lock file still present! C:\DOCUME~1\LOCALS~1\Te
mp\customProfileDir3dcc7040497a4e90853a27ade6194c53\parent.lock
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.wai
tForFileLockToGoAway(FirefoxChromeLauncher.java:247)
at org.openqa.selenium.server.browserlaunchers.FirefoxChromeLauncher.wai
tForFullProfileToBeCreated(FirefoxChromeLauncher.java:288)
… 18 more

(and at client side)

PHPUnit 3.2.15 by Sebastian Bergmann.

E

Time: 23 seconds

There was 1 error:

1) testMyTestCase(Donation1FF)
RuntimeException: Could not connect to the Selenium RC server.

FAILURES!
Tests: 1, Errors: 1.

/////
ie it looks like it is having problem with creating firefox profile…
So guide me to solve this problem

sameer

April 28th, 2009 at 9:28 pm

Instead of using Firefox try IE.

change:

$this->setBrowser(“*chrome”);

to this:

$this->setBrowser(“*iexplore”);

18

rohan

May 26th, 2009 at 5:01 am

very informative,,waiting for the next part,,do try to include a brief regarding few important selenium commands and the rollup rules,,keep it up,,

19

Nec Soriano

August 5th, 2009 at 4:32 am

Hi Sameer,

I’m hoping you could help me on this.

I’m new to selenium IDE, I tried following your sample for the tutorial part 2. Every time I try to run an IDE test i get the following error:
[error] Element xpath=/html/body[@id='index']/div[@id='doc3']/div[@id='bd']/div[@id='yui-main']/div[@id='content']/h3/a/em[1] not found
And, for that I wasn’t able to proceed for the .php stuff.

I hope to get an answer from you.

Thanks in advance.

sameer

August 5th, 2009 at 10:07 pm

Well delicious has made some minor changes to their site since this post was written, so the previous xpath no longer works. Here is a new xpath for the same.

xpath=/html/body[@id='index']/div[@id='doc3']/div[@id='bd']/div[@id='yui-main']/div[@id='content']/h3/div/em

I’ve also changed the source in the post, so you can also download it from there.

21

Favio

August 7th, 2009 at 1:31 am

Hi,

your tutorial is awesome, thanks for making it, it helps a lot for newbies like me. :)

22

Nec Soriano

August 7th, 2009 at 9:34 pm

Hi Sameer,

Thanks for responding, I appreciate it. I will try the new xpath and let you know how I go with it.

Regards.

23

sundar

August 8th, 2009 at 1:22 am

Hi sameer

your Blog information is very useful to all. i would like to ask one question. i hope u will suggest me a good solution. how to use while loop in selenium test runner functional test tool.

24

Kumar

August 11th, 2009 at 7:38 am

Hi,

I wrote test case to click on download button. How to go to download confirmation pop up window and select save or open option, and click ok and come back to main window. I am using selenium-java client for web test automation.

Thanks

sameer

August 11th, 2009 at 9:45 pm

Selenium doesn’t seem to handle dialog boxes correctly. For more information check this article:

http://qtp-help.blogspot.com/2009/07/selenium-handle-dialogs.html

26

Kumar

August 11th, 2009 at 11:52 pm

Thanks Sameer.

27

Rajiv

August 27th, 2009 at 4:47 am

28

Nec Soriano

August 30th, 2009 at 11:10 pm

Hi there, do you have a tutorial for Selenium RC?

29

Rajiv

August 31st, 2009 at 6:01 am

I am trying to Run Selenium RC on Windows Win 7 but I am getting error. Steps followed by me are written below:

1. Installed Selenium IDE.

2. Record a test case and Exported as “PHP selanium RC”

3. Open it in notepad and get following code:

a. Code [Filename is "Example.php"]

setBrowser(“*chrome”);
$this->setBrowserUrl(“http://delicious.com/”);
}

function testMyTestCase()
{
$this->open(“/”);
$this->type(“homepage-searchinput”, “selenium”);
$this->click(“homepage-searchsubmit”);
$this->waitForPageToLoad(“30000″);
$this->getText(“xpath=/html/body[@id='index']/div
[@id='doc3']/div[@id='bd']/div
[@id='yui-main']/div[@id='content']/h3/div/em”);
}
}
?>

b. Replaced “$this->setBrowser(“*chrome”);” with

“$this->setBrowser(“*iexplore C:\Program Files (x86)\Internet Explorer\iexplore.exe”);”

c. Save and close it.

4. Installed Java

5. Downloaded Selenium RC and Run Selenium RC server by using “java -jar selenium-server.jar” command on command prompt and minimized it.

6. Installed PHP.

7. Copied and pasted the “Example.php” file in “C:\wamp\bin\php\php5.2.8″ folder

8. Open another cmd window and wrote cd C:\wamp\bin\php\php5.2.8

9. Run the command phpunit Example

10. I have noticed that Internet explorer was opened and nothing was happened after that.

10. Error recieved by me on my command prompt:

C:\wamp\bin\php\php5.2.8>phpunit Example
PHPUnit 3.3.17 by Sebastian Bergmann.

E

Time: 02:00

There was 1 error:

1) testMyTestCase
RuntimeException: Could not connect to the Selenium RC server.

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

sameer

August 31st, 2009 at 10:23 pm

31

Rajiv Walia

September 2nd, 2009 at 11:36 pm

Hi Sameer,

Its working fine now

1. I just replaced following code as suggested by you:
xpath=/html/body[@id='index']/div[@id='doc3']/div[@id='bd']/div[@id='yui-main']/div[@id='content']/h3/div/em

2. Replaced chrome by “iexplore”

Its working great :)

But still not working with chrome and firefox.

32

Radhika

September 23rd, 2009 at 11:26 am

Hi Sameer, Good work on the tutorials .. thank you — definitely getsa newbie started…

Was curious to know how i could test forms, with a huge data set ?

any pointers…

thanks…

sameer

September 23rd, 2009 at 9:07 pm

Well you could create the basic test structure with the Selenium Ide and then use the RC server and PHP to unserialize test data sets from a file and again serialize results back.

34

Swapnil

October 6th, 2009 at 1:00 am

I am new to use selenium. I want to do data driven test for a form with some data set in IDE. Canyou please help for the same?Can we do it in selenium IDE?

sameer

October 6th, 2009 at 5:23 am

The best way to design data driven tests is to use Selenium IDE along with Selenium RC server and PHP. Design your tests with the IDE, export the generated PHP code, and use it with PHP and the RC Server

36

sowjanya

October 14th, 2009 at 2:52 am

I tried opening selenium IDE in Firefox and clicked on record button,gave base url as http://www.google.co.in/ click on enter is not logging any thing…

Please help me in startup…

37

amol

October 28th, 2009 at 6:52 am

Hi sameer,

I am trying to execute selenium RC in ubuntu , so i have fetch a problem for phpunit Example case not executed. I have doing whole procedure as you told above.

Please told me ,ASAP.

sameer

October 28th, 2009 at 7:04 am

You are not being specific about the problem you are facing.

39

amol

October 28th, 2009 at 7:05 am

swami@swami-laptop:/etc/php5$ phpunit Example
PHP Warning: Module ‘mysql’ already loaded in Unknown on line 0
PHPUnit 3.4.2 by Sebastian Bergmann.

E

Time: 0 seconds

There was 1 error:

1) Example::testMyTestCase
PHPUnit_Framework_Exception: Could not connect to the Selenium RC server.

FAILURES!
Tests: 1, Assertions: 0, Errors: 1.

sameer

October 28th, 2009 at 7:10 am

Make sure that the Selenium RC server is started correctly on port 4444.

41

AJY

December 14th, 2009 at 2:43 am

Sameer,

Thanks for the tutorial you have provided.

I am using the selenium + php + bromine. I just started to do this. and while im doing the record from IDE and export it as php- selenium, mainly the code contains,

function testMyTestCase()
{
//the steps that recorded in IDE

}

So what is the method to break the single function to different function? i mean is it possible to call different functions inside this main function?

Also in case of login check of a website say example, We need to check all the incorrect user-name and password also. So if we need to check 5 combination of username and password, then need to repeat all the steps again/?

Actually i have stucked in initial stage itself…

42

Rohith

December 14th, 2009 at 4:02 am

Thanks sameer for the post..

Can you give me an example to handle the datadriven using php?? Like data can use from csv…???

sameer

December 14th, 2009 at 4:48 am

You can add support functions to your test case, for example:

public function myFunction()
{
// return someThing
}

function testMyTestCase()
{
$data = $this->myFunction();
// the steps that recorded in IDE
}

sameer

December 14th, 2009 at 4:52 am

Hello Rohith!, I’m planning on writing a post on PHPUnit data set testing in a few days.

45

AJY

December 14th, 2009 at 6:49 pm

Thank you for the quick reply…

I really want a start up examples to write the coding on selenium. Can you provide me some examples?? then it will be great.

AI really don’t know the steps to follow in standard way if i am writing the code.

or is there any tutorials to let me do the example coding on selenium

sameer

December 14th, 2009 at 8:44 pm

PHPUnit documentation has some nice examples on testing, you could try that. http://www.phpunit.de/manual/3.4/en/automating-tests.html

47

AJY

December 16th, 2009 at 10:19 pm

Sameer,

Thanks for the help sameer. I just started. Initially started to do on IDE and then later it will become a routine and will learn coding.

Now i stucked like if i ever used the $this->waitForPageToLoad(“30000″);, its showing time out error while running the test case.

Evertime its showing in red color, timeout….I tried to just increased tye number. But i think its not related to that…Di u have any idea regarding that????

AJY

sameer

December 16th, 2009 at 10:27 pm

Try reading this post, see if it can be of any help:

http://www.codediesel.com/testing/running-selenium-testrunner-slowly/

49

Venkata

December 19th, 2009 at 7:54 pm

Hello there, I am using Selenium and want to find out how to use it to open and attach a file command. Is there one such command?

current my code is using open command for edit-field-student-file-decree-0-filefield-upload. To upload a file. But it does not open a file. Any suggestions.

thanks
Venkat.

sameer

December 20th, 2009 at 9:33 pm

I’ve used the following sample Selenium IDE html to upload a file.


New Test
open /selenium/index.php
type uploadFile C:\Program Files\ Mozilla Firefox\application.ini
clickAndWait mysubmit

‘uploadFile’ is the name of the file input box.

You can duplicate the command and see if it works on your browser.
I’m using Firefox 3.5.5.

51

Suman

January 5th, 2010 at 12:52 am

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
Suman

sameer

January 5th, 2010 at 4:20 am

Hi Suman! I’m not a Java person, so I really cannot be of any help there. Sorry!

53

Ruvini

January 7th, 2010 at 5:02 am

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

sameer

January 7th, 2010 at 5:35 am

I’m writing a small post on extending the IDE for the purpose you mentioned in a couple of days, keep watching.

55

Ruvini

January 10th, 2010 at 10:52 pm

Thanks a lot Sameer…

Waiting for your next post

56

yaver

February 2nd, 2010 at 6:00 am

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

57

avanthi

March 10th, 2010 at 11:29 am

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.

sameer

March 12th, 2010 at 9:22 am

My apologies! I’m not conversant with SharePoint.

59

avanthi

March 12th, 2010 at 9:54 pm

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.

60

avanthi

March 12th, 2010 at 9:57 pm

I played it back by using selenium RC

61

petr

March 17th, 2010 at 9:17 am

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 disabled for this post, but if you have spotted an error, feel free to contact me.

Sign up for fresh content in your email