Adding custom commands to Selenium


In the previous Selenium posts we saw how to use the selenium IDE for testing web pages. In this post we will see how to extend the Selenium IDE by adding our own custom commands.

Selenium IDE offers many useful commands (often called Selenese) for testing purposes. You use this sequence of commands to create various tests. But many times these commands are not sufficient and you need to add your own custom commands. For example a reader recently requested on how to input unique email ids for testing. Of course you can easily do this using the Selenium RC server and a language like PHP or Java. But we want to accomplish this in the IDE itself.

Types of Selenium commands

Selenium commands come in three varieties : Actions, Accessors and Assertions. Each Selenium command being given in the format below:

command target value

Actions are commands which in general modify the state of the application; click, typeKeys, deleteCookie etc. are all actions. Many actions also come with a sibling with a “AndWait” suffix added; e.g. ‘typeKeysAndWait’. This tells Selenium to wait until the page loads after performing the particular action.

Accessors examine the state of the application and store the results in variables, e.g. “storeLocation”.

Assertions are like Accessors, but they verify that the state of the application conforms to what is expected

In this post we will see how to write Selenium Action command using a simple example.

Writing your first custom Selenium Action Command

In the following example we will add a new action, ‘typeRandomEmail’, which generates a random email address and types it in an input field.

Every Selenium Action command starts with a ‘do’ prefix. The corresponding action comprising ‘AndWait’ is automatically registered. e.g when we create the ‘typeRandomEmail’ action, the ‘typeRandomEmailAndWait’ command is automatically added to the command list. An action command takes up to two parameters, which will be passed the second and third column values in the test table. In our example we will be using only one parameter – the locator id where the random email will be typed.

The following shows the code for the typeRandomEmail action.

 
Selenium.prototype.doTypeRandomEmail = function(locator) {
    /**
    * Sets the value of an input field to a random email id, 
    * as though you typed it in.
    *
    * @param locator an <a href="#locators">element locator</a>
    */
 
    // All locator-strategies are automatically handled by "findElement"
    var element = this.page().findElement(locator);
 
    /* The following block generates a random email string */
    var allowedChars = "abcdefghiklmnopqrstuvwxyz";
    var stringLength = 8;
    var randomstring = '';
 
    for (var i=0; i<stringLength; i++) {
        var rnum = Math.floor(Math.random() * allowedChars.length);
        randomstring += allowedChars.substring(rnum,rnum+1);
    }
 
    // Append a domain name
    randomstring += "@somedomain.com"
 
    // Replace the element text with the new text
    this.browserbot.replaceText(element, randomstring);
};

Importing your custom commands in Selenium

Now that we have completed writing our own action command, it is time to import it to the Selenium IDE. Your custom commands are usually added to the file ‘user-extensions.js’. By default the ‘user-extensions.js’ file does not exist, so you need to first create a blank file into which you will add the above code. Once that is done you then need to tell Selenium to load the particular extension file from the ‘Options’ menu.

selenium_options_page

Once you have added the file to the options page you need to restart the Selenium IDE for the changes to take effect.

Whenever you add some new code or make changes to the exiting one in the ‘user-extensions.js’, you need to restart the Selenium IDE for the changes to take effect.

Now once you have restarted the IDE, you can see that your command has been added to the drop-down.

selenium_ide

Writing other Action commands

You may be wondering how to go about writing other Selenium Action commands; what the this.browserbot object means and where it is defined. Although no clear documentation exists, the Selenium source code can itself be a valuable tool. All the Selenium commands are stored in the ‘selenium-api.js’ file. I have included it here for easy download and reference. The original source file and other support files are located in the ‘selenium-ide.jar’ file in your Selenium installation directory. Unzip that file and you have all the Selenium source code at your disposal.

This site is a digital habitat of Sameer Borate, a freelance web developer working in PHP, MySQL and WordPress. I also provide web scraping services, website design and development and integration of various Open Source API's. Contact me at metapix[at]gmail.com for any new project requirements and price quotes.

13 Responses

1

sandip

May 26th, 2010 at 10:01 pm

Hi,,,

The custom command is really very good.

can you tell me from where i can get more custom commands??

2

sandip

May 30th, 2010 at 10:54 pm

Hi,

Is there any command to rerun the test case automatic. means i want to
add 50 users.. so how can i add through selenium?

is it possible?

3

WhiteAngel

June 10th, 2010 at 7:33 am

Hy,

I have a question: Why doesn’t Selenium IDE save the modification that I made in source page. When I exit the window is asking me if I want to save changes and of course I press Save button. But when I reopen the test case it is exactly the same as before and modifications gone. Is there some workaround trick for this or I’m missing something.

Thank you

sameer

June 10th, 2010 at 9:41 am

Selenium saves only the commands you have modified in the source, not any other html.

5

priti

June 17th, 2010 at 1:11 am

Hi,

I am using selenium for web testing, i know recording and execution of Selenium scripts.but don’t know how to write scripts without recording.is there any solution or guide to create selenium scripts withour recording

sameer

June 18th, 2010 at 3:03 am

You will need to use Selenium RC. Try these link:

http://seleniumhq.org/docs/04_selenese_commands.html

7

WhiteAngel

June 18th, 2010 at 8:04 am

Thanks guys, how about Hudson does anyone knew something about? I found some documentations but not to relevant.

8

Ateeq

August 27th, 2010 at 2:43 am

Dear Sameer, Iam a very new user to selenium, i was little confused, can you please tell me , can selenium tool be used to test the browser compatibility of a site? Ie some page element is not properly arranged in one browser but will be perfect in another. can this be done? Thanks a lot in advance.

sameer

August 27th, 2010 at 8:16 pm

Ateeq, you cannot check the visual layout in Selenium, if that is what you are asking. Yes, you can check individual elements of a web page, their content, the relative placements of elements etc, that is everything that is accessible as a source code.

10

Selenium IDE user extension example 3 « selenium online training with expert

November 28th, 2012 at 8:07 am

[...] is taken from Website for learning [...]

11

Jessi

January 26th, 2013 at 12:34 am

If I export a test case that uses a custom action command as web-diver to a development environment, where would I need to save the custom function so that the program is able to access the function when the code is compiled?

12

Saba Manzoor

February 19th, 2013 at 4:57 am

Required solution for a problem:

Id is fixed ‘qualityassurance’ I want to generate ids like:
1- q.ualityassurance

For second run id should be like:

2- qu.alityassurance

for third run it should be like:
3- qua.lityassurance

and so on till id’s length. But i dont understand how to recall function for next run with incremented value.

Code is:

Selenium.prototype.doNothing = function(){

// The do in front of Nothing in the function is what tells the system this is a global function

}

Selenium.prototype.doRandomEmail = function(locator, num)
{
var id = “qualityassurancetask”;
var stringLength = id.length;
var randomstring = ”;
var insstring = ‘.’;
var num = 1;

//var rnum = Math.floor(Math.random() * stringLength);

var remainingstring = id.substring(num,id.length);

randomstring += id.substring(0,num).concat(insstring, remainingstring);
randomstring += “@gmail.com”

num= num+1;

selenium.doType(locator,randomstring,num);
}

13

Shawn

May 2nd, 2013 at 5:20 am

Hi All,

Thank you very much for the post. I have a quick question. How can I call an in-built function (Eg. doVerifyTitle) and get its result from a custom function?

Eg.

Selenium.prototype.doMyVerifyTitle = function(locator, text) {
var result = Selenium.prototype.doVerifyTitle(locator, text);
storedVars['result'] = result;
};

My aim is to get the result in a js and log it in an HTML table so as to have an easy view of test results rather than going through the log.

Any thoughts are much appreciated.

Regards,
Shawn.

Your thoughts