WebDriver provides some methods through which user can verify the visibility of the web elements. These web elements can be buttons, drop boxes, checkboxes, radio buttons, labels etc.
- isDisplayed()
- isSelected()
- isEnabled()
Syntax:
isDisplayed():
boolean buttonDisplay = driver.findElement(By.id(“abcd”)).isDisplayed();
isSelected():
boolean radioButtonSelected = driver.findElement(By.id(“xyz”)).isSelected();
isEnabled():
boolean saveButtonEnabled = driver.findElement(By.id(“lmn”)).isEnabled();
WebDriver Code goes as below:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Visibility {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.manage().timeouts().implicitlyWait(20L, TimeUnit.SECONDS);
driver.manage().window().maximize();
boolean buttonVisibility = driver.findElement(By.xpath(".//*[@id='tsf']/div[2]/div[3]/center/input[1]")).isDisplayed();
System.out.print("Is 'Google Serach' button displayed? :");
System.out.println(buttonVisibility);
WebElement textBox = driver.findElement(By.xpath(".//*[@id='sb_ifc0']"));
textBox.click();
textBox.sendKeys("testing");
WebElement search = driver.findElement(By.xpath(".//*[@id='sblsbb']/button"));
search.click();
WebElement settings = driver.findElement(By.xpath(".//*[@id='abar_button_opt']"));
settings.click();
WebElement languages = driver.findElement(By.xpath(".//*[@id='ab_options']/ul/li[2]/a"));
languages.click();
boolean radioButtonSelected = driver.findElement(By.xpath(".//*[@id='langtpl']/div/span")).isSelected();
System.out.print("Is Radio button for 'Polski' selected? :");
System.out.println(radioButtonSelected);
boolean saveButtonEnabled = driver.findElement(By.xpath(".//*[@id='form-buttons']/div[1]")).isEnabled();
System.out.print("Is 'Save' button enabled? :");
System.out.println(saveButtonEnabled);
}
}
0 Comment(s)