You are on page 1of 9

A Total Beginners Guide to Writing a Selenium-Java Program using Eclipse By Milad Afdasta

This document is written with the assumption that you have already read A Total Beginners guide to setting up Selenium By Milad Afdasta (http://clearspace.openqa.org/docs/DOC-1 !"# The $irst step is to get %clipse. 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.wi&ipedia.org/wi&i/%clipse'(so$tware# (n other words %clipse is a so$tware that allows you to write and e)ecute programs in many di$$erent languages. (n our case we will *e using %clipse to write and e)ecute +elenium via ,ava. -est o$ all %clipse is a $ree so$tware. (/Than& you Open +ource#

0et us *egin1 2etting %clipse: 2o to http://www.eclipse.org/downloads/ o 3ou will want to get Eclipse Classic 3.4 o Clic& on 45indows6 lin& to the right.

o 7ow to download it8 Clic on the lin& that appears to the right o$ 4!o"nload from#6

o +ave the $ile. 9$ter it has $inished downloading8 you need to e)tract the $iles to your C: driver directory. (n windows you would ta&e the $ollowing steps: 1. ". =. >. ?. !. @. A. B. :ight Clic& on the ;ip $ile you <ust downloaded Clic& on E$tract All%%% Clic& &e$t Clic& on Bro"ser' Clic& on My Computer Clic& on (ocal !is )C#* Clic& on +, Clic& on &e$t Clic& on -inis.

9t this point %clipse is installed and ready to *e used


7ote: Ceep in mind %clipse is an 4%)ecuta*le6 programD there is no installation need other than e)tracting the $iles on to your hard drive.

7ow let us start up %clipse1 2o to your C# driver and open up your Eclipse $older. Double !lick on Eclipse to run the program. 9 +ettings 5indow will open up letting you &now that %clipse saves your pro<ects into a $older called 4workspace.6

($ you wish to change the location o$ this $older $rom itEs de$ault location8 !lick on Bro"ser' and choose your desired location. ($ you do not wish to *e in$ormed a*out this again8 !"eck the Bo$ ne)t to 4Fse this as the de$ault and do not as& me again.6 !lick on +, to continue. Once %clipse has loaded you will have the option to get an overview o$ the application8 $ind out what is &new8 review come sample codes8 go through some tutorials8 or go to your wor&*ench. (n our case8 we want to go to the wor&*ench8 so clic& on the icon with the title 45or&*ench.6

This will *ring you to the environment that you will *e wor&ing in:

Our $irst step will *e to create a Pro/ectG. 1. 2o to -ile ". Clic& on &e" =. Clic& on Pro/ect' 9t this point a new window has opened8 here you will tell %clipse what type o$ Hro<ect you want to create. >. Clic& on Java ?. Clic& on Java Pro/ect

!. Clic& &e$t @. On the top ne)t to Pro/ect name# give your pro<ect the name 4Iirst6 A. Clic& -inis. 7ow you should *e *ac& at your wor& *ench with your pro<ectEs $older visi*le to the le$t.

G 9 Pro/ect is *asically what will save property setting o$ your environment and create a $older that will contain all the $iles you will *e creating/using to accomplish your programming tas&s.

The ne)t step in the process is to import two +elenium 0i*raries8 without them8 you would not *e a*le to run +elenium. 1. :ight Clic& on the 4Iirst6 $older ". Clic& on Properties

(Hlease note ( crop the screenshots sometimes to a more appropriate si;e#

=. Clic& on Java Build Pat. >. Clic& on the (i0raries ta* ?. Clic& on Add E$ternal JA1s'

!. Iind and select selenium-server.<ar then Clic& +pen


(i.e. C:Jselenium-remote-control- .B."Jselenium-server- .B."Jselenium-server.<ar#

@. Clic& on Add E$ternal JA1s' again A. Iind and select selenium-<ava-client-driver.<ar then Clic& +pen
(i.e. C:Jselenium-remote-control- .B."Jselenium-<ava-client-driver- .B."J selenium-<avaclient-driver.<ar#

B. 3ou should now see the two new ,9:s added to your 0i*raries list

1 . Clic& +, 3ou have $inished setting up your pro<ect to run selenium8 now we create a class and write a simple +elenium program. 1. :ight Clic& on Iirst Iolder ". Clic& on &e" =. Clic& on Class

9t this point a new 5indow will open up >. (n the 7ame $ield give it the name Ky+elenium ?. Clic& + 9 new su* $older was created called 4src6 (short $or source#8 and in this su*$older your class $ile was created called 4Ky+elenium.<ava6 Iurther more your class $ile is opened up in eclipse and ready to *e populated *y code.

0ets try to run a simple +elenium program that opens o$ a *rowser and goes to the 2oogle we*site. Copy and past this code into your class $ile:
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" !!!! ""ie#$lore" "htt$%&&www.google.com"); browser.start(); browser.o$en("htt$%&&www.google.com"); ' '

+ave the $ile8 and now *e$ore we run the program we need to start the +elenium +erver. 1. Clic& on Start (5indows +tart menu# ". Clic& 1un =. Type cmd This will open up the command window

>. Type in <ava L<ar Mlocation o$ your selenium-server.<ar $ileN -multi5indow


(i.e. <ava -<ar C:Jselenium-remote-control- .B."Jselenium-server- .B."Jselenium-server.<ar -multi5indow# The +elenium +erver should *e running now:

7ow that +elenium +erver is running in the *ac&ground lets go *ac& to %clipse and run our program. 1. Clic& on 1un ". Clic& on 1un As =. Clic& on 2 Java Application

9nd you are DO7%. Congratulations you have written your $irst ,ava-+elenium program using %clipse.

You might also like