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.

17 comments :

  1. Good details for the learner. Really learner can start by taking help of this article...

    ReplyDelete
  2. hi vishal,
    Your article is really helpful

    Thanks
    Srinivas

    ReplyDelete
  3. ERROR SERVER EXCEPTION:Session id not be null or yet not be started

    Yours Naveen

    ReplyDelete
  4. ERROR SERVER EXCEPTION:Session id not be null or yet not be started


    How to get rid of the exception
    if any details send me to my mail
    youandnaveen@gmail.com

    ReplyDelete
  5. Hello Vishal,
    I have one problem.I need to connect database with selenium RC.If you have any idea please let me know on gbchoudhari@gmail.com

    ReplyDelete
  6. Thanks all. Also sorry to them who need some help. I was little busy and out of selenium as working some other tool.

    I will get back to you individually.

    ReplyDelete
  7. Hi Vishal,

    Nice article.

    Can you please tell me "How to Run Selenium RC in different browsers one after the other (After executing the scripts in one browser(Firefox), need to run the same scripts in the other browser(IE)) using Java?"

    Thanks & Regards

    Sunil D

    ReplyDelete
  8. @ Su,

    setUp("http://www.google.co.in/", "*firefox");
    you can change ur script accordingly. like here i have used firefox. U can iether save multiple script with diff browser specification or change it at the time of execution.

    ReplyDelete
  9. This comment has been removed by a blog administrator.

    ReplyDelete
  10. How to run a realtime java web project in selenium RC creating the build?

    ReplyDelete
  11. How to run a realtime java web project in selenium RC creating the build?

    ReplyDelete
  12. CONSULTING, not Cunsulting!!

    Dipak, London.

    ReplyDelete
  13. hi vishal,

    I am getting the following errors-

    com.thoughtworks.selenium.SeleniumException: Timed out after 30000ms
    at com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:100)
    at com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:94)
    at com.thoughtworks.selenium.DefaultSelenium.waitForPageToLoad(DefaultSelenium.java:647)
    at test1.testVishal(test1.java:15)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at junit.framework.TestCase.runTest(TestCase.java:164)
    at junit.framework.TestCase.runBare(TestCase.java:130)
    at com.thoughtworks.selenium.SeleneseTestCase.runBare(SeleneseTestCase.java:228)
    at junit.framework.TestResult$1.protect(TestResult.java:106)
    at junit.framework.TestResult.runProtected(TestResult.java:124)
    at junit.framework.TestResult.run(TestResult.java:109)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.runTest(TestSuite.java:230)
    at junit.framework.TestSuite.run(TestSuite.java:225)
    at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

    Can you help me

    ReplyDelete
  14. really a nice blog.......thanks vishal as it helped me a lot.
    Also want to know something about selenium2.0 that how can we run test cases using maven project.

    ReplyDelete