Selenium Script for Java using Eclipse.

Hello Friends,

This is one of my tutorials regarding the use of Selenium IDE for testing and automation purpose. Most of the users who are in Testing field, they may know about this. But for those who are newbies, this will be a helpful tip. Selenium IDE is a Firefox add-on that allows the user to copy or record the actions one perform on the browser, it also allows the user to Playback the same multiple times. This is why it is used as the testing tools. It allows the user to generate a language specific code to run the tests with Selenium Remote Control. It itself is a vast topic, so we will be discussing it on short terms. For now, we will first learn how to use this tool for testing and automation.

NOTE: User reading this article is considered to be having basic knowledge of Selenium and is having the IDE (Add On) installed on Firefox browser.

To run any Selenium script, you will need the Selenium Server to be installed on your machine. This installation is nothing but the jar file which can be downloaded here. Once you finished this download, you can extract the files to any Directory and there you can see the selenium-server.jar file. This is the file we need to start the Selenium Server.

Selenium can be used with different platforms like Java, dotNet, PhP, Perl, Ruby etc. For now, we will be discussing use of Selenium IDE with Java. To use java-client with selenium we will need the java-client-drivers. You can download this here selenium-java-client-driver (Selenium Client Drivers). This is the jar file which should be present in the build path of your eclipse project. You need the JUnit packages to run the Java Selenium Scripts from eclipse. Download link for JUnit packages is http://www.junit.org/ , download and extract the files to disk.

Now, start the Eclipse IDE, and create a TestProject there. Then import the Selenium and JUnit packages into the Eclipse project. Follow these steps for setting the build paths.

  • In Package Explorer right click project “TestProject” and select Properties.
  • Go to “Java Build Path” then select “Libraries” tab.
  • Click “Add External JARs” and import junit-4.7.jar and selenium-java-client-driver.jar  (which you downloaded earlier)

Once you are finished with setting up build path, our eclipse is ready to use.

Now, go to the browser and start the Selenium IDE. Go To Tools –> Selenium IDE.

Once you click the link, a new window will be opened, this is the Selenium IDE window. There you can see the red ball on the top right corner, which indicates that the Selenium is now running and ready to record.

Now, we need to record some actions on the browser. I am opening the link https://code2java.com and then will click on the About Me link and then will come back on the Home link. The time you performed these actions on the browser, the IDE has recorded your actions and same can be seen there.

You can see the recorded actions on the IDE. However this wont help us write script, but the step we which we are doing next will really do. Now, we will ask the IDE to convert this script into the java code. Its really easy to do this. Just click on File –> Export test case as –> JUnit 4 (Remote Control) and then save the file with .java extension (code2java.java).

Now, open code2java.java file in editor, you will see the selenium script in the java format. It will be something like this-

Add this java file in the eclipse project under the required package.

Eclipse Java Project

We have already set up the build path in the earlier steps. Now, our program is ready to run. Before running the program using JUnit, you need to start the Selenium Server. For this, open the command prompt, go to the folder where selenium-server.jar is located, and then type the following command and run: —

Once the command is executed the Selenium server will be started and the cmd prompt will look like:

Now, the server is stared and you can run your program from Eclipse. Just right click on the java file and click on Run –> Junit4. And the program will be executed.

Once the execution started, the Selenium command window will open. It will show the command history i.e. which command has been executed. Along with this, the browser window will open and will run your script.

Once your script completes. both the Command Window and the Browser will be closed. You can see the changes in the command prompt window, after the execution of the script.

This is how, you can use the Selenium IDE for automation and testing purpose. Please leave your comments and queries on this.

Thanks,

Nikhil Naoghare

One thought on “Selenium Script for Java using Eclipse.”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.