Thursday, April 2, 2009

Step by step instructions - Run your First Java program using Eclipse

This is to continue with the assumption that you have already read -
A Total Beginners guide to setting up Selenium (Make sure you have all the installation about Selenium, Java - Please refer to other links on this blog).

The first step is to get Eclipse.

Eclipse is a software platform comprising extensible application frameworks, tools and a runtime library for software development and management. It is written primarily in Java to provide software developers and administrators an integrated development environment (IDE).
–source: http://en.wikipedia.org/wiki/Eclipse_(software)

In other words Eclipse is a software that allows you to write and execute programs in many different languages. In our case we will be using Eclipse to write and execute Selenium via Java.

Best of all Eclipse is a free software! (~Thank you Open Source)

Let us begin…

Getting Eclipse:
Go to http://www.eclipse.org/downloads/
1. You will want to get Eclipse Classic 3.4
2.Click on “Windows” link to the right.
3.Now to download it, Click on the link that appears to the right of “Download from:”
4.Save the file.

After it has finished downloading, you need to extract the files to your C: driver directory.
In windows you would take the following steps:

1. Right Click on the zip file you just downloaded
2. Click on Extract All...
3. Click Next
4. Click on Browser…
5. Click on My Computer
6. Click on Local Disk (C:)
7. Click on OK
8. Click on Next
9. Click on Finish

At this point Eclipse is installed and ready to be used

Note: Keep in mind Eclipse is an “Executable” program; there is no installation need other than extracting the files on to your hard drive.

Now let us start up Eclipse…
1.Go to your C: driver and open up your Eclipse folder.
2.Double Click on Eclipse to run the program.
3.A Settings Window will open up letting you know that Eclipse saves your projects into a folder called “workspace.”

If you wish to change the location of this folder from it’s default location, Click on Browser… and choose your desired location.

If you do not wish to be informed about this again, Check the Box next to “Use this as the default and do not ask me again.”

Click on OK to continue.
Once Eclipse has loaded you will have the option to get an overview of the application, find out what is knew, review come sample codes, go through some tutorials, or go to your workbench. In our case, we want to go to the workbench, so click on the icon with the title “Workbench.”

This will bring you to the environment that you will be working in:

Our first step will be to create a Project*.

1. Go to File
2. Click on New
3. Click on Project…At this point a new window has opened, here you will tell Eclipse what type of Project you want to create.
4. Click on Java
5.Click on Java Project

6. Click Next
7. On the top next to Project name: give your project the name “First”
8. Click FinishNow you should be back at your work bench with your project’s folder visible to the left.

The next step in the process is to import two Selenium Libraries, without them, you would not be able to run Selenium.

1. Right Click on the “First” folder
2. Click on Properties
3. Click on Java Build Path
4. Click on the Libraries tab
5. Click on Add External JARs…
6. Find and select selenium-server.jar then Click Open (i.e. C:\selenium-remote-control-0.9.2\selenium-server-0.9.2\selenium-server.jar)
7. Click on Add External JARs… again
8. Find and select selenium-java-client-driver.jar then Click Open (i.e. C:\selenium-remote-control-0.9.2\selenium-java-client-driver-0.9.2\ selenium-java-client-driver.jar)
9.You should now see the two new JARs added to your Libraries list
10. Click OK

You have finished setting up your project to run selenium, now we create a class and write a simple Selenium program.

1. Right Click on First Folder
2. Click on New
3. Click on Class

At this point a new Window will open up

1. In the Name field give it the name MySelenium
2. Click Ok
A new sub folder was created called “src” (short for source), and in this subfolder your class file was created called “MySelenium.java”

Further more your class file is opened up in eclipse and ready to be populated by code.

Lets try to run a simple Selenium program that opens of a browser and goes to the Google website.Copy and past this code into your class file:

import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.DefaultSelenium;

public class MySelenium {

static Selenium browser;

public static void main(String arg[])
{
browser = new DefaultSelenium("localhost",
4444, "*iexplore", "http://www.google.com");

browser.start();

browser.open("http://www.google.com");

}
}

Save the file, and now before we run the program we need to start the Selenium Server.

1. Click on Start (Windows Start menu)
2. Click RunType cmd
3. This will open up the command window
4. Type in java –jar -multiWindow
(i.e. java -jar C:\selenium-remote-control-0.9.2\selenium-server-0.9.2\selenium-server.jar
-multiWindow)
5. The Selenium Server should be running now:

Now that Selenium Server is running in the background lets go back to Eclipse and run our program.

1. Click on Run
2. Click on Run As
3. Click on 2 Java Application

And you are DONE! Congratulations you have written your first Java-Selenium program using Eclipse.

No comments:

Post a Comment