Working with JAVA Eclipse

advertisement
Prerequisites:
1) JDK (Java Development Kit): Download the JDK from
http://www.filewatcher.com/m/jdk-1_5_0_11-windows-i586-p.exe.53394464.0.0.html
link.
After finishing the download, install the executable file into your system.
Set Java home as Environmental variable into your system, see screen:
User can see the above screen if he follows the following steps:
a) Right click on the My Computer icon from your desktop
b) Click Properties
c) Click Advanced tab
d) Click Environment Variables button
e) User would see Environment Variables interface
f) Click New associated with System variables section
g) You would see New System Variable interface
h) Put JAVA_HOME into the variable name field
i)
j)
Path of your JDK into the variable value field, as into the above screen my JAVA
Home directory is installed into C:\Program Files\Java\jdk1.5.0_11
Click Ok
User can check if java is successfully installed on the system or not, follow two screens
which are below:
Run java –version command into the command prompt like:
After execution of above command you would see the following screen:
2) Selenium RC: Download selenium RC from here http://seleniumhq.org/download/ link.
Put unzipped folder into your hard drive.
Click here to
download
binary file of
selenium rc
Launch Eclipse exe, if you launch first time on your box then
user would see the following screen, enter your workspace
location and click Ok

Eclipse screen is following:

Select File > New > Java Project.

Provide Name to your project, Select JDK in ‘Use a project
Specific JRE’ option (JRE6 selected in this example) >
click Next then Finish.

Keep ‘JAVA Settings’ intact in next window. Project
specific libraries can be added here, this would create
project Demo in Package Explorer/Navigator pane

Right click on src folder and click on New > Folder, name
this folder com and finish

This should get com package inside src folder.

Following the same steps create core folder inside com.

Following the same steps create tests folder inside core

Create a folder called lib inside project Demo. Right click
on Project name > New > Folder. This is a place holder for
jar files to project (i.e. Selenium client driver, selenium
server etc).

This would create lib folder in project directory.

Right click on lib folder > Build Path >Configure build
Path

Under Library tab click on Add External Jars to navigate to
directory where jar files are saved. Select the jar files
which are to be added and click on Open button.

Click on Add External jars button and add “selenium-javaclient-driver.jar”, “junit.jar” and all the jar files of
‘selenium-java-2.0a6’ folder from your system. User would
see following screen, now click Ok button:

Right click on com.core.tests folder, click New>>Class

Give a class name like into the following screen and click
Finish button:

Paste the following codes
package com.core.tests;
import
import
import
import
org.openqa.selenium.By;
org.openqa.selenium.WebDriver;
org.openqa.selenium.WebElement;
org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class Example {
public static void main(String[] args) {
// Create a new instance of the html unit driver
// Notice that the remainder of the code relies on the
interface,
// not the implementation.
WebDriver driver = new HtmlUnitDriver();
// And now use this to visit Google
driver.get("http://www.google.com");
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Cheese!");
// Now submit the form. WebDriver will find the form for
us from the element
element.submit();
// Check the title of the page
System.out.println("Page title is: " +
driver.getTitle());
}
}

Open a Command Prompt and go to the following location.
C:\selenium-rc\selenium-server-1.0.3
Launch selenium rc server as in following screen using
java –jar selenium-server.jar

Right click on the test, and Run >> Run Configuration

Click on RUN button to run the test

You would see the following screen
Download