Thursday 29 August 2013

Overview of Selenium RC and Installation RC

Selenium 1 (Selenium RC)
Introduction
As you can read in brief history of selenium project, Selenium RC was the main Selenium project for a long time, before the WebDriver/Selenium merge brought up Selenium 2, the newest and more powerful tool.
Selenium 1 is still actively supported (mostly in maintenance mode) and provides some features that may not be available in Selenium 2 for a while, including support for several languages (Java, Javascript, Ruby, PHP, Python, Perl and C#) and support for almost every browser out there.
How Selenium RC Works
First, we will describe how the components of Selenium RC operate and the role each plays in running your test scripts.
RC Components
Selenium RC components are:
  • The Selenium Server which launches and kills browsers, interprets and runs the Selenese commands passed from the test program, and acts as an HTTP proxy, intercepting and verifying HTTP messages passed between the browser and the AUT.
  • Client libraries which provide the interface between each programming language and the Selenium RC Server.
Here is a simplified architecture diagram....



















The diagram shows the client libraries communicate with the Server passing each Selenium command for execution. Then the server passes the Selenium command to the browser using Selenium-Core JavaScript commands. The browser, using its JavaScript interpreter, executes the Selenium command. This runs the Selenese action or verification you specified in your test script.
Selenium Server
Selenium Server receives Selenium commands from your test program, interprets them, and reports back to your program the results of running those tests.
The RC server bundles Selenium Core and automatically injects it into the browser. This occurs when your test program opens the browser (using a client library API function). Selenium-Core is a JavaScript program, actually a set of JavaScript functions which interprets and executes Selenese commands using the browser’s built-in JavaScript interpreter.
The 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.
Client Libraries
The client libraries provide the programming support that allows you to run Selenium commands from a program of your own design. There is a different client library for each supported language. 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.
The client library takes a Selenese command and passes it to the Selenium Server for processing a specific action or test against the application under test (AUT). The client library also receives the result of that command and passes it back to your program. Your program can receive the result and store it into a program variable and report it as a success or failure, or possibly take corrective action if it was an unexpected error.
So to create a test program, you simply write a program that runs a set of Selenium commands using a client library API. And, optionally, if you already have a Selenese test script created in the Selenium-IDE, you can generate the Selenium RC code. The Selenium-IDE can translate (using its Export menu item) its Selenium commands into a client-driver’s API function calls. See the Selenium-IDE chapter for specifics on exporting RC code from Selenium-IDE.
Installation
Installation is rather a misnomer for Selenium. Selenium has set of libraries available in the programming language of your choice.
Once you’ve chosen a language to work with, you simply need to:
  • Install the Selenium RC Server.
  • Set up a programming project using a language specific client driver.
Installing Selenium Server
The Selenium RC server is simply a Java jar file (selenium-server-standalone-<version-number>.jar), which doesn’t require any special installation. Just downloading the zip file and extracting the server in the desired directory is sufficient.
Running Selenium Server
Before starting any tests you must start the server. Go to the directory where Selenium RC’s server is located and run the following from a command-line console.
java -jar selenium-server-standalone-<version-number>.jar
This can be simplified by creating 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.
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.
Using the Java Client Driver
  • Download Selenium java client driver zip from the SeleniumHQ 
  • Extract selenium-java-<version-number>.jar file
  • Open your desired Java IDE (Eclipse, NetBeans, IntelliJ, Netweaver, etc.)
  • Create a java project.
  • Add the selenium-java-<version-number>.jar files to your project as references.
  • Add to your project classpath the file selenium-java-<version-number>.jar.
  • From Selenium-IDE, export a script to a Java file and include it in your Java project, or write your Selenium test in Java using the selenium-java-client API. The API is presented later in this chapter. You can either use JUnit, or TestNg to run your test, or you can write your own simple main() program. These concepts are explained later in this section.
  • Run Selenium server from the console.
  • Execute your test from the Java IDE or from the command-line.
For details on Java test project configuration, see the Appendix sections configuring selenium RC with enclipse and configuring RC with Installi.
Using the Python Client Driver
  • Install Selenium via PIP, instructions linked at SeleniumHQ 
  • Either write your Selenium test in Python or export a script from Selenium-IDE to a python file.
  • Run Selenium server from the console
  • Execute your test from a console or your Python IDE.
Using the .NET Client Driver
  • Download Selenium RC from the SeleniumHQ 
  • Extract the folder
  • Download and install NUnit ( Note: You can use NUnit as your test engine. If you’re not familiar yet with NUnit, you can also write a simple main() function to run your tests; however NUnit is very useful as a test engine.)
  • Open your desired .Net IDE (Visual Studio, SharpDevelop, MonoDevelop)
  • Create a class library (.dll)
  • Add references to the following DLLs: nmock.dll, nunit.core.dll, nunit. framework.dll, ThoughtWorks.Selenium.Core.dll, ThoughtWorks.Selenium.IntegrationTests.dll and ThoughtWorks.Selenium.UnitTests.dll
  • Write your Selenium test in a .Net language (C#, VB.Net), or export a script from Selenium-IDE to a C# file and copy this code into the class file you just created.
  • Write your own simple main() program or you can include NUnit in your project for running your test. These concepts are explained later in this chapter.
  • Run Selenium server from console
  • Run your test either from the IDE, from the NUnit GUI or from the command line
For specific details on .NET client driver configuration with Visual Studio, see the appendix.NET Client Driver Configuration.
Using the Ruby Client Driver
  • If you do not already have RubyGems, install it from Ruby forge
  • Run gem install selenium-client
  • At the top of your test script, add require "selenium/client"
  • Write your test script using any Ruby test harness (eg Test::Unit, Mini::Test or RSpec).
  • Run Selenium RC server from the console.
  • Execute your test in the same way you would run any other Ruby script.
For details on Ruby client driver configuration, see the selenium client documentation
From Selenese to a Program
The primary task for using Selenium RC is to convert your Selenese into a programming language. In this section, we provide several different language-specific examples.
Sample Test Script
Let’s start with an example Selenese test script. Imagine recording the following test with Selenium-IDE.
open
/

type
q
selenium rc
clickAndWait
btnG

assertTextPresent
Results * for selenium rc


Note: This example would work with the Google search page http://www.google.com
Selenese as Programming Code
Here is the test script exported (via Selenium-IDE) to each of the supported programming languages. If you have at least basic knowledge of an object- oriented programming language, you will understand how Selenium runs Selenese commands by reading one of these examples. To see an example in a specific language, select one of these buttons.

    /** Add JUnit framework to your classpath if not already there
     *  for this example to work
    */
package com.example.tests;

import com.thoughtworks.selenium.*;
import java.util.regex.Pattern;

public class NewTest extends SeleneseTestCase {
    public void setUp() throws Exception {
        setUp("http://www.google.com/", "*firefox");
    }
      public void testNew() throws Exception {
          selenium.open("/");
          selenium.type("q", "selenium rc");
          selenium.click("btnG");
          selenium.waitForPageToLoad("30000");
          assertTrue(selenium.isTextPresent("Results * for selenium rc"));
    }
}


In the next section we’ll explain how to build a test program using the generated code.

Wednesday 28 August 2013

Selenium IDE And Selenium TestNG with Eclipse Configuration


Selenium-IDE:

Selenium-IDE is the Integrated Development Environment for building Selenium test cases. It operates as a Firefox add-on and provides an easy-to-use interface for developing and running individual test cases or entire test suites. Selenium-IDE has a recording feature, which will keep account of user actions as they are performed and store them as a reusable script to play back. It also has a context menu (right-click) integrated with the Firefox browser, which allows the user to pick from a list of assertions and verifications for the selected location. Selenium-IDE also offers full editing of test cases for more precision and control. Although Selenium-IDE is a Firefox only add-on, tests created in it can also be run against other browsers by using Selenium-RC and specifying the name of the test suite on the command line.


Features:
·                     Easy record and playback
·                     Intelligent field selection will use IDs, names, or XPath as needed
·                     Auto complete for all common Selenium commands
·                     Walk through tests
·                     Debug and set breakpoints
·                     Save tests as HTML, Ruby scripts, or any other format
·                     Support for Selenium user-extensions.js file
·                     Option to automatically assert the title of every page

























Selenium-RC:
Selenium-RC allows the test automation developer to use a programming language for maximum flexibility and extensibility in developing test logic. For instance, if the application under test returns a result set, and if the automated test program needs to run tests on each element in the result set, the programming language’s iteration support can be used to iterate through the result set, calling Selenium commands to run tests on each item.
Selenium-RC provides an API (Application Programming Interface) and library for each of its supported languages: HTML, Java, C#, Perl, PHP, Python, and Ruby. This ability to use Selenium-RC with a high-level programming language to develop test cases also allows the automated testing to be integrated with a project’s automated build environment.

Selenium TestNG with Eclipse Configuration

In order to configure selenium TestNG with Eclipse we need to follow the below steps, I have also mentioned about configuring selenium server in eclipse with the help of which we will be able to see selenium server in console bar within eclipse, the steps are as follows.


Start Eclipse:

·                     Go to path where eclipse application is stored.
·                     Launch eclipse by double clicking the Eclipse icon.
·                     Browse the workspace directory where you want to store all your project contents.
·                     Eclipse is launched now.

Creating New Project:

·                     Goto to File menu select new than click on new project.
·                     From displayed list select JAVA project and click next.
·                     Give the appropriate project name than click next.
·                     Now to add external libraries click on libraries than click add external Jars..
·                     Browse the external Jars and add it.
·                     Click on finish button.




External JARs:

TestNG 5.14.1:
TestNG is a testing framework designed to simply a broad range of testing needs, from unit testing(testing a class in isolation of the others),functional testing,browser-compatibility testing with Selenium-RC to integration testing (testing entire systems made of several classes, several packages and even several external frameworks, such as application servers).
Now lets start implementing it.

First you need to record a script in selenium-ide,just try with a simple script and put some check points(to know how Selenium ide works visit www.mindqonline.com

Now convert your IDE script to any language as selenium supports multiple platforms like junit,TestNG,Python,C#.

Here i have used TestNG because of it's powerfull features like multi threading and annotaions

so convert the IDE script into TestNG format like this: