Verify that all text input fields are empty with Selenium

16,375

Solution 1

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;
}

Solution 2

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
 }
}

Solution 3

did u tried.

driver.findElements(By)

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

Share:
16,375
Admin
Author by

Admin

Updated on June 09, 2022

Comments

  • Admin
    Admin almost 2 years

    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