Writing extensions

Tags: 

You can extend functionalities of Selenium IDE with JavaScript. There are 2 types of scripts in Selenium IDE - formats and extensions.

 

Formats are used to read and write test files. You can find how to write your own format in Adding Custom Format.

 

Extensions are executed when Selenium IDE is loaded, similar to user-extensions.js in Selenium Core. You can specify the location of your file in the options dialog of Selenium IDE.

 

Using extensions, you can extend following features of Selenium IDE:

 

  • Adding Event Handlers. Event Handlers record commands by listening to DOM events.
  • Adding Locator Builders. Locator Builders convert HTML elements into Element Locators that are used by many commands in Selenium. You can also change the priority of Locator Builders.
  • Adding Command Builders. Command Builders help users adding commands to the test by showing available commands in the context menu when you right-click the element.

 

Extensions can be used in Selenium IDE 0.8 and later.

 

Event Handlers

 

Event handlers can be added using following function:

 

Recorder.addEventHandler(handlerName, eventName, handler, options);

 

  • handlerName: The name of the handler. The name can be used to remove event handler later.
  • eventName: The name of the event you want to listen on, e.g. "click", "change"
  • handler: The function that is called when the event is fired. It takes one argument, event, which is an event object that has been fired. It should not return any value.
  • options (optional): An object that is used to pass options. You can set following properties:

 

Event handlers can be removed using following function:

 

Recorder.removeEventHandler(handlerName);

 

You can use removeEventHandler to disable default handlers and add your own handlers instead.

 

See built-in handlers in chrome://selenium-ide/content/recorder-handlers.js for the examples.

 

Locator Builders

 

Locator builders can be added using following function:

 

LocatorBuilders.add(name, builder);

 

  • name: The name of the builder.
  • builder: The function that is called to build the element locator. The function takes the HTML element as the argument, and returns element locator.

 

You can change the priority of locator builders by setting LocatorBuilders.order variable. This variable points to an array of the names of the builders. You can also disable any locator builder by excluding it from this array.

 

See built-in locator builders in chrome://selenium-ide/content/locatorBuilders.js for the examples.

 

Command Builders

 

Command builders can be added using following function:

 

CommandBuilders.add(type, builder);

 

  • type: The type of the command, either "action" or "accessor".
  • builder: The function that is called to build the command. The function takes one argument, window, which is a window object the user is working on. It must return an object representing a command. These are the properties that are used in a returning object:
    • command: If the type is "action", you must set this property to the name of the command. This property is not used if the type is "accessor".
    • accessor: If the type is "accessor", you must set this. If the command you're building are "(verify|assert|store|waitFor)TextPresent", this property will be "textPresent".
    • disabled: Set this property to true to disable the command.
    • target: "Target" of the command.
    • value: "Value" of the command.
    • booleanAccessor: Set this to true if the type is "accessor" and if the accessor returns a boolean value, such as isTextPresent.

 

See built-in command builders in chrome://selenium-ide/content/commandBuilders.js for the examples.