Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Verify that all text input fields are empty with Selenium

I want to verify that all input text fields are empty with Selenium IDE/Webdriver. Suppose if there is a method that can return all HTML input elements with the attribute "text" or "textarea", then I could iterate over all of them and check if the text context is empty.

But I cannot find such a method. What other ways can I do this?

Thanks

like image 677
user1028980 Avatar asked Nov 04 '11 02:11

user1028980


3 Answers

Consider that .text will not work on input fields and you MUST get the value attribute.

bool areAllFieldsClear()
{
    var myFields = driver.FindElements(By.Xpath("//input"));
    foreach(var field in myFields)
    {
       if(field.GetAttribute("value") != "") {
           return false;  //field.Clear(); maybe?
    }
    return true;
}
like image 82
Highstead Avatar answered Oct 23 '22 03:10

Highstead


Here's how I do it. I catch all of the inputs text and textarea and password in some cases and store those elements in a list of IWebElements. Then I can iterate through the list and verify whatever I want.

private List<IWebElement> GetTextFields(IWebDriver driver)
{

 List<IWebElement> textFields;

 try{
  textFields.AddRange(driver.FindElements(By.CssSelector("input[type='text']").ToList());
 }
 catch {
  //throw exception or log exception
 }

 try {
  textFields.AddRange(driver.FindElements(By.TagName("textarea").ToList());
 }
 catch {
  //throw exception or log exception
 }

textFields.RemoveRange(i => !i.Displayed); //removes all hidden fields

return textFields
}

here's how you can verify no text with that list.

foreach(IWebElement element in textFields)
{
 if(element.text != "")
 {
  //log error or throw exception
 }
}
like image 23
CBRRacer Avatar answered Oct 23 '22 04:10

CBRRacer


did u tried.

driver.findElements(By)

This will give you a list of all webelements available on the page using the locator u specified.

like image 24
Varun Menon Avatar answered Oct 23 '22 04:10

Varun Menon