Handling Text Boxes in Selenium 3

Writing Automation Script for Text Box in Selenium

Writing Automation Script for Text Boxes

In this tutorial, we are going to discuss – “Writing Automation Script for Text Box in Selenium“. This tutorial is part of complete Selenium 3.0 Tutorials, written by Techcanvass.

In this tutorial, we are going to cover the following topics:

  • How do we conduct Unit testing of Text boxes?
  • Text box interaction methods
  • Automating a negative test case to login into TechEdge LMS system by Techcanvass

Unit Testing for Text Boxes

Unit testing is the method of verifying smallest unit (e.g. a screen) in a software system. A user registration screen or a login screen is an example of a unit. A unit comprises of multiple elements e.g. text box, check box, drop down.

In this tutorial, we are going to learn -how to write test cases involving usage of text boxes. We have chosen the login screen of our TechEdge portal. In the next tutorial, you can learn writing selenium scripts for checkbox and radio button.

Test case for TechEdge Login Page

The login page for our TechEdge portal is as shown below

TechEdge Login page 2

Our negative test case is to enter wrong email ID and password for the login screen and click on the submit button, so that the login fails


Interacting with Text Boxes

In Selenium 3.0, we can use SendKeys(Text) to enter text in the text box.  To do so, we need to locate the element using locators. To find out the text box, we can Inspect the element as shown below.

Step 1: Right click on the username field and click on inspect.

Login Page Inspect

Step 2: Use context menu Copy to copy the Xpath of the username field.

getting xpath

so, the code for getting the reference of username field, the code would be as shown below

WebElement userid = driver.findElement(By.xpath(“//*[@id=’txtLoginid’]”));

And to enter the desired value into this, the code would be:


Refer: Understanding and using locators

Interacting with Button

We can use the following method to send “Click” action.

WebElement login = driver.findElement(By.xpath(“//*[@id=’btnLogin’]”));

Writing the negative test case

We are now going to write the code for a negative test case, where we will be entering wrong user id and password for the login screen.

All the elements of the web page may not load at the same time. Java script and Ajax scripts may take a little more time to load. To make sure that the element is found by Web Driver, we use waits.

//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

The complete code for our example is:

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TextBox {

public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub

System.setProperty(“webdriver.chrome.driver”, “C:\\SeleniumCode\\SeleniumConfig\\chromedriver.exe”);

//defining driver

WebDriver driver;

driver= new ChromeDriver();

//Open Facebook

// Enter email or phone
WebElement userid = driver.findElement(By.xpath(“//*[@id=’txtLoginid’]”));

//wait 5 secs for userid to be entered
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

//Enter Password
WebElement password = driver.findElement(By.xpath(“//*[@id=’txtpassword’]”));

//Click on login button (for login into facebook)
WebElement login = driver.findElement(By.xpath(“//*[@id=’btnLogin’]”));
//driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

if (driver != null)




Writing test cases for a text box

Few things that need to take under consideration while writing test cases for unit testing of Text boxes.

  • Editable or Disable functionality to verify whether we can enter the data or not.
  • Check for Value Acceptance Capability of the text box with different set of inputs like characters, numeric values, alphanumeric values, special characters etc.
  • Maximum input Length of Text box (Number of characters accepted).
  • Place of the Text Box on the screen (x, y coordinates).
  • Can you copy and paste inside text field? (not feasible for automation)
  • Length, Height, Width and Background color of the text box (Secondary level checks).

Checking if the text box is editable

How can we check whether email or phone text field is editable, if yes then try to enter string “Test” to check whether it accepts it or not? For this we can use isEnabled() method of the element referencing the text box.


About Techcanvass

Techcanvass offers IT certification courses for professionals. We are an IIBA endorsed education provider (EEP), iSQI ATP (for Certified Agile Business Analyst Training) as well as Agile Testing alliance partner for CP-SAT certification training in Selenium.

If you prefer to try out, here is an option to check out our Selenium certification course:

CP-SAT Certification Training

Selenium Courses with FREE Trial

You can join our Selenium Training in Mumbai (Classroom Format), Live Online Selenium Training or can even go for Selenium Self-learning course. Each of these courses are offered through TechEdge LMS portal, which includes:

a) Actual class recordings, divided into small chapters for easy learning

b) Chapter wise Quizzes

c) Chapter wise practice assignments

Want to try it for Free?

Selenium Training Free Trial