Quantcast
Channel: Pochampalli IT Labs
Viewing all articles
Browse latest Browse all 261

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

$
0
0
Hi, In this post, we will see the differences between WebElement interface most commonly used methods - isDisplayed(), isEnabled() and isSelected() with real time example scenarios.

IsEnabled() has some special purpose of verifying elements such as buttons enabled or disabled status. Let's look into each of them.

Image and test site courtesy : https://www.testandquiz.com/selenium/testing.html

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.
  • This method can be applicable for almost all of the elements on web-page.
Example:  
HTML for : Sample
<bxpath="1">This is sample text.</b>

Selenium snippet
//isDisplayed() | Text on web page | Example : This is sample text.
WebElement ThisIsSimpleText
= driver.findElement(By.xpath("//b[contains(text(),'This is sample text.')]"));
boolean b1 = ThisIsSimpleText.isDisplayed();
System
.out.println("Verify dispaly status of the text \"This is sample Text\"="+b1);

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. 
Example :
Please find this blog post for a real time login use case example in which you will see "Sign Up" button is enabled after selecting a check box of terms and conditions.
    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
    Example:
    HTML
    <inputid="male"type="radio"name="gender"value="male"xpath="1">
      Selenium snippet
      //isSelected()  | Example :  male radio button
      WebElement maleRaditoButton
      = driver.findElement(By.xpath("//input[@id='male']"));
      boolean b2 = maleRaditoButton.isSelected(); //false
      System
      .out.println("Verify male radio button selected or not before click = "+b2);
      //select the male radio button and verify isSelected()
      maleRaditoButton
      .click();
      boolean b3 = maleRaditoButton.isSelected(); //true
      System
      .out.println("Verify male radio button selected or not after click = "+b3);

      Checkout this video tutorial to understand the example below

      WebElementInterfaceMethods_isDisplayedisSelectedDemo.java
      package selenium.webelement.methods;

      /* isDisplayed() , and isSelected() */

      importjava.util.concurrent.TimeUnit;

      importorg.openqa.selenium.By;
      importorg.openqa.selenium.PageLoadStrategy;
      importorg.openqa.selenium.WebDriver;
      importorg.openqa.selenium.WebElement;
      importorg.openqa.selenium.chrome.ChromeDriver;
      importorg.openqa.selenium.chrome.ChromeDriverService;
      importorg.openqa.selenium.chrome.ChromeOptions;

      publicclassWebElementInterfaceMethods_isDisplayedisSelectedDemo{

      publicstaticvoidmain(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://www.testandquiz.com/selenium/testing.html");

      //maximize the browser
      driver
      .manage().window().maximize();

      //isDisplayed() | Text on web page | Example : This is sample text.
      WebElement ThisIsSimpleText
      = driver.findElement(By.xpath("//b[contains(text(),'This is sample text.')]"));
      boolean b1 = ThisIsSimpleText.isDisplayed();
      System
      .out.println("Verify dispaly status of the text \"This is sample Text\"="+b1);

      //isSelected() | Example : male radio button
      WebElement maleRaditoButton
      = driver.findElement(By.xpath("//input[@id='male']"));
      boolean b2 = maleRaditoButton.isSelected();
      System
      .out.println("Verify male radio button selected or not before click = "+b2);

      //select the male radio button and verify isSelected()
      maleRaditoButton
      .click();
      boolean b3 = maleRaditoButton.isSelected();
      System
      .out.println("Verify male radio button selected or not after click = "+b3);


      //close the browser
      driver
      .close();

      //close all the browsers opened by WebDriver during execution and quit the session
      driver
      .quit();
      }
      }



      Viewing all articles
      Browse latest Browse all 261

      Trending Articles



      <script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>