We can add custom functions to selenium or extend your selenium jar functions as you want. We can do this in 2 ways.
1. Extending selenium-browserbot.js and selenium-api.js
a. Extract your selenium-server-standalone-xxx.jar to temp folder.
b. Find the files selenium-browserbot.js and selenium-api.js inside core\scripts\ folder.
c. Add your custom function to class BrowserBot like below in selenium-browserbot.js.
d. Now add your function to class selenium to expose Selenium API like
Here is the example code:
To add in selenium-api.js,
2. User-Extension.js
a. Just add your custom functions in user-extensions.js by adding function to class selenium.
b. Important point is, here you have use getDocument() for browserbot to do your operations.
c. Finally just start your selenium server JAR with your user-extenstions.js like
java -jar /selenium-server-1.0.1/selenium-server.jar -userExtensions /user-extensions.js
d. Thats all. Your functions are available now in JVM.
e. To use this, create instance for httpCommandProcessor and use doCommand(yourFunctionName) OR simply selenium.youFunctionName.
Here is the example code:
Hope this helps you to create your own functions to extend your selenium-server.jar.
1. Extending selenium-browserbot.js and selenium-api.js
a. Extract your selenium-server-standalone-xxx.jar to temp folder.
b. Find the files selenium-browserbot.js and selenium-api.js inside core\scripts\ folder.
c. Add your custom function to class BrowserBot like below in selenium-browserbot.js.
- BrowserBot.prototype.yourFunctionName = function() {
- //code here
- }
d. Now add your function to class selenium to expose Selenium API like
- selenium.prototype.yourFunctionName = function() {
- return this.browserbot.youFunctionName();
- }
e. Thats all. Re-bundle your JAR file with updated files. Your Selenium-Server JAR file is ready with your custom functions.
Here is the example code:
- 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;
- };
To add in selenium-api.js,
- Selenium.prototype.getAllRadios = function() {
- return this.browserbot.getAllButtons();
- };
2. User-Extension.js
a. Just add your custom functions in user-extensions.js by adding function to class selenium.
b. Important point is, here you have use getDocument() for browserbot to do your operations.
c. Finally just start your selenium server JAR with your user-extenstions.js like
java -jar /selenium-server-1.0.1/selenium-server.jar -userExtensions /user-extensions.js
d. Thats all. Your functions are available now in JVM.
e. To use this, create instance for httpCommandProcessor and use doCommand(yourFunctionName) OR simply selenium.youFunctionName.
Here is the example code:
- 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;
- };
Hope this helps you to create your own functions to extend your selenium-server.jar.
Comments
Post a Comment