Skip to content

Instantly share code, notes, and snippets.

@tarun3kumar
Created January 4, 2012 07:06
Show Gist options
  • Save tarun3kumar/1558896 to your computer and use it in GitHub Desktop.
Save tarun3kumar/1558896 to your computer and use it in GitHub Desktop.
Retrieve Dynamic Objects from Seleneium
/**
* Provides object ids or name for HTML object input
*
* @param inputType Could be checkbox, TextBox or Radio button
* @return objectId
*
*/
public static String[] getInputObjects(String inputType) {
StringBuilder sb = new StringBuilder("var objectId = [];")
.append("var cnt = 0;")
.append("var objectFields = [];")
.append("objectFields = window.document.getElementsByTagName('input');")
.append("for(var i=0; i
.append("if(objectFields[i].getAttribute('type') == '")
.append(inputType).append("'){")
.append("objectId[cnt] = objectFields[i].id || objectFields[i].name;")
.append("cnt += 1;}}objectId.toString();");
String script = selenium.getEval(sb.toString()).trim();
String[] objectId = script.split(","); // Split the string.
return objectId;
}
/**
* Provides object id or name of HTML objects
*
* @param objectType Could be text, textarea etc
* @return objectId
*/
public static String[] getCommonObjects(String objectType) {
StringBuilder sb = new StringBuilder("var objectId = [];")
.append("var cnt = 0;")
.append("var objectFields = [];")
.append("objectFields = window.document.getElementsByTagName('")
.append(objectType).append("');")
.append("for(var i=0; i
.append("objectId[cnt] = objectFields[i].name || objectFields[i].id;")
.append("cnt += 1;}objectId.toString();");// Convert array in to string.
String script = selenium.getEval(sb.toString()).trim();
String[] objectId = script.split(","); // Split the string.
return objectId;
}
/**
* Retrieves all dropdown objects in a web page
* @return dropDownObjects
*/
public static String[] getAllDropDowns () {
String[] dropDownObjects = getCommonObjects("select");
return dropDownObjects;
}
/**
* Retrieves all multiline text box objects in a web page
* @return textareaObjects
*/
public static String[] getAllMultiLineTextbox() {
String[] textareaObjects = getCommonObjects("textarea");
return textareaObjects;
}
/**
* Retrieves all radio button objects in a web page
* @return radioButtonObjects
*/
public static String[] getAllRadioButtons () {
String[] radioButtonObjects = getInputObjects("radio");
return radioButtonObjects;
}
/**
* Retrieves all check box objects in a web page
* @return checkBoxObjects
*/
public static String[] getAllCheckBoxes () {
String[] checkBoxObjects = getInputObjects("checkbox");
return checkBoxObjects;
}
/**
* Retrieves all text box objects in a web page
* @return textBoxObjects
*/
public static String[] getAllTextboxes () {
String[] textBoxObjects = getInputObjects("text");
return textBoxObjects;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment