Thursday, March 15, 2012

Selenium RC with JUnit4

Here is an example explaining how to use JUnit4 with Selenium RC i.e using JUnit4 to run selenium rc tests.

//GoogleTestJunit4.java
  1. package test.example;
  2. import org.junit.AfterClass;
  3. import org.junit.BeforeClass;
  4. import org.junit.Test;
  5. import com.thoughtworks.selenium.DefaultSelenium;
  6. import com.thoughtworks.selenium.Selenium;
  7. /**
  8.  * Search Google example.
  9.  *
  10.  * @author aksahu
  11.  */
  12. public class GoogleTestJUnit4 {
  13.    
  14.     Selenium selenium;
  15.    
  16.     @BeforeClass
  17.     public void startSelenium(){
  18.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  19.         selenium.start();
  20.         selenium.windowMaximize();
  21.     }
  22.    
  23.     @Test
  24.     public void testGoogleSearch(){    
  25.         selenium.open("/");
  26.         selenium.type("id=lst-ib""selenium");
  27.         selenium.click("name=btnK");
  28.         selenium.click("link=Selenium - Web Browser Automation");
  29.         selenium.waitForPageToLoad("30000");
  30.     }
  31.    
  32.     @AfterClass
  33.     public void stopSelenium(){    
  34.         selenium.stop();
  35.     }
  36. }

This test launches firefox browser and executes the google search test.The selenium session is started in startSelenium() method which is called before any test run in the test class and ends with stopSelenium() method which 'll call after the tests run.

Selenium RC with JUnit3

Here is an example explaining how to use JUnit3 with Selenium i.e using JUnit3 to run selenium test.

//GoogleTestJunit3.java
  1. package test.example;
  2. import junit.framework.TestCase;
  3. import com.thoughtworks.selenium.DefaultSelenium;
  4. import com.thoughtworks.selenium.Selenium;
  5. /**
  6.  * Search Google example.
  7.  *
  8.  * @author aksahu
  9.  */
  10. public class GoogleTestJUnit3 extends TestCase{
  11.    
  12.     Selenium selenium;
  13.    
  14.     public void setUp(){
  15.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  16.         selenium.start();
  17.         selenium.windowMaximize();
  18.     }
  19.    
  20.     public void testGoogleSearch(){      
  21.         selenium.open("/");
  22.         selenium.type("id=lst-ib""selenium");
  23.         selenium.click("name=btnK");
  24.         selenium.click("link=Selenium - Web Browser Automation");
  25.         selenium.waitForPageToLoad("30000");
  26.     }
  27.    
  28.     public void tearDown(){    
  29.         selenium.stop();       
  30.     }
  31. }

            This test launches firefox browser to run the google search test.The selenium session is started in setUp() method which is called before every test run and ends with tearDown() method which is called after every test run.Since setUp() and tearDown() are called before and after every test run so we can use only one test method to be run in each class if you want to start and stop selenium sessions with setUp() and tearDown() methods.Multiple tests with this fashion is a drawback i.e if you want to write multiple tests in a same class selenium session 'll start and stop for every test run.

A simple Selenium RC example

Here is a simple example that explains how to write a test using selenium rc(selenium 1).
Follow the following steps to write your 1st test using selenium rc.
  • Open your desired Java IDE (in my case Eclipse)
  • Create a java project.
  • Create a package 'test.example' and add a new class 'GoogleTest.java' to it.
  • Create a folder namely 'lib' and place the downloaded 'selenium-java-.jar' file and add it to your project buildpath.
//GoogleTest.java
  1. package test.example;
  2. import com.thoughtworks.selenium.DefaultSelenium;
  3. import com.thoughtworks.selenium.Selenium;
  4. /**
  5.  * Search Google example.
  6.  *
  7.  * @author aksahu
  8.  */
  9. public class GoogleTest {
  10.    
  11.     static Selenium selenium;
  12.    
  13.     public static void main(String[] args) {
  14.         selenium = new DefaultSelenium("localhost"4444"*firefox""http://www.google.com");
  15.         selenium.start();
  16.         selenium.windowMaximize();
  17.        
  18.         selenium.open("/");
  19.         selenium.type("id=lst-ib""selenium");
  20.         selenium.click("name=btnK");
  21.         selenium.click("link=Selenium - Web Browser Automation");
  22.         selenium.waitForPageToLoad("30000");
  23.        
  24.         selenium.stop();       
  25.     }
  26. }
This test opens "http://www.google.com" in firefox browser and search a keyword.
Note: Make sure that you have started the selenium server before running the test.

Wednesday, February 15, 2012

Running Selenium Server

Before running any tests we must start the selenium server.
Navigate to the directory where Selenium RC’s server is located and run the server from a command-line.
           java -jar selenium-server-standalone-<version-number>.jar
In simple create a batch or shell executable file (.bat on Windows and .sh on Linux) containing the command above. Then make a shortcut to that executable on your desktop and simply double-click the icon to start the server when ever required.
Note:
      For the server to run you’ll need Java installed and the PATH environment variable correctly configured to run it from the console. You can check that you have Java correctly installed by running the following on a console.
          java -version
If you get a version number (which needs to be 1.5 or later), you’re ready to start using Selenium RC.

Getting started with Selenium RC (selenium 1)

Mainly there are two components of selenium rc i.e
  • Selenium Server
  • Client libraries
The Selenium server receives the Selenese commands from your test program using simple HTTP GET/POST requests. This means you can use any programming language that can send HTTP requests to automate Selenium tests on the browser.
A Selenium client library provides a programming interface (API), i.e., a set of functions, which run Selenium commands from your own program. Within each interface, there is a programming function that supports each Selenese command.

Installation:
      Selenium RC supports various programming languages like java, csharp(C#), python, ruby, php, perl etc. In my explanation I am using java.

Installing Selenium Server:
      The Selenium RC server doesn’t require any special installation which is simply a Java jar file (selenium-server-standalone-.jar). Just download the zip file from SeleniumHQ  downloads page and extract the server in the desired directory.

Using the Java Client Driver:
      Download Selenium java client driver zip from SeleniumHQ  downloads page and extract selenium-java-
.jar file.

Steps after having both client and server jars

  • Open your desired Java IDE (Eclipse, NetBeans, IntelliJ, Netweaver, etc. in my case Eclipse)
  • Create a java project.
  • Add the selenium-java-.jar files to your project buildpath.
  • Write your Selenium test in Java using the selenium-java-client API. The API is presented later in this chapter.
  • Run Selenium server from the console.
  • Execute your test from the Java IDE or from the command-line.
Note: For details about how to configure selenium rc with eclipse go through the link Configuring Selenium RC With Eclipse.