July 30, 2015

How To Test For Disabled Elements

Long back i saw some post from Dave Haeffner  and he is writer of Elemental Selenium -- a free, once weekly Selenium tip newsletter that's read by thousands of testing professionals, also the author of The Selenium Guidebook; a guide on how to use Selenium successfully.

I am receiving some weekly tips from Dave Haeffner's site and those are in Ruby language. i want to rewrite them in Java. Using Selenium webdriver + TestNg + Java

Problem:
On occasion you may have the need to check if an element on a page is disabled or enabled. Sounds simple enough, but how do you do it? 

Solution:
We have a available method called isEnabled(). which will help us in resolving the above problem.
boolean org.openqa.selenium.WebElement.isEnabled()


  • isEnabled

    boolean isEnabled()
    Is the element currently enabled or not? This will generally return true for everything but disabled input elements.
    Returns:
    True if the element is enabled, false otherwise.


package sample;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class Enable_test {
	public WebDriver driver;

	@Test
	public void testEnable_test() throws Exception {

		driver.get("http://the-internet.herokuapp.com");
		driver.findElement(By.linkText("Dropdown")).click();
		List<WebElement> dropdown=driver.findElements(By.xpath("//select[@id='dropdown']/option"));
		for (int i = 0; i < dropdown.size(); i++) {
			if(dropdown.get(i).getText()=="Please select an option")
			{
				//assert false will always expects false value. 
				//Here option is disabled so it will return false
				Assert.assertFalse(dropdown.get(i).isEnabled());
			}
		}
	}

	@BeforeClass
	public void beforeClass() {
		driver = new FirefoxDriver();
		driver.manage().window().maximize();
		driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
		
	}

	@AfterClass
	public void afterClass() throws Exception {

		driver.quit();
	}

}

4 comments:

  1. Thanks for your post; selenium is most trusted automation tool to validate web application and browser. This tool provides precise and complete information about a software application or environment.
    Regards,
    Best Selenium Training Institute in Chennai

    ReplyDelete
  2. Selenium is a portable software testing framework for web applications. Selenium provides a record/playback tool for authoring tests without learning a test scripting language (Selenium IDE).Selenium IDE is an integrated development environment for Selenium tests.
    Regards,
    Selenium training institute in Chennai | Selenium training Chennai

    ReplyDelete
  3. Thank you for sharing in this article, I can learn a lot and could also be a reference, I hope to read the next your article updates.
    Software Testing Training in chennai | Testing Training in chennai

    ReplyDelete