Tuesday, January 12, 2010

Import selenium into java

This is a step by step tutorial to import a selenium generated java code into eclipse, add the necessary wrappers and make it work. It assumes the following






  • Basic understanding of testing in selenium.


  • A properly installed eclipse ide




The lay out of the tutorial includes three parts, "Set up Instructions" to help set up the ide for developing selenium code, "The Code" to wrap the automated code generated by selenium and "The run" to run the code that we wrote





Set up Instructions







  • Create a download folder on your computer, lets do C:/selenium




  • Download the following zip file to the location created in the previous step





  • Open the eclipse ide ( This tutorial has been writen using Eclipse Galileo )





  • Click on windows->preferences as shown in the screen shot below
























  • This will open up window called preferences. Click on the '+' sign next to 'Java' on the left panel, then click on 'Build Path' and then click on 'User Libraries' as shown in the screen shot below





  • Click New on the right hand side of the window. This will pop up a window asking for the name of the library we are about to create. Give the name of the library as Selenium








Select the newly created user library Selenium, this should enable the 'Add Jars...' Button on the right. Click this button and add select all the jars that have been extracted to C:/selenium. Ensure succesfull addition by expanding the Selenium library. Click ok to get out of the libraries window




The Code




The next step is to create a project in eclipse. The following steps will walk you through it




Click on window->Open Perspective->Java Browsing









  • Click on window->Show view->Navigator. This will add a panel to the left of the ide that provides a simple way to navigate across the project elements ( like classes, packages etc)





  • Click on File->New->Java Project







  • In the "New Java Project" window, type in the project name as "TestSelenium"( Case sensitive). Leave the rest as default and click finish.




  • Right click the newly created project in the navigator tab and click on properties


In the "Properties for TestSelenium" window, select "Java Build Path", Now in the panes that open to the right, click on libraries. By default it will show "JRE System library"




Now click on "Add Library" button which should pop up the "Add Library" screen. In this screen, select User Library and click "Next"



The screen should show all the libraries that have been created by the user. Select the "Selenium" library and click finish



Select File->New->Class which should open up the following window. In the package box type in "com.testselenium" ( without the quotes), In the name section type in "Server" (without the quotes) and click finish. This should create a folder structure that you can see in the "Package Explorer"/ "Navigator" tabs on the left of the ide



In the file that the ide opens for you. Delete all the stubs( text for your convinience ) and paste the following




package com.testselenium;
import org.openqa.selenium.server.*;
public class Server {


private static final Server instance = new Server();
public static Server getInstance() {
return instance;
}
private SeleniumServer server = null;
protected Server() {
}
public void startSeleniumServer() {
if (server == null) {
try {
server = new SeleniumServer(SeleniumServer.DEFAULT_PORT);
System.out.println(" selenium server " + server.toString());
} catch (Exception e) {
System.err.println("Could not create Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
}
try {
server.start();
} catch (Exception e) {
System.err.println("Could not start Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
}
public void stopSeleniumServer() {
if (server != null) {
try {
server.stop();
server = null;
} catch (Exception e) {
System.err.println("Could not stop Selenium Server because of: "
+ e.getMessage());
e.printStackTrace();
}
}
}

public static void main (String[] args){
Server.getInstance().startSeleniumServer();
Server.getInstance().stopSeleniumServer();

}
}