Friday, September 4, 2009

using selenium RC in your java project - How to run your script in different browser

I have been using selenium IDE fro last 1 year or so. In fact I just use this to automate few of our module. I was not so active user of selenium in this period.
Few months back my company placed me in a java project. I wanted to work with some automation tool not exactly play back and recording. There for I tried my hand in selenium RC. Recently I successfully managed to install RC with eclipse (Java IDE used in my project) and ran some of test case recorded in IDE. I am trying my hand to make them data driven and more specific. For that one should be good in java programming. I am also trying to be good in that.

I have seen many of us seeking help to configure RC in their project. All they want to run very first test case. for that, I decided to write some thing which may help to begin with the very first etst case in RC.
Here in my article I will explain how to configure RC with eclipse and how to run the test case recorded in IDE.
I am assuming that you are good in using IDE record and playback.
So once you done your recording with IDE convert your recorded script in java. for this click on OPTIONS->FORMAT and choose java. save this test case as simple java file.

so you have your test case say test.java.

Now follow the steps to configure Rc and java client driver with eclipse.

1- Download Selenium-RC from the SeleniumHQ downloads page and save this some where you want.

2- launch eclipse
3- Create a new java project (I guess you are aware of creating a new project in eclipse. If not then take help from developer or visit documentation section in http://seleniumhq.org/)
4- Add external jar files. (from the location where you have already saved downloaded selenium rc)
5- Add your saved java file(which you have saved after recording in IDE) to this project
6- Now run this as Junit test


here I am giving some explanation which might help you to get the things easily.


This is the java code which i have recorded using IDE to test the search in Google.

import junit.framework.*;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

import com.thoughtworks.selenium.SeleneseTestCase;
public class test1 extends SeleneseTestCase {

public void setUp() throws Exception {
setUp("http://www.google.co.in/", "*firefox");
}
public void testVishal() throws Exception {
selenium.open("/");
selenium.type("q", "vishal sachan");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("vishal sachan"));
}

}

Running your test in multiple browser:
You can now run this test in different browse by initializing the desired browser info like firefox, iexplorer, chrome..
setUp("http://www.google.co.in/", "*firefox") ;

I hope this will help you to get started with you very first test using selenium RC.