0

I'm stuck with one basic issue in Selenium WebDriver: How to test a capacity of the text field in the form? I have used .sendKeys(String) and then I tried to get the text back with .getText(), but it doesn't work. It retrieves an empty String. How to test the size of text field then? How to compare my input and actual data? This is my code:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.*;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
import org.testng.Assert;
public class main {
public static void main(String [] args){
WebDriver driver= new FirefoxDriver();
driver.navigate().to("http://energy-telecom.org/ServiceReview/quote.php");
String valid="0123456789";
WebElement element=driver.findElement(By.name("firstName"));
element.sendKeys(valid);
System.out.println("input: "+element.getText());
//Assert.assertEquals(element.getText(), valid, "Valid_TEXT");
}

} My output:

input:  

I know that I can use .getAttributes, but I'm curious can I retrieve this value. Obviously, the last line is commented because it gives an exception. Thank you. PS: Don't submit anything in this form pls :D

George Revkov
  • 95
  • 3
  • 8
  • What is `Valid_TEXT` in your code? Did you assign anything to this variable? Also please paste the exception your are getting which will be helpful for debugging – vkrams Jun 11 '13 at 23:11
  • Sure, I forgot to post an output; I updated my question – George Revkov Jun 11 '13 at 23:24
  • @GeorgeRevkov: You shouldn't use `getText()` for input fields, use `getAttributes()`, because the text you see is the value. What do you mean by "but I'm curious is it real to get back something that you put in this field"? – Yi Zeng Jun 11 '13 at 23:53
  • @GeorgeRevkov: Then use `getAttributes()`. You are talking about how many characters in the textbox or the actual size(length/width) of it? – Yi Zeng Jun 11 '13 at 23:55
  • I mean: is it real to retrieve this input? Thanks, it works if to use getAttribute("value"). – George Revkov Jun 11 '13 at 23:56
  • @GeorgeRevkov: What do you mean `is it REAL`? – Yi Zeng Jun 11 '13 at 23:56
  • Are there any ways to retrieve this value :D Sorry for my English. – George Revkov Jun 12 '13 at 00:00

1 Answers1

3

For input, the text is the value, so you may try

System.out.println("input: "+ element.getAttribute("value"));
Yi Zeng
  • 32,020
  • 13
  • 97
  • 125