Showing posts with label Examples. Show all posts
Showing posts with label Examples. Show all posts

July 30, 2015

How To Test For Disabled Elements

July 30, 2015 4
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();
	}

}