Advertise

Sunday, October 25, 2009

Selenium Get Attribute of Fields

Selenium Get Attribute of Fields

Below is an Example to get Attribute of Text Box using Selenium :




package Selenium;
import com.thoughtworks.selenium.*;
import org.openqa.selenium.server.SeleniumServer;
public class Main_Test extends SeleneseTestCase {

public void setUp() throws Exception {
/*
* Startting Selenium Server from Test Case itself
* */
SeleniumServer s = new SeleniumServer();
s.start();

/*
* Opening Google web page in Firefox
* */
setUp("http://www.google.co.in//", "*firefox");

}
public void testNew() {

selenium.open("http://www.google.co.in//");

/*
* Getting Property of Text Field like maxlength and size of Text Field
*
* */

String collectTextProperty = selenium.getEval("var property = new Array(); " +
"property[0] = window.document.getElementsByName('q')[0].maxLength;" +
"property[1] = window.document.getElementsByName('q')[0].size; property.toString();");
/* Replace getElementsByName('q') in this 'q' by element id of Textbox*/

String[] Textproperties = collectTextProperty.split(",");
for (int i = 0; i < Textproperties.lenght();i++){
/*
*Textproperties[0] give maxlength of Text Box.
*Textproperties[1] give size of Text Box.
**/

}
selenium.type("q", "selenium");
selenium.stop();
}



}

No comments:

Post a Comment