Thursday 21 May 2020

WebElement interface methods examples in selenium - part 1 | Understanding of methods getText(), sendKeys(), clear(), getAttribute() and click() for radio button and check box

Hi , In this post - I'm writing my learning experiences on various methods of "WebElement" interface.
Test site courtesy : 

Watch the below ~1 min video for end-to-end execution 
getText()
  • getText() method gets you the visible text or inner text of an element. 
  • That is it fetches text between the opening and closing tags. 
  • For instance, the following text between bold tags can be fetched using getText() method.
    <b xpath="1">This is sample text.</b>
  • Store the locator in a Weblement variable and then use getText()
    //getText() example --> locate text "This is sample text." on page and display the text
    WebElement ThisIsSimpleText = driver.findElement(By.xpath("//b[contains(text(),'This is sample text.')]"));
    String displayThisIsSimpleText = ThisIsSimpleText.getText();
    System.out.println(displayThisIsSimpleText);
sendKeys()
  • This method allows users to enter data into editable elements such as text boxes or search boxes.
  • For instance, first name is a text box input and using sendKeys() method enter input value
    <input id="fname" type="text" name="firstName" xpath="1">
  • Code snippet
    //sendKeys("sadakar") example --> locate TextBox input and enter first name
    WebElement firstName = driver.findElement(By.xpath("//input[@id='fname']"));
    firstName.sendKeys("Sadakar");
    
clear()
  • clear() method clears any text already entered or displayed in text box or search box inputs. 
  • Some applications may use default values in editable forms so clear them before entering new.
  • For instance, clear the already existing text in "first name" text box and enter new value
    //clear() --> clear TextBox input and re-enter first name
    firstName.clear();
    firstName.sendKeys("Hasini");
    
getAttribute("<attribute>")
  • getAttribute() method returns the current value of a given attribute as a string.
    <input id="fname" type="text" name="firstName" xpath="1">
    
  • id, type, name , xpath and vlaue(not given above statement) are attributes of first name element HTML 
  • For instance, the following snippet for firstname text field fetches the value displayed on the form then the values of the corresponding attributes
    //getAttribute("<attribute>") --> <input id="fname" type="text" name="firstName">
    firstName.getAttribute("value");
    System.out.println("<input id=\"fname\" type=\"text\" name=\"firstName\">");
    System.out.println("Value of value attribute="+firstName.getAttribute("value"));
    System.out.println("Value of id attribute="+firstName.getAttribute("id"));
    System.out.println("Value of type attribute="+firstName.getAttribute("type"));
    System.out.println("Value of name attribute="+firstName.getAttribute("name"));
    
Handling radio buttons and check boxes using - click() method
  • Toggling(select or deselect) on/off are the actions performed for radio buttons or check boxes. 
  • Using click() method toggling can be done. 
  • For instance, take a look at the following code snippet - "male" radio button and check boxes for testings labels are selected.
    //radio button click example --> locate "male" radio button and select it
    WebElement male = driver.findElement(By.xpath("//input[@id='male']"));
    male.click();
      
    //check box click example - locate "Automation Testing" check box and tick it
    WebElement automationTesting = driver.findElement(By.xpath("//input[@class='Automation']"));
    automationTesting.click();
    
    //check box click example - locate "Performance Testing" check box and tick it  
    WebElement performanceTesting = driver.findElement(By.xpath("//input[@class='Performance']"));
    performanceTesting.click();
    

Complete java class for above examples: WebElementInterfaceMethodsDemo.java
package selenium.webelement.methods;

import java.util.concurrent.TimeUnit;

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

public class WebElementInterfaceMethodsDemo {

 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://www.testandquiz.com/selenium/testing.html");
  
  //maximize the browser
  driver.manage().window().maximize();
  
  //getText() example --> locate text "This is sample text." on page and display the text
  WebElement ThisIsSimpleText = driver.findElement(By.xpath("//b[contains(text(),'This is sample text.')]"));
  String displayThisIsSimpleText = ThisIsSimpleText.getText();
  System.out.println(displayThisIsSimpleText);
  
  //sendKeys("sadakar") example --> locate TextBox input and enter first name
  WebElement firstName = driver.findElement(By.xpath("//input[@id='fname']"));
  firstName.sendKeys("Sadakar");
  
  //clear() --> clear TextBox input and re-enter first name
  firstName.clear();
  firstName.sendKeys("Hasini");
  
  //getAttribute("<attribute>") --> <input id="fname" type="text" name="firstName">
  firstName.getAttribute("value");
  System.out.println("<input id=\"fname\" type=\"text\" name=\"firstName\">");
  System.out.println("Value of value attribute="+firstName.getAttribute("value"));
  System.out.println("Value of id attribute="+firstName.getAttribute("id"));
  System.out.println("Value of type attribute="+firstName.getAttribute("type"));
  System.out.println("Value of name attribute="+firstName.getAttribute("name"));
  
  //radio button click example --> locate "male" radio button and select it
  WebElement male = driver.findElement(By.xpath("//input[@id='male']"));
  male.click();
  
  //check box click example - locate "Automation Testing" check box and tick it
  WebElement automationTesting = driver.findElement(By.xpath("//input[@class='Automation']"));
  automationTesting.click();
  
  WebElement performanceTesting = driver.findElement(By.xpath("//input[@class='Performance']"));
  performanceTesting.click();
  
  //close the browser
  driver.close();
  
  //close all the browsers opened by WebDriver during execution and quit the session
  driver.quit();
 }
}

No comments:

Post a Comment