You are currently browsing the tag archive for the ‘Selenium’ tag.
In this posting I document extending Selenium server with a custom javascript method, getAllRadios()
, to return all radio button elements in a page. It’s based on the getAllButtons()
that is stock in Selenium.
I will demonstrate two ways to add this custom method. The first strategy is to add the custom method to the native javascript files that ship with Selenium server. This is not the recommended way to extend Selenium but it is an almost guaranteed way to be sure the server picks up my new method and that frees me to focus on getting my client syntax right for calling the new method using the client’s doCommand()
method.
Method 1 – selenium-browserbot.js + selenium-api.js
Unpack the server jar into a temporary directory to get access to the selenium-browserbot.js
and selenium-api.js
files.
$ jar xf ../selenium-server.jar
The new custom method is added to core/scripts/selenium-browserbot.js
. It is a slightly modified version of getAllButtons()
which is defined in the same file.
BrowserBot.prototype.getAllRadios = function() {
var elements = this.getDocument().getElementsByTagName('input');
var result = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].type == 'radio') {
result.push(elements[i].id);
}
}
return result;
};
and then the new method is exposed to the Selenium API in core/scripts/selenium-api.js
, again modeling after getAllButtons()
in selenium-api.js
.
Selenium.prototype.getAllRadios = function() {
return this.browserbot.getAllButtons();
};
Now rebundle the updated javascript into a new server jar, replacing the original.
[21:01 20090906 crashing@server /selenium-server-1.0.1/temp]
$ jar cmf META-INF/MANIFEST.MF ../selenium-server.jar .
Method 2 – user-extension.js
The better way to extend Selenium is to add methods to the user-extensions.js
file. This avoids screwing around with the native server jar. I initiall had some trouble getting this working, hence the Method 1 approach, but after a bit of futzing I finally got this to work.
Selenium.prototype.getAllRadios = function() {
var elements = this.browserbot.getDocument().getElementsByTagName('input');
var result = [];
for (var i = 0; i < elements.length; i++) {
if (elements[i].type == 'radio') {
result.push(elements[i].id);
}
}
return result;
};
This is basically the same method used in selenium-browserbot.js
. The important changed bits are (1) the method is attached to the Selenium object instead of the BrowserBot (Selenium.prototype
rather than BrowserBot.prototype
) and (2) calling getDocument()
on the browserbot
instance.
I start the Selenium server with the -userExtensions
option pointing to the user-extensions.js
file.
/selenium-server-1.0.1/selenium-server.jar \
-userExtensions /user-extensions.js
Client Coding
For either of the above methods of extending the Selenium server, I call this new method in my client code with the doCommand()
.
proc = new HttpCommandProcessor(seleniumServerHost,
seleniumServerPort, brower, seleniumServerStartUrl);
selenium = new DefaultSelenium(proc);
selenium.open(url);
// radio buttons returned as comma-delimited strings
String allRadios = proc.doCommand("getAllRadios", null);
// alternatively, get radio buttons as a String array
String[] radios = proc.getStringArray("getAllRadios", null);
for (String radio : radios) {
System.out.println(radio);
}
To call custom doCommand()
‘s, it’s necessary to instantiate with DefaultSelenium(proc)
to inject the HttpCommandProcessor
into the DefaultSelenium
object.
Related:
Select the ids of radio buttons in a page using selenium
User-Extensions
Hot on the heels of yesterday’s posting about my brief experience with YSlow in a Selenium framework, comes the release of Cesium 0.1, a tool specifically designed for automating YSlow runs.
I don’t care enough about YSlow tests to setup and maintain another test harness just for that purpose, so I don’t think I’ll be trying it myself. For others who get in to this sort of thing, here’s something to get in to.
Adam Goucher has a clever idea to automate YSlow reporting to a Show Slow database with Selenium.
Well, I tried this with my Selenium RC setup, testing one page. In my setup, two beacons are sent by YSlow to the Show Slow database but the urls recorded,
http://localhost:3000/selenium-server/core/Blank.html?start=true
and
chrome://src/content/RemoteRunner.html?sessionId=2d7695881a3881f940ada5bc889a6dee&multiWindow=true&baseUrl=http%3A%2F%2Fintegrate.crashingdaily.com&debugMode=false&driverUrl=http://localhost:3000/selenium-server/driver/
are not the ones for the page under test. Neither report generated from selenium matches the report I get from the standalone browser so it’s not just a matter of the url being misreported to Show Slow.
disappointing.
Firefox 3.0.3 (on OS X at least) launched by Selenium RC 1.0 beta 1 displays an alert dialog box:
Alert sb-ssl.google.com:443 uses an invalid security certificate. The certificate is not trusted because the issuer certificate has expired. (Error code: sec_error_expired_issuer_certificate)
I’ve seen a report that the nightly build doesn’t have this issue but, as a quick fix for Se 1.0b1, one can edit /Applications/Firefox 3.0.3.app/Contents/MacOS/defaults/pref/firefox.js
and change a couple of "browser.safebrowsing"
preferences to false
.
pref("browser.safebrowsing.enabled", false); pref("browser.safebrowsing.malware.enabled", false);
That disables Google Safe Browsing – a feature which isn’t important to me but maybe it is to you, in which case this solution isn’t your cup of tea.
Related
Browser.safebrowsing.enabled
Google Safe Browsing for Firefox