Create a Java Selenium RC test script and executing the script 

Hello All,

In this article I will explain you step-by-step process of how to run Selenium RC application in Java. Before we start writing the script you need to make you have the following installed on you computer.

1. Java JDK – you can download the latest from http://java.sun.com/javase/downloads/index.jsp

2. Selenium RC – you can download this from https://sourceforge.net/projects/selenium-rc/

3. Selenium IDE – it is a firefox plugin and you can download this from http://seleniumhq.org/download/

4. Junit – you can download this from https://sourceforge.net/projects/junit/files/junit/ – download junit4.7.zip

Install JDK in the following location.

C:\Program Files\Java\

Selenium RC unzip the file and save the files is the following location: C:\selenium-remote-control-1.0-SNAPSHOT

In the folder C:\selenium-remote-control-1.0-SNAPSHOT you will have the following folders:

1. selenium-dotnet-client-driver-1.0-SNAPSHOT

2. selenium-java-client-driver-1.0-SNAPSHOT

3. selenium-perl-client-driver-1.0-SNAPSHOT

4. selenium-php-client-driver-1.0-SNAPSHOT

5. selenium-python-client-driver-1.0-SNAPSHOT

6. selenium-ruby-client-driver-1.0-SNAPSHOT

7. selenium-server-1.0-SNAPSHOT

We will need the selenium-java-client-driver-1.0-SNAPSHOT and selenium-server-1.0-SNAPSHOT for running Selenium RC application in Java

Unzip Junit and save the files in the following location: C:\junit

In the folder c:\junit will have the folder junit4.7

Create a folder c:\selenium\tests to save all the scripts

Now that you have all the required software open selenium ide on your firefox browser and record a simple google search.

Your Selenium IDE screen should look this http://clearspace.openqa.org/servlet/JiveServlet/previewBody/1431-102-1-1174/GoogleSearch.jpg

<tr>
<td>open</td>
<td>/firefox?client=firefox-a&rls=org.mozilla:en-US:official</td>
<td></td>
</tr>
<tr>
<td>type</td>
<td>sf</td>
<td>Selenium IDE</td>
</tr>
<tr>
<td>clickAndWait</td>
<td>btnG</td>
<td></td>
</tr>
<tr>
<td>verifyTextPresent</td>
<td>Selenium IDE</td>
<td></td>
</tr>

Now export test case save as Java(Junit) – Selenium RC
File-> Export Test Case as..-> Java (Junit) – Selenium RC
Check the link File-> Export Test Case as..-> Java (Junit) – Selenium RC
Save the file as GoogleSearch.java in the folder c:\selenium\tests
Go to c:\selenium\tests folder
You will see GoogleSearch.java
Your GoogleSearch.java should look some thing

package com.example.tests;
import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
public class simplesearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com.au/", "*chrome");
}
public void testSimplesearch() throws Exception {
selenium.open("/firefox?client=firefox-a&rls=org.mozilla:en-US:official");
selenium.type("sf", "Selenium IDE");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("Selenium IDE"));
}
}

Now go to command prompt

Go to the folder c:\selenium\tests

Type the following command and press enter

set
CLASSPATH=%CLASSPATH%;C:\junit\junit4.7\junit4.7\junit-4.7.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar;C:\Program
Files\Java\jdk1.5.0_16\bin\javac.jar;C:\Program
Files\Java\jdk1.5.0_16\lib\tools.jar;

You are have sucessfully set the classpath
Now type the following command and press enter
path=C:\Program Files\Java\jdk1.5.0_16\bin\

You will have to now change the GoogleSearch.java file for it to get complied

//package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;
import junit.framework.*; //added
public class GoogleSearch extends SeleneseTestCase {
public void setUp() throws Exception {
setUp("http://www.google.com.au/", "*chrome");

}
public void testSimplesearch() throws Exception {
selenium.setTimeout("1000000");
// this is used when you get the error message
thoughtworks.selenium.SeleniumException: Timed out after 30000ms

selenium.open("/");
selenium.type("sf", "Selenium IDE");
selenium.click("btnG");
selenium.waitForPageToLoad("30000");
verifyTrue(selenium.isTextPresent("Selenium IDE"));
}
public static Test suite() {
//method added
return new TestSuite(GoogleSearch.class);
}
public void tearDown(){
//Added . Will be called when the test will complete
selenium.stop();
}
public static void main(String args[]) {
// Added. Execution will started from here.
junit.textui.TestRunner.run(suite());
}
}

After making changes to the GoogleSearch.java save the file.
Now lets compile the java file
In your cmd prompt execute the following command
javac GoogleSreach.java
c:\selenium\tests\javac GoogleSreach.java
Your class file will be saved in c:\selenium\tests\
Run the TestCase
Now before you run the test cases you need to start the Selenium server. To start the server go to the command prompt and run the following command
java -jar C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar -interactive
In the other command prompt (c:\selenium\tests\) and type the following command
java GoogleSearch

If the test passes you will get the following result.
Time: 102.172
OK (1 test)

It lot of cases you will have more than one test script to run; here is an example of how to run more than one script using a batch file and saving the Test Results

To create a batch file open notepad and type the following text:

cd\
set
CLASSPATH=%CLASSPATH%;C:\junit\junit4.7\junit4.7\junit-4.7.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar;C:\Program
Files\Java\jdk1.5.0_16\bin;C:\Program
Files\Java\jdk1.5.0_16\lib\tools.jar;
path=C:\Program Files\Java\jdk1.5.0_16\bin\
cd C:\selenium\Tests
java GoogleSearch >c:\TestResults.txt
java GoogleSrearch1 >>c:\TestResults.txt

In this there are two scripts that I am running GoogleSearch and GoogleSearch1. I am saving the results in  TestResults.txt. >> will append the TestResults.txt file and you will not lose the old test results. Once executed the TestResults.txt will have all the test results.

The most common errors that you will encounter are:

java:3: package com.thoughtworks.selenium does not exist

import com.thoughtworks.selenium.*;

package junit.framework does not exist

import junit.framework.*; //added

GoogleSearch.java:14: cannot find symbol

symbol  : variable selenium

location: class GoogleSearch
selenium.type(“q”, “selenium IDE”);

The solution for the errors are


set
CLASSPATH=%CLASSPATH%;C:\junit\junit4.7\junit4.7\junit-4.7.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-java-client-driver-1.0-SNAPSHOT\selenium-java-client-driver.jar;C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar;C:\Program
Files\Java\jdk1.5.0_16\bin;C:\Program
Files\Java\jdk1.5.0_16\lib\tools.jar;

C:\selenium\Tests>java googlesearch
Exception in thread “main” java.lang.NoClassDefFoundError: googlesearch
The issue can be resolved by typing java GoogleSearch
Always make sure you have set the JDK path correctly path=C:\Program Files\Java\jdk1.5.0_16\bin\

Cheers

Pavandeep Puddupakkam


Author: Pavandeep Puddupakkam on September 12, 2009
Category: Selenium RC, Software Testing
Tags: , ,
Google Webdriver Forum -The Online Community for Google Webdriver and Selenium RC Users and Professionals
19 responses to “Create a Java Selenium RC test script and executing the script”
  1. srinivas says:

    Hi Pavan,
    I followed your steps to execute my java script from command prompt.I am getting “Exception in thread ‘main’ java.lang.noclassDefFounderError:google”

    could pls help in this.

    Thanks & regards,
    Srinivas

  2. Harine says:

    I tried with Eclipse, It works fine there. But I need to run using Command prompt only. I too encounter that exception in main. Can you be more clear about that Snapshot in every jar folder you have used, jUnit needs no main right? the y does it throw an exception?

    • Pavandeep Puddupakkam says:

      This issue is again with the name of the .java file. The file name is case sensitive. So, if you have complied the file with the name GoogleSearch.java you can only run the script with the command java GoogleSearch; if you try and run the script as java googlesearch you will get the error “main” java.lang.NoClassDefFoundError:
      Also name sure of the path of your JDK path.

  3. Harine says:

    I was sure of my filename but I’m yet to check out with the jdk path. While compiling only that main exception is thrown.

  4. Smusica says:

    Hi,

    I have created test case through selenium as

    package com.example.tests;

    import com.thoughtworks.selenium.*;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import java.util.regex.Pattern;

    public class GoogleSearch extends SeleneseTestCase {
    @Before
    public void setUp() throws Exception {
    selenium = new DefaultSelenium(“localhost”, 4444, “*chrome”, “http://www.google.co.in/”);
    selenium.start();
    }

    @Test
    public void testGoogleSearch() throws Exception {
    selenium.open(“/”);
    selenium.type(“q”, “selenium ide”);
    }

    @After
    public void tearDown() throws Exception {
    selenium.stop();
    }
    }

    and run through command prompt displayed following error–>

    C:\selenium\tests>java GoogleSearch
    Exception in thread “main” java.lang.NoClassDefFoundError: Google
    Caused by: java.lang.ClassNotFoundException: GoogleSearch
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Met
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Sou
    at java.lang.ClassLoader.loadClass(Unknown Source)
    Could not find the main class: GoogleSearch. Program will exit.

    Also Could u please provide steps to compile java code in Eclipse. Just need to confirm whether the extracted file code (from selenium and saved as java file ) need to copy past in Eclipse to compile or what?…Please clear on the same.

    Thanks in advance!!

  5. Gurunath says:

    These is a excellent blog on selenium very best of all Thanks a lot man

  6. Mak says:

    Hi all ,,
    I did all of the things mentioned here ,, and setup classpath, compiled script etc. but now when I am running the script after running the server. it just Hangs… but server and java program just hang.. any idea..

    And thanks for such a informative post

    • Pavandeep Puddupakkam says:

      Hi Mak,

      Have you started the selenium server
      java -jar C:\selenium-remote-control-1.0-SNAPSHOT\selenium-server-1.0-SNAPSHOT\selenium-server.jar -interactive
      May have to change the path of the selenium server jar file.

      Also change the selenium = new DefaultSelenium(“localhost”, 4444, “*chrome”, “http://www.google.com/”); to selenium = new DefaultSelenium(“localhost”, 4444, “*iexplore”, “http://www.google.com/”);

      Some times when you are using Firefox to run your Selenium Scripts you will have issue when you have an instant of Firefox already running.

      Please also have a look at the post http://www.seleniumwiki.com/automation-tips/firefox-profile-manager/ to understand how to create a Firefox profile.

      I would suggest you to run the script using Junit; here is the post to test if your script is really working.

      To run the script in Junit please see the following article: http://www.seleniumwiki.com/software-testing/selenium-rc-with-java-and-junit/

  7. Mak says:

    Hey thanks for your reply, I got it after posting my question here. that was because I was not using the latest Snapshot. I got the clue from Selenium FAX its it says .. if you are using version X{ version number is given there} you can not use firefox 2 and above ..

    I installed the new version and did the samething it ran.. but still thanks for responding to my message
    bbye

  8. Parag says:

    Hi Pavandeep ,

    I have created a test suite of Selenium JUnit test cases and I can run this suite from Eclipse.
    How can I run this test suite via the command line ?

    Thanks,
    Parag.

  9. Shweta says:

    Hi Pavandeep ,

    Thanks for giving us this knowledge, I want to ask you as I am a black box tester. I have very less knowledge of programming language so to understand errors and script and all do I need to learn Java first and can you give this steps in C#? can you please answer.
    Thanks
    shweta

  10. Mahesh says:

    Hi,

    I have create a java program “Google.java” and save in “D:\SeleniumAssignments\SeleniumProject\bin\seleniumTestPackage” folder. My JDK 1.6.0 is installed in C:\Program Files\Java\Jdk1.6.0. And selenium client and server .jar files are store into the “D:\SeleniumRC\selenium-java-client-driver-1.0.1\selenium-java-client-driver-1.0.1.jar” and “D:\SeleniumRC\selenium-server-1.0.3\selenium-server-1.0.3.jar”. so can u help me to set class path?.

  11. Needa says:

    Hi Mak,
    Will u please tell me what is the latest version of snapshot? I also did the same thing but evrything is hanged.I am using firefox 9.1.pleas reply
    Thanks & Regards.

Leave a Reply

Last articles