Tuesday, April 17, 2012

Selenium remote control & Selenium Client API Introduction

Selenium remote control is a server implemented in Java, which accepts commands via http and perform requested operation in requested browser.

Selenium Client API is a framework developed to encapsulate the command implementation of the operations, we need might want to perform on browser via selenium remote control. Selenium Client API have been developed in multiple languages like java and .net.

Being a java programmer, here I will give examples using java language (wherever necessary)

The combination of Selenium remote control & Selenium client API is useful in following scenarios –
  1. Performing Automation testing of web based application.
  2. Performing some automated operation on the website and retrieve the result

Selenium Remote control and Selenium Client API frame work implements client sever model. Selenium remote control works as server and we implement the client to perform operations, using Selenium Client API.

In high level, we follow the following steps to use it –
  1. Start selenium remote control server
  2. Execute client possibly a java program to fire the commands and perform the operation
Details of the way to perform these operations is as mentioned below –
Starting the selenium remote control server –
Selenium remote control server can be started in two ways –
  1. Starting from command line as stand alone application – We can use the following command to start the selenium rc server.

    java -jar selenium-server.jar -trustAllSSLcertificates -multiWindow

    By default the server starts listening on port no 4444. If this port is not available to connected, You will get connection reset error while trying to connect from client. In this case, you can use another available port with below command line -

    java -jar selenium-server.jar -trustAllSSLcertificates -multiWindow -port 8080

    selenium-server.jar can downloaded from the following url - 

  2. Starting from with in the application – We can include the jar in the application and start the server from using below sample code -

    SeleniumServer server = new SeleniumServer ();

    By default it will listen at  port 4444 but we can reset it if needed before starting the server.
Starting selenium client code executionWe can use below section of code to
develop the selenium client code to perform operation on the respective site - 
 Selenium selenium = new DefaultSelenium( String seleniumServerHost, int seleniumServerPort,  
                      String browserType,  
                      String baseURL);  
 selenium.open ("http://www.somesite.com/somePage.html");  
 selenium.stop ();  

Demo -
Below is sample display of code of combination of selenium remote control server and selenium client in execution.

It performs below operations -
1- Open the browser
2- Type the url & go to site
3- Put the pin in site & click on go
4- Count the number of locations obtained
5- Retrieves pre-defined number of locations
6- Save them in a file.

Note  - If video looks to be blurred, kindly choose 720p (HD) quality option in the embedded player to get fine quality picture.

We can easily integration selenium in the project using below mentioned maven dependencies.


These dependencies exist on below mentioned repository -

Debugging – If selenium client faces any issues while connecting, we can test the proper functioning of each component independently. We can test the proper setup of server by running it using interactive mode. The steps to test are as mentioned below - 
  1. Run Selenium server in interactive mode. To do this run the command:
            java -jar selenium-server.jar -interactive -multiWindow
    Once the server starts in the interactive mode you will see a line that says:
    Entering interactive mode ... type selenium commands here

  2.  Now you can start a new selenium browser session using the command:
    This should open a new blank Firefox browser window.

  3. Now run the following command:
    If the browser starts and you see Google’s home page in it, your basic setup seems to be fine.

  4. Type quit to exit the interactive mode
Refenreces - .

1 comment: