Handling textBoxes in selenium

TextBox is an element where we can enter/input alphanumeric text.
To identify the textBoxes we need to look for the input tag with type=text, [below screenshot]
 Let's see some of the actions we can perform with textboxes in selenium:


package controls;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TextBoxes {
 public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  String UserName = "John K";
  driver.get("http://www.qavalidation.com/p/demo.html");
  WebElement txt_Uname = driver.findElement(By.id("username"));
  txt_Uname.sendKeys(UserName); //sendKeys method to send text to textBox
  //Get the entered text using getAttribute("value")
  System.out.println("UserName Entered: "+txt_Uname.getAttribute("value"));
  //compare if the string entered is correct or not
  if(UserName.equals(txt_Uname.getAttribute("value")))
   System.out.println("TestCase Pass");
  
  //To select or highlight the text entered
  //also shows combination of keys and characters
  txt_Uname.sendKeys(Keys.CONTROL,"a");
  //txt_Uname.clear();
  //System.out.println("UserName Entered:"+txt_Uname.getAttribute("value"));
 }
}

No comments:

Post a Comment