Tuesday, March 15, 2011

Selenium Simplified

I was in the NCRT-4th meet where I met a lot of people who were struggling to integrate junit with selenium IDE. The whole idea of how to record with Selenium IDE -->migrate the testcase to junit -->Integrating it with eclipse was a real pain for some of the attendees. I think what’s important is to make the first simple test-case run and then move ahead step by step. I thought of simplifying it a bit to help people to kick start with selenium integration.

Let’s list down the some prerequisite first. It’s very important to have these things installed/downloaded in the machine you are trying to setup:-


2.       IDE Installations :-

PS: - These links should be opened only on Firefox as the selenium IDE is available for Firefox only.

3.       Library to be added in Eclipse project class-path

(Do not add this in Eclipse project class path)

(Needed by eclipse)

Xpather: - Feature rich XPath generator, editor, inspector and simple extraction tool.
Firebug: - Firebug integrates with Firefox to put a wealth of development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.

Scenario to Test:-
 Open a url http://ajay-sharma.blogspot.com/ and check for “Ajay Sharma” link.


Selenium IDE

Steps:-
1.       Open link http://ajay-sharma.blogspot.com/ on your Firefox browser.
2.       On the Firefox toolbar select Tools-->Selenium IDE.
3.       On the Selenium IDE click on the red button to start recording the above scenario.
4.       On the link bar enter the URL http://ajay-sharma.blogspot.com/ .
5.       When the page opens right click on the link “Ajay Sharma” and select “show all available commands” --> assertTextPresent Ajay Sharma.
6.       Navigate back to Selenium IDE and stop the recording.
7.       Now playback the testcase to see there is no error.
If no errors wallah….you are through with you first simplest selenium testcase.

It’s time to take it to the next level and start working on some real stuff.

Export to Junit4:-
1.       On the Selenium IDE from top menu select the “Options -->Format -->Junit 4 (Remote Control).
2.       You will see the code like
package com.example.tests;

import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.regex.Pattern;

blah blah…….

You are through to the second step of the ladder. It’s time to setup Eclipse and start automating those monotonous regression cases.

Setting Up JAVA_HOME:-
You need to setup Java_Home to the installed JDK location before starting eclipse. To know more how to setup environmental variables visit the link:-

Setting Up your first Selenium Project:-
1.       Start eclipse and setup a workspace on desired location (In case eclipse is installed for the first time).
2.       Select File -->New -->Java Project.
3.       Type the Project Name as “FirstSeleniumFramework”.
4.       Select the installed JDK version in the section JRE.
5.       Click on the finish button.
6.       Right click on the project “FirstSeleniumFramework” and select new -->option folder.
7.       Name the folder as lib.
8.       Copy the selenium-java-client-driver-1.0.2.jar and junit-4.7.jar to lib folder.
It’s time to add the lib folder to project classpath.
9.       Right click on the project and click on properties.
10.   Select the LHS option “Java Build Path”.
11.   Select libraries from RHS top navigation bar.
12.   Click on the link add external jars and navigate to “${workspace}\FirstSeleniumFramework\lib” folder to add the above two jars or this might be any other folder where you have kept selenium-java-client-driver-1.0.2.jar & junit4.7.jar

All set to create your first Selenium Junit Testcase.

1.       Right click on src folder and click on create package option.
2.       Set Name as “com.example” and click on finish button.
3.       Again right click on newly created package “com.example” and select the option class.
4.       Set the name as “HomePage” and click on the finish button.
5.       Copy pastes the code from selenium IDE to the newly created class.
You will have few errors which will be corrected by changing the following:-

public class Untitled -- > public class HomePage 
package com.example.tests --> package com.example
public void testUntitled() --> public void testNamelink

You are done to execute your first “testNamelink” testcase.

Few things to understand here:-
selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://ajay-sharma.blogspot.com/");
localhost= The server where Selenium remote control is installed. You can set this up in some other machine too.
Port= Default port on RC server runs.
*chrome= Browser on which the test-case will be executed. (Other values can be *iexplore,*firefox,*safari,*opera).
Note: - The browser will be opened on the machine where the RC server is running. So for testing on various different browsers you need to install different browsers on the machine acting as RC server.

Start Selenium Remote Control:-
1.       Navigate to the location where you want to setup selenium server.
2.       Copy selenium-server-standalone-2.0b2.jar to the location and start the command prompt.
3.       Navigate to the location and type :-

java –jar selenium-server-standalone-2.0b2.jar –singlewindow

4.       Press enter to start the selenium server.
Finally after all the hard work we are done with project setup. Let’s start testing now. 

Right click on the file Homepage.java and select the option Run As -->Junit Test.

“I know everything will work fine.”

Well this is the simplest of way you can add a selenium test-case. You can use Xpather and firebug to add more complex cases. I will be talking more about this in my next blog.
I will be soon writing about integrating simplium and selenium-grid which will surely help in making selenium worth using for web application testing.

Don't forget to give your feedback if this article is somewhat proved helpful to you. In case of any errors/issues while setup just let me know.

5 comments:

  1. I've got the following error

    java -jar selenium-server-standalone-2.0b3.jar -singlewindow
    15:09:08.941 INFO - Java: Sun Microsystems Inc. 19.1-b02
    15:09:08.943 INFO - OS: Linux 2.6.18-194.32.1.el5 i386
    15:09:08.951 INFO - v2.0 [b3], with Core v2.0 [b3]
    15:09:09.115 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
    15:09:09.117 INFO - Version Jetty/5.1.x
    15:09:09.118 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
    15:09:09.119 INFO - Started HttpContext[/selenium-server,/selenium-server]
    15:09:09.120 INFO - Started HttpContext[/,/]
    15:09:09.145 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@13bd574
    15:09:09.145 INFO - Started HttpContext[/wd,/wd]
    15:09:09.147 WARN - Failed to start: SocketListener0@0.0.0.0:4444
    Exception in thread "main" java.net.BindException: Selenium is already running on port 4444. Or some other service is.
    at org.openqa.selenium.server.SeleniumServer.start(SeleniumServer.java:402)
    at org.openqa.selenium.server.SeleniumServer.boot(SeleniumServer.java:236)
    at org.openqa.selenium.server.SeleniumServer.main(SeleniumServer.java:201)

    do you know what i'm doing wrong?
    do you also know how i can run these without a GUI
    thx

    ReplyDelete
  2. Looks like you are running firewall which is not allowing you to connect to localhost. Can you check by disabling ur windows/linus firewall? Do let me know if it work or doesnt work.

    --Ajay

    ReplyDelete
  3. Also try restarting the machine and run netstat -ab to check there should'nt be any other process running on port 4444.(it will be java.exe most probably)killing that process will resolve the issue.

    ReplyDelete
  4. Hi,

    Did you use SeleniumServer.start() in your code? If yes remove that.

    Regards,
    Aruna

    ReplyDelete
  5. Remove SeleniumServer.start() ???

    ReplyDelete