Running your first Selenium RC script

Selenium Remote Control (RC) is a test tool that allows you to write automated web application UI tests in any programming language against any HTTP website using any mainstream JavaScript-enabled browser. Selenium RC is great for testing complex AJAX-based web user interfaces under a Continuous Integration system. It is also an ideal solution for users of Selenium IDE who want to write tests in a more expressive programming language like Java,Perl etc.
Selenium IDE is much simpler and can be used only on firefox. Selenium RC lets you to write much complex scripts using the programming language features. And you can mention the browser on which you want to run the script. Selenium RC supports Internet Explorer, firefox, googlechrome etc.

You can use any IDE for developing your scripts but I like using Eclipse. I will be explaining later how to use Eclipse.

Ok...so lets start creating the Selenium RC script using TestNG. You will need to install the following first.

Install the below
1. JDK : You can be download it from here http://java.sun.com/javase/downloads/index.jsp. Set the classpath.

2. Selenium RC: Download it from here: http://seleniumhq.org/download . Extract the files in a folder named SeleniumRC and rename the selenium-server to JavaServer (it would be easy for us to use later).



3. TestNG : Download TestNG from http://testng.org/doc/download.html. Unzip. The files will get extracted to a folder called testng.

4. Eclipse : Download eclipse from http://www.eclipse.org/downloads/.Choose the "Eclipse IDE for Java Developers".Unzip the downloaded file. The files will get extracted to a folder called 'eclipse'.

5. TestNG plug-in for Eclipse: Start Eclipse by clicking the eclipse icon in the eclipse folder that's mentioned earlier. Click on Help->Install New Softwares. Enter "http://beust.com/eclipse/" in the "Work With:" and press Enter. You should see TestNG. Select it and then press Next till you reach Finish. Restart Eclipse.

Good that we have finished installing everything that we needed to start with selenium and TestNG. You could always work with selenium and testng without eclipse in which case you wont have to install Eclipse and the TestNG plug-in required for eclipse. But its better to have an IDE (like Eclipse or anyother) so that its far easier to write the scripts.

Let's start with the SeleniumRC and TestNG script now:
1. Open eclipse. Start by creating a new project : File->New->Java Project. Give a name say 'Automation'.
2. Add the required jars. Select the project-'Automation' , right click Build Path->Configure Build Path. Click Libraries->Add External Jars. You will have to add the testng and selenium jars.
3. Create a new package in the project. Select the project -'Automation' and then right click New->Package. Give a name say 'First'.
4. Create a new java program. Select the package-'First' and then right click New->Class. Enter the name as say 'GoogleSearchTC'.Press Enter.
5. Now go to Selenium IDE and open the 'google.html' script which we had created in our first blog- 'Starting with Selenium IDE'. Click on Options->Format->Java(TestNG)Selenium. You can see that the format of your test case is now changed.
The format is in the testng format now. Your script would look like below:




6. Copy paste the whole code into eclipse(GoogleSearchTC). You can see some errors.
7. Make changes so that your testng script looks like below:



Great!!!!! Your first testng script is ready to be run. Wasnt that easy?

8. Start your selenium server. Go to command prompt and then go to SeleniumRC/JavaServer folder (where you installed selenium rc). Type in
java -jar selenium-server.jar -port 1234
This is assuming that the port 1234 is free. You can give any other port number if 1234 is being used. Just ensure that the port num given in the testng script matches with the port num used to bring up the selenium server.

Your command prompt screen should look something like below:



Your selenium server is up and running now.

9. Now run the testng script by going back to eclipse. Right click on the testng script. Click RunAs->TestNG Test.
Your script should run successfully.

Some useful selenium tips

When I was trying to automate using Selenium there were couple of times that I got stuck. And google.com was there to help always but at times I would end up searching lot many sites for solutions. So I decided to come up with this post.

Following is a list of tips that would be helpful to you while using Selenium.

*Identifying a dynamic element with Selenium:
Lets say you have an element "input type="text" id="mail__ID197:_ID05_test". The next time you check the id its input type="text" id="mail__ID820:_ID95_test". Now you have a dynamic element. There are multiple ways of identifying this element. You can use "contains" in the locator. You can give the locator as
                    xpath=//input[contains(@id,"mail__") and contains(@id,"_test")]



*Identifying multiple dynamic elements (with similiar xpaths)with Selenium:
Lets say you have a textfield in a page which is dynamic. Every time the number of textfields that appear differ and every time you need to enter some value for every text field that appears. You can use "storeXpathCount" command of IDE (or getXpathCount for Selenium RC)in this case.Lets say the xpaths for the textfields look like input type="text" id="mail__ID97:_ID95_test and input type="text" id="mail__ID26:_ID87_test.
Your commands would look like(Selenium RC and Java):
                     int k = selenium.getXpathCount("xpath=//input[contains(@id,\"mail__\") and contains(@id,\"_test\")]");
                    for (int i=0;i<=k;i++){
                    selenium.type("xpath=//input[contains(@id,\"mail__\") and contains(@id,\"_test\")]","Testing");
                    }


*Identifying multiple dynamic elements (with different xpaths) with Selenium:
Lets take the above case itself but now say you have buttons instead of textfields and you want to perform clicking action for every button. You can use "storeAllButtons" command of IDE in this case.
Your commands would look like(Selenium RC and Java):
String[] g = selenium.getAllButtons();
for (int i=0;i<=g.length();g++){
selenium.click(g[i]);
}
The id's of all the buttons would be stored in the string array. If a button doesnt have an id it will be stored as null. Hence this would work only if you have id's specified for your elements. There are other selenium commands which work the same way- storeAllFields, storeAllLinks.


*Identifying a pop-up window with Selenium
There are multiple ways of identifying a pop-up window. Some are given below:
* storeAllWindowIds, storeAllWindowNames, storeAllWindowTitles: You can use any of these commands to get all the windows. You can then select your pop up window based on its id, name or title. This never really worked for me so I always used the next
solution.

* You forcefully open a window with a name that you specify and then select it. Here I am opening a window with name 'mynewwin' and then selecting it.
selenium.getEval("this.browserbot.getCurrentWindow().open('','mynewwin')");
selenium.click(buttonxpath);
Thread.sleep(5000);
selenium.windowFocus();
selenium.waitForPopUp("mynewwin", "50000");
selenium.selectWindow("mynewwin");
This doesnt work in IE. So you can use the below solution.

* Identifying a pop-up window in IE using selenium:
selenium.openWindow('','mynewwin');
selenium.click(xpath);
selenium.windowFocus();
selenium.waitForPopUp("mynewwin", "50000");
selenium.selectWindow("mynewwin");

Starting with Selenium IDE

Selenium IDE is an integrated development environment for building selenium test cases.It is a firefox add-on that allows recording and playing back of tests.It is a very powerful tool for automating web sites and is very easy to use. Since it is only available with firefox it cant be used to run the tests on any other browser.

To start with Selenium you need to install the following


1. JRE : Selenium server needs the java runtime env version 1.5 or higher in order to start.Try running the command : java -version from your command prompt. If your version is 1.5 or higher you are good to go else you will have to install it or add it to your PATH env.

2. Firefox: Install a firefox version greater than 2.0. You can download it here: http://www.mozilla.com/en-US/firefox/



3. Selenium IDE: Using the firefox browser install ide. You can download it here: http://seleniumhq.org/download/



Open a new firefox window and click on Tools.If you see Selenium IDE there you are all set. You have installed Selenium IDE.



Recording your script


1. Open a site say www.google.com using firefox browser.


2. Open Selenium IDE by clicking on Tools->Selenium IDE.


3. Make sure that the red button (record)is clicked.
4. Type "selenium testing" in the search textfield.Click the "Google Search" button.
5. Click the first link - Selenium web application testing system.
6. Right Click and select assertTitle.
7. Stop Recording by clicking on the record button.
8. Save the script by clicking File->Save Test Case As. Make sure that you give the extension as html say give the name as "googlesearch.html".


Your script should look something like below:



Congrats!! You just recorded your first selenium script.


Playing back your script


Playback the script by pressing the play button (green triangle and one line).
Does your script run successfully?

Your script should have failed as shown below:



The script failed because selenium was trying to look for the element -"link=Selenium web application testing system" even before the page was loaded. This is one of the most common reasons why playing back a selenium script fails - trying to locate a field on a page even before the page is loaded. By adding a command -"waitForPageToLoad" this issue can be resolved.

Try running the script after adding the waitForPageToLoad command as shown below.




As you can see the script run should now be a success with all the command executions shown in green.



Congrats again!! You now played back a selenium script successfully.