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 element locator
    // 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

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.

14 thoughts to “Adding custom commands to Selenium”

  1. Hi,,,

    The custom command is really very good.

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

  2. 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. 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

  4. 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 there any solution or guide to create selenium scripts withour recording

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

  6. 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.

  7. 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.

  8. 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?

  9. 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;


  10. 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.


  11. Hi, thank you for very helpful article.
    How to call command line external tool from custome action.
    For example i want to run dir in command prompt and get stdout result in text.
    I tried to use

    Selenium.prototype.doMyCustomAction = function(p_caption, p_text) {

    var exec = document.require(‘child_process’).exec;
    exec(‘dir’, function callback(error, stdout, stderr){

    but it raises TypeError: document.require is not a function.

    Thank you.

Leave a Reply

Your email address will not be published.