Wednesday, February 20, 2008

Automate your Test With Selenium RC - JAVA

Download Selenium RC :-
http://release.openqa.org/selenium-remote-control/0.9.2/selenium-remote-control-0.9.2-dist.zip

Download JAVA(JRE - version "1.5.0_07" ) :
http://www.java.com/en/download/manual.jsp

STEPS ::::::::

you may need to add JAVA_HOME/bin to your PATH environment variable.

* Windows:
If you're on Windows XP or Windows 2003, you can just use Internet Explorer for this example, or install Mozilla Firefox or Opera. If you're using Windows 2000, you'll need to install reg.exe(http://wiki.openqa.org/display/SRC/Windows+Registry+Support) in order to use Internet Explorer, but Firefox should work regardless. We recommend (but do not require) that you add your browser executable to your PATH environment variable. (If you do not explicitly add your browser's installation directory to the PATH, then you must install your browser in its standard location; Firefox's standard location is "c:\Program Files\Mozilla Firefox\firefox.exe"; Internet Explorer's standard location is "c:\Program Files\Internet Explorer\iexplore.exe".)
* Unix/Linux: For this tutorial, install Firefox and add the Firefox directory to your PATH environment variable. Note that on Unix/Linux we'll be trying to invoke "firefox-bin" directly, so make sure that executable is on the path; also don't forget to add the Firefox libraries to your LD_LIBRARY_PATH. If needed, we can invoke Firefox using a shell script (e.g. "firefox" or "run-mozilla.sh"), but in that case, we may not be able to stop Firefox until the server is shut down.
* Mac OS X: On Mac OS X, it should be enough to install Firefox.app in your /Applications directory. Note in order to control the browser accurately, we need to invoke the embedded Firefox executable (firefox-bin) directly in /Applications/Firefox.app/Contents/MacOS; if your browser isn't installed there, then you'll want to add the correct embedded location to your PATH environment variable as well as your DYLD_LIBRARY_PATH environment variable.

To setup Selenium on your Machine :
Extract selenium-remote-control-0.9.2-dist.zip

Interactive Mode
Selenium Server "interactive mode" is a way of rapidly prototyping tests that requires no coding whatsoever, so it's a good way to introduce new users to Selenium Remote Control. In interactive mode, you type your commands one by one into the Selenium Server command window; this allows you to immediately see the results of running your command in a working browser, on-the-spot. With that said, normally you'll be coding these tests in your favorite programming language, so the whole thing is completely automated.

Once you've got Java installed and ready to go, you can start the Selenium Server from the command line like this:

java -jar selenium-server.jar -interactive

If you want to use Firefox, try this:

cmd=getNewBrowserSession&1=*firefox&2=http://www.google.com


Example class :

import com.thoughtworks.selenium.*;
import junit.framework.*;
public class GoogleTest extends TestCase {
private Selenium browser;
public void setUp() {
browser = new DefaultSelenium("localhost",
4444, "*firefox", "http://www.google.com");
browser.start();
}

public void testGoogle() {
browser.open("http://www.google.com/webhp?hl=en");
browser.type("q", "hello world");
browser.click("btnG");
browser.waitForPageToLoad("5000");
assertEquals("hello world - Google Search", browser.getTitle());
}

public void tearDown() {
browser.stop();
}
}


You may need to add libraries to run and compile this class .You can find them from selenium-server-0.9.2 and selenium-java-client-driver-0.9.2 folders(in your extract location)

compile java class in unix :

javac -classpath temp.jar:temp2.jar: GoogleTest.java
javac -cp temp.jar:temp2.jar: GoogleTest (You can use tool instead of this eg : Idea)