Thursday, 31 January 2013

Learning the API | Selenium Tutorial pdf

Learning the API

The Selenium-RC API uses naming conventions that, assuming you understand Selenese, much of the interface will be self-explanatory. Here, however, we explain the most critical and possibly less obvious, aspects of the API.

 => Starting the Browser

In C#:
selenium = new DefaultSelenium( "localhost" , 4444, "*firefox" , "http://www.google.com/" selenium.Start();
In Java:
setUp( "http://www.google.com/" , "*firefox" );
In Perl:
my $sel = Test::WWW::Selenium->new( host => "localhost" ,
port => 4444,
browser => "*firefox" ,
browser_url => "http://www.google.com/" );
 
In PHP:
$this->setBrowser("*firefox");
$this->setBrowserUrl("http://www.google.com/");
In Python:
self.selenium = selenium( " localhost " , 4444, " *firefox " ,
" http://www.google.com/ " )
self.selenium.start()
In Ruby:
if $selenium
@selenium = $selenium
else
@selenium = Selenium::SeleniumDriver.new( " localhost " , 4444, " *firefox " , " http://@selenium.start

Each of these examples opens the browser and represents that browser by assigning a “browser instance” to a program variable. This browser variable is then used to call methods from the browser. These methods execute the Selenium commands, i.e. like open or type or the verify commands.
The parameters required when creating the browser instance are:
host Specifies the IP address of the computer where the server is located. Usually, this is the same machine as where the client is running, so in this case localhost is passed. In some clients this is an optional parameter.
port Specifies the TCP/IP socket where the server is listening waiting for the client to establish a connection.
This also is optional in some client drivers.
browser The browser in which you want to run the tests. This is a required parameter.
url The base url of the application under test. This is required by all the client libs and is integral information for starting up the browser-proxy-AUT communication.
Note that some of the client libraries require the browser to be started explicitly by calling its start() method.

=> Running Commands

Once you have the browser initialized and assigned to a variable (generally named “selenium”) you can make it run Selenese commands by calling the respective methods from the browser variable. For example, to call the type method of the selenium object:
selenium.type( " field-id " , " string to type " )
In the background the browser will actually perform a type operation, essentially identical to a user typing input into the browser, by using the locator and the string you specified during the method call.

No comments: