Extensions to Selenium have to be placed in a file named user-extensions.js as described in the reference guide. In version 0.6 the file is placed in th [selenium-home] directory, while in 0.7 it has to be placed in the [selenium-home]/core/scripts directory.
Adding a new option to dropdown and selecting it
The following action adds a new option to a given HTML dropdown and selects it:
/*
* Adds a new option to a dropdown list and selects that option.
* locator - locator for the dropdown element to add the option to
* text - the option value to set.
*/
Selenium.prototype.doSelectNew = function(locator, text) {
var element = this.page().findElement(locator);
if (element.type.match(/select-+/i)) {
element.options[element.options.length] = new Option(text, text);
element.options[element.options.length - 1].selected = true;
}
}
Example:
| open | /dropdown.html | |
| verifySelected | teammember | Rick |
| selectNew | teammember | Bob |
| verifySelected | teammember | Bob |
This particular excample loads a page, check that the option with label "Rick" is selected, adds an option with label and value 'Bob', and finally checks that the selected option is 'Bob'.
Look up a given cookie
The following locator locates and returns a given cookie on the document.
// Looks up a given cookie
//
//
PageBot.prototype.locateElementByCookie = function(text, inDocument) {
var cookieMatch = new RegExp(";?" + text + "\=([^;]+)").exec(inDocument.cookie);
if (null != cookieMatch) {
var retObj = new Object();
retObj.value = retObj.textContent = cookieMatch[1];
retObj.type = "";
return retObj;
}
return null;
};
Example:
| assertValue | cookie=prefLanguage | en-US |
This examples checks that the cookie named 'prefLanguage' has the value 'en-US'.
Removing cookies
The following action removes cookie with a given name, belonging to a given path:
// The following examples try to give an indication of how Selenium can be extended with javascript.
function createCookie(doc, name,value, path,days)
{
if (!path) {
path = "/";
}
if (days)
{
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
doc.cookie = name+"="+value+expires+"; path="+path;
}
/*
* Removes the cookie with the given name.
* text - the cookie name
* path - the cookie path
*/
Selenium.prototype.doRemoveCookie = function(text, path) {
createCookie(this.page().currentDocument, text, "", path, -1);
};
I found the code for the createCookie on the Selenium forums. I just added a member function on the Selenium object prototype named doRemoveCookie which in effect creates a Selenium action 'removeCookie'.
Example:
| removeCookie | JSESSIONID | /admin |
| removeCookie | JSESSIONID | / |
In the example above, I call the removeCookie twice, one removing a JSESSIONID cookie set on the root (/) and one removing a JSESSIONID cookie set on the directory /admin. In some cases, I found that it would be relevant to have to give the path information as some servers set cookies with the same name on different paths.