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, being developed 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
  • Automation script to login into Facebook

Unit Testing for Text Boxes

Unit testing is the method of verifying smallest piece of testable code  against its purpose. 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 write test case for a text box. The next tutorial will cover check box and radio button unit testing.

For a text box, we can conduct the testing by entering correct and incorrect values.

Test case for Facebook Login Page

We are going to unit test the login screen of Facebook, which comprises of text boxes and buttons.

Facebook Login

Interacting with Text Boxes

In Selenium 3.0, we can use SendKeys(Text) to enter text in the text box. Prior to that, we need to locate the element using locators.

If the name of the text box is “LoginTxt”, the piece of code to write text in this text box will be as shown below:

WebElement userid = driver.findElement(By.id(“Logintxt”));
userid.sendKeys(“abc@gmail.com”);

Refer:¬ Understanding and using locators

Interacting with Button

Once we enter the values in the text boxes, we need to also submit the values to simulate the login process. We need to also automate the clicking process of a button. We can use the following method:

WebElement login = driver.findElement(By.id(“SubmitButton”));
login.click();

Automating the Login process in Facebook

The login text box name on the Facebook login page is named as “fb”. The password has a name “Passwd”. The button name is “fblogin”.

Note: In case the names of these elements have changed, please replace them in the code appropriately.¬

We are going to add an additional line of code in our script. This wait statement is entered to make sure that¬†Selenium does not throw¬†“ElementNotVisibleException“.

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 org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.List;
import java.util.concurrent.*;

public class Loginfun {

public static void main(String[] args) {

//initialize Chrome driver


System.setProperty(“webdriver.chrome.driver”, “E:\\Selenium 3\\chromedriver.exe”);

//defining driver

WebDriver driver


driver= new ChromeDriver();

//Open Facebook
driver.get(“http://www.facebook.com”);

// Enter email or phone
WebElement userid = driver.findElement(By.id(“fb”));
userid.sendKeys(“abc@gmail.com”);

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

//Enter Password
WebElement password = driver.findElement(By.id(“Passwd”));
password.sendKeys(“Password”);

/Click on login button (for login into facebook)
WebElement login = driver.findElement(By.id(“fblogin”));
login.click();

¬ }

}

 

You can use your correct Facebook credentials to try if the code works.

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?

The code for the same is given below

Editable Text Box

 

About Techcanvass

Techcanvass is a software training organization and offers certification courses in Business Analysis and automation testing. Our courses in selenium are:

Selenium Certification (CP-SAT) Training

Selenium Certification Course

 

 

 

 

 

Selenium Training with Java

Selenium Training Course

Leave a Reply

Your email address will not be published. Required fields are marked *