Verify element is enabled in Selenium WebDriver


647
647 points
verify element is enabled
verify element is enabled

Verify element is enabled in Selenium WebDriver

There are menu situations when you want to verify if en element is enabled or disabled , selenium webdriver allows us to verify element is enabled following a quite simple API

Solution

isEnabled() – is a Selenium WebDriver method which is developed in Web Element class and allows us to check if an element is enabled or disabled on the page. Basically the statement returns a Boolean, (true or false) and we can value the result.

WebElement submit = driver.findElement(By.id("submit-button"));
boolean submit_button=submit.isEnabled();

Now submit button will be a true or false and we can use that information in different ways:

Assert element is enabled or disabled:
assertTrue(submit_button,"Exception");

assertFalse(submit_button,"Exception");
Perform an action based on isenabled() results:
if submit_button{

  //some code if element is enabled ;

} else {

 // some code if element is disabled ;

}

Selenium is really powerful in its own API and you can pretty much to whatever you want , I would like to quote one of the selenium developers :

Selenium automates browsers. That’s it. What you do with that power is entirely up to you. Primarily it is for automating web applications for testing purposes, but is certainly not limited to just that. Boring web-based administration tasks can (and should!) also be automated as well.

If you are a Ruby capybara user you are not left behind , you can easily check using  find_field method :

# find by label
find_field 'Filed label', disabled: true

 # Find a form field on the page. The field can be found by its name, id or label text.

If you want to return a boolean value you can use Node::Matchers   has_field?:

 # Checks if the page or current node has a form field with the given
 # label, name or id.

# For text fields and other textual fields, such as textareas and
      # HTML5 email/url/etc. fields, it's possible to specify a :with
      # option to specify the text the field should contain:


 page.has_field?('Name', with: 'Jonas' ,disabled: true)

Happy testing!


Like it? Share with your friends!

647
647 points
Test engineer

0 Comments

Your email address will not be published. Required fields are marked *

two × one =