Wednesday, 27 May 2020

WebElement interface methods examples in selenium - part 3 | Understanding of method isEnabled()

Hi, in this post, we will walk through the usage and live example of isEnabled() method. isEnabled() method, we apply in special scenarios where in we select a check box and get the status of a button i.e., enabled or disabled. Let's take a look into the following example.

isEnabled()
  • To verify if an element is enabled or disabled on web-page. 
  • Returns "ture" if element is enabled and returns "false" if an element is disabled. 
  • Examples: Mostly used with button elements, locked/disabled text input elements.
Problem Statement : 
Display the status of "Sign up" button - The below two images gives an idea on how the button looks before enabled and after enabled after selecting a check box.

Sign up  button before enabled i.e., before selecting check box "I have read...." 
Tap on the image to get better visibility:
Sign up  button after enabled i.e., after selecting check box "I have read...." 
Tap on the image to get better visibility:
The below piece of selenium script does
    a) verifies the status of "Sign up" button before enabled.
    b) selects the check box "I have read..." and
    c)  verifies the status of "Sign up" button after selecting the  check box.

//isEnabled() example - before selecting the check box
//display the enable or disable status of "Sign Up" button before selecting the check box
WebElement signUp = driver.findElement(By.xpath("//button[@id='signup']"));
boolean b1 = signUp.isEnabled();//false
System.out.println("Sign Up button enable/disable before selecting \"I have read and accpet...\" check box = "+b1);
   
    
//select the "I have read and accept check box
WebElement element=driver.findElement(By.xpath("//input[@id='termsAndPrivacy']"));
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOf(element));
new Actions(driver).moveToElement(element, 1, 1).click().perform();

//isEnabled() example - check box
//display the enable or disable status of "Sign Up" button after selecting the check box
boolean b2 = signUp.isEnabled();//true
System.out.println("Sign Up button enable/disable after selecting \"I have read and accpet...\" check box = "+b2);

Watch this ~ 6 min video tutorial for the demo : 


WebElementInterfaceMethod_isEnabledDemo.java
package selenium.webelement.methods;

/* button isEnabled() demo */

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WebElementInterfaceMethod_isEnabledDemo {

 public static void main(String[] args)  {
  
  WebDriver driver;
  
  //loading Chrome driver from physical drive
  System.setProperty("webdriver.chrome.driver", "D:\\006_trainings\\chromedriver.exe");
  
  System.setProperty("webdriver.chrome.silentOutput", "true");
  //System.setProperty(ChromeDriverService.CHROME_DRIVER_SILENT_OUTPUT_PROPERTY, "true");
  
  //launch the browser
  driver = new ChromeDriver();
  
  //navigate to site
  driver.navigate().to("https://us.megabus.com/account-management/login");
  
  //maximize the browser
  driver.manage().window().maximize();
  
  //close the message on site
  driver.findElement(By.xpath("//i[@class='close mb-close']")).click();
  
  // Sign Up form 
  WebElement signUpFromTab = driver.findElement(By.xpath("//a[@class='btn btn-link btn-block'][contains(text(),'Sign up')]"));
  signUpFromTab.click();
  
  //Email
  WebElement email = driver.findElement(By.xpath("//input[@id='email']"));
  email.sendKeys("java.selenium2023@gmail.com");

  //Confirm Email
  WebElement confirmEmail = driver.findElement(By.xpath("//input[@id='confirmEmail']"));
  confirmEmail.sendKeys("java.selenium2023@gmail.com");
  
  //Choose a Password
  WebElement chooseAPassword = driver.findElement(By.xpath("//input[@id='choosePassword']"));
  chooseAPassword.sendKeys("JavaSelenium2023");
  
  //Confirm Password
  WebElement confirmPassword = driver.findElement(By.xpath("//input[@id='confirmPassword']"));
  confirmPassword.sendKeys("JavaSelenium2023");

  //isEnabled() example - before selecting the check box
  //display the enable or disable status of "Sign Up" button before selecting the check box
  WebElement signUp = driver.findElement(By.xpath("//button[@id='signup']"));
  boolean b1 = signUp.isEnabled();
  System.out.println("Sign Up button enable/disable before selecting \"I have read and accpet...\" check box = "+b1);
   
    
  //select the "I have read and accept check box
  WebElement element=driver.findElement(By.xpath("//input[@id='termsAndPrivacy']"));
  WebDriverWait wait = new WebDriverWait(driver, 60);
  wait.until(ExpectedConditions.visibilityOf(element));
  new Actions(driver).moveToElement(element, 1, 1).click().perform();

  //isEnabled() example - check box
  //display the enable or disable status of "Sign Up" button after selecting the check box
  boolean b2 = signUp.isEnabled();
  System.out.println("Sign Up button enable/disable after selecting \"I have read and accpet...\" check box = "+b2);
  
  signUp.click();
  
  //close the browser
  //driver.close();
  
  //close all the browsers opened by WebDriver during execution and quit the session
  //driver.quit();
 }
}


To know more about isDisplayed() or isSelected() methods, please visit this post @

WebElement interface methods examples in selenium - part 2 | Understanding of methods isDisplayed() Vs isEnabled() Vs isSelected()


isDisplayed()
  • To verify presence of a web-element with in the web page. 
  • This method returns either true or false boolean values. 
  • If the element is present it returns "true" otherwise it returns "false". 
  • This method avoids the problem of having to parse an element's "style" attribute.
  • Example : This method can be applicable for all the elements on web-page. 
 isSelected()
  • Returns whether an element say check box or radio button is selected or not. 
  • If selected returns "true", if not selected returns "false". 
  • Examples: Check boxes, drop downs , radio buttons 
I hope this helps someone in the community, stay tuned for more automation.

No comments:

Post a Comment