Selenium - User Extensions

selenium

By default, Selenium looks for a file called "user-extensions.js", and loads 
javascript code found in that file.  user-extensions.js provides a convenient 
location for adding extensions to Selenium, like new actions, checks and 
locator-strategies.

By default, this file does not exist. Users can create this file and place their 
extension code in this common location, removing the need to modify the Selenium 
sources, and hopefully help with the upgrade process.

We can find contributed extensions at: http://bit.ly/2kucFev

All do* methods on the Selenium prototype are added as actions.  For example, to 
add a typeRepeated action to Selenium, which types the text twice into a text 
box, we must name our method as doTypeRepeated:

Selenium.prototype.doTypeRepeated = function(locator, text) {
  var element = this.page().findElement(locator);
  var valueToType = text + text;
  this.page().replaceText(element, valueToType);
};

Notice that our function accepts two parameters.  The first parameter is the 
locator used to find the target element, and the second parameter is the text or 
value for the target element.  This is exactly like any typical Selenium 
command.

Inside our function, all locator-strategies are automatically handled by 
findElement.  In other words, in order to find the target element, we just have 
to use the findElement method as shown above.

The typeTwiceAndWait command will be available automatically.

All assert* methods on the Selenium prototype are added as checks.

Selenium.prototype.assertValueRepeated = function(locator, text) {
  var element = this.page().findElement(locator);
  var expectedValue = text + text; // Create the text to verify
  var actualValue = element.value; // Get the actual element value
  Assert.matches(expectedValue, actualValue);
};

The verify version will be available automatically.

All get* methods on the Selenium prototype result in the equivalent store, 
assert, assertNot, verify, verifyNot, waitFor, and waitForNot commands.  For 
example, a getTextLength method that returns the length of the text of a 
specified element, ill result in support for storeTextLength, assertTextLength, 
etc.

Selenium.prototype.getTextLength = function(locator) {
  eturn this.getText(locator).length;
};

All locateElementBy* methods are added as locator-strategies.  To add a 
"valuerepeated=" locator, that finds the first element with the supplied value, 
repeated:

PageBot.prototype.locateElementByValueRepeated = function(text, inDocument) {
  var expectedValue = text + text;  // Create the text to search for

  // Loop through elements, looking for ones that have value === expected value
  var allElements = inDocument.getElementsByTagName("*");
  for (var i = 0; i < allElements.length; i++) {
    var testElement = allElements[i];
    if (testElement.value && testElement.value === expectedValue) {
      return testElement;
    }
  }
  return null;
};

Selenium IDE commands are restricted to a three-column format.  However, some 
commands might need more information to execute the command.  For example, to 
create an extension that verify cell data, we need to know the row, column, and 
expected value.  We can specify these parameters in the value argument by 
separating parameter values using a pipe character.  Inside our extension 
function, we can split this string into values, and do our thing.

// Other interesting codes:
this.defaultTimeout = Selenium.DEFAULT_TIMEOUT;
this.browserbot.selectWindow("null");
this.browserbot.resetPopups();

this.browserbot.findElement(locator);
PageBot
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License