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
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;
}
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
}
}
did u tried.
driver.findElements(By)
This will give you a list of all webelements available on the page using the locator u specified.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With