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 =;
    /* 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 += ""
    // 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.


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.


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.

13 Responses

  1. sandip says:


    The custom command is really very good.

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

  2. sandip says:


    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 says:


    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

  4. sameer says:

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

  5. priti says:


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

  6. sameer says:

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

  7. WhiteAngel says:

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

  8. Ateeq says:

    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.

  9. sameer says:

    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. Jessi says:

    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?

  11. Saba Manzoor says:

    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 += “”

    num= num+1;


  12. Shawn says:

    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?


    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.


  1. November 28, 2012

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