Monday, March 1, 2010

Useful Functions and Notes while writing cases in Selenium RC

Useful Functions of Selenium

You have a huge list of selenium functions that we can use for writing test cases. Some of the main functions are:

isElementPresent(location) – Returns true or false based on whether “location” of the element passed is present on the page or not. Verifies that the specified element is somewhere on the page. This function can be used while you are working on an Ajax based system or in a system where you need to identify whether the element is present before we execute any action on the said "location". It is good to check if the location is present or not if your test case include clicking on some link or page refresh and then taking some action.

getValue(location)- Returns the data stored in the said “location” of the element that is passed to the function. Gets the (whitespace-trimmed) value of an input field (or anything else with a value parameter). For checkbox/radio elements, the value will be "on" or "off" depending on whether the element is checked or not.This function is useful when you need to get some data from some field or a particular location and need to use it in you program or testcase.

isTextPresent(text)- Returns true or false based on the specified text pattern appears somewhere on the rendered page shown to the user.Used to verify the whether a text present on the page or not.

click(location) - Clicks on a link, button, checkbox or radio button. If the click action causes a new page to load (like a link usually does), call "waitForPageToLoad" function.

waitForPageToLoad(timeout) - Waits for a new page to load. Selenium constantly keeps track of new pages loading, and sets a "newPageLoaded" flag when it first notices a page load. Running any other Selenium command after turns the flag to false. Hence, if you want to wait for a page to load, you must wait immediately after a Selenium command that caused a page-load.

type(locator,value) - Sets the value of an input field, as though you typed it in.

select(locator,option locator) - Select an option from a drop-down using an option locator. Using to select values from combo boxes.

Useful things while writing cases in Selenium

  1. While testing any Ajax based application use “waitForElementPresent” or “isElementPresent” before trying to do any action over the element.

  2. While writing your own case always remember to extend the “SeleneseTestCase” class.This create a “selenium” object and stores the browser session onto the created object.

  3. To set the browser on which you need to do execution of your test case add the following function to your test case class

public void setUp() throws Exception {

setUp(URL,browser name);


List of browsers that can used is “*firefox” -for firefox, “*iexplore” -for IE, “*safari” - for Safari, “*opera” - For Opera.This need to be placed in place of "browser name" while calling the "setUp" function.

  1. If you are internally going to call another function from your test case and going to execute some actions under it. Don't forget to pass the “selenium” object to the function otherwise your test case will fail. This happens because when you are executing a case ,the browser session details is stored with the “selenium” object and if you call a function without passing the selenium object to the function it wont be having any session details with it and hence the case will Fail.


Post a Comment