Reputation: 912
I have a scenario in which there is a text box and when i type something in that text box it auto-populates. I need to trace that auto populated values and verify that it contains the string that i have entered in the text field. Can someone please help me out with this.? Thanks in advance
Upvotes: 2
Views: 5897
Reputation: 5667
Try something like this
Below code is for getting auto populated result for google site.
driver.get("http://www.google.co.in");
driver.findElement(By.id("lst-ib")).sendKeys("Test");
List<WebElement> autoPopulatedList=driver.findElements(By.cssSelector("tr>td>span"));
for(WebElement ele:autoPopulatedList)
{
System.out.println(ele.getText());
if(ele.getText().contains("Test"))
{
System.out.println("Your case passed..!!");
}
}
Upvotes: 1
Reputation: 4259
driver.get(URL); // URL = http://www.google.co.in;
driver.findElement(By.id(id_of_the_element)).sendKeys(value); //Value for the field
List autoPopulatedList=driver.findElements(auto_populate_element_path);
int autoPopulateSize = autoPopulatedList.length(); // Take the auto populate size to compare
int testedSize = 0; //initialize a variable for testing
for(WebElement element : autoPopulatedList){
if(element.getText().contains(value)) //Checking the autopopulated list with the value
{
testedSize++; // this will add 1 if the autopopulate contains the value
}
}
if(autoPopulateSize == testedSize){
System.out.println("Autopopulate contains the values");
}else{
System.out.println("Fail");
}
Upvotes: 0
Reputation: 323
Just Send text threw Sendkey And then get field value like this :
driver.findElement(By.id("field ID here")).sendkeys("ABC");
String strActualValue = driver.findElement(By.id("field ID here")).getAttribute("value");
System.out.println("Field value is = "+strActualValue +"");
it will print the value which you have sent by send key and the value which was auto-populated in the field.
Result : ABC1234
ABC is your value 1234 is auto-populated value.
ENJOY!
Upvotes: 0