Java Tutorial for Selenium WebDriver

Rating: 4
Views: 46
by Madhuri Yerukala
Last modified: December 2nd 2018

The following blog on Java tutorial for Selenium WebDriver makes you to learn basic Java concepts needed to write a test case in Selenium WebDriver.

The basic Java concepts are as follows, 

Table of Contents

  • Classes and Objects
  • Data types and Variables
  • Operators
  • Decision Making
  • Arrays
  • Loops
  • Constructors
  • String class

Classes and Objects

Classes and Objects are the basic and important concepts in Java programming.  It becomes easy to understand these concepts once they are visualized and related to real life. 

What is an Object?

In real world, an object is defined as a thing which we can see, touch and feel. It has both state and behavior. For example, dog is an object which has state (breed, color, age and size) and behavior (eat, run, sleep etc.).

Similarly in an object-oriented language like Java, object is called as a class instance which performs a group of activities. It implements its state in the form of variables and behavior in the form of methods.  From the above example, we can define an object (dog) as a collection of variables (breed, color, age and size) and methods (eat, run and sleep).

What is a class?

A class can be defined as guide to create objects. Objects created from a single class always share a similar state and behavior.  The major difference between an object and a class is that an object is created during the run time whereas a class is created during the program. 

Creation of Class

A class can be created using the keyword class followed by a name. The body of the class is delimited by curly brackets ({ }). A class can have one variable or a group of variables. 

  • Declaring a class
  • Declaring class member variables
  • Initializing class object
  • Methods of a class

Data types and variables

Knowledge on data types and variables helps to easily understand programming in Java for Selenium. 

What is a data type?

A data type is an indicator for the type of data that a variable holds. There are data types such as boolean, integer, character, double, floating-point, alphanumeric, short, and long. But in Java tutorial for Selenium, we use only basic data types such as integer, boolean, character and double. 

What is a variable?

A variable is something that varies or changes. A simple program can be written using data and instructions. Data is a fixed or a constant value that does not change during the execution of a program.  A programmer can use variables rather than giving the data directly and during the compile time, the variables are replaced with the data entered by the programmer. 

Technically, a variable can be defined as a memory location or space reserved to store information of different data types such as integer, character, float, boolean and double. Each variable has a different memory allocation based on the data type. For example, a variable of integer data type occupies four bytes of memory whereas a character occupies only two bytes of memory. 

Variable declaration

A variable declaration can be done using the syntax – 

Datatype VariableName;

When a variable is declared, the compiler allocates some space or memory depending upon the data type and fetches the stored value from the memory location using the variable name. A variable name can be of your choice but it must be simple and easy to use. The given variable name is called as an Identifier of that variable.

Variable initialization

The syntax for variable initialization is as follows, 

VariableName = Value;

Once a variable is declared, you can store a value before it is used for any kind of operations. An assignment operator ‘=’ symbol is used for initializing a variable.

Examples of different data types

The following are some examples of different data types which are used in Java for Selenium.


Only 1-bit of memory is allocated for boolean and can store only boolean values such as “true” and “false”.  A boolean variable can be declared using the keyword boolean as shown below,

boolean VariableName;


You can store only numeric values or number using the integer data type. A 32-bit of memory is allocated for integer and can store the values ranging from -2,147,483,648 to 2,147,483,648. A decimal number cannot be store in a variable of an integer data type. An integer variable can be declared using the keyword int as shown below,

int VariableName;


You can store decimal numbers using the double data type. A 64-bit of memory is allocated for double and a double variable can be declared using the keyword double as shown below,

double VariableName;


Alphabets and special characters are stored using the character data type. A 16-bit of memory is allocated for character and a character variable can be declared using the keyword char as shown below, 

char VariableName;

Note: single quotation marks (‘ ‘) must be used to initialize a character variable. 

The following are some important rules to be followed while naming a variable,

  • Alphabets (A-Z and a-z) are used for naming a variable.
  • Special characters such as “_” (underscore) and “$” (dollar sign) are allowed.
  • Variable name cannot be started with a number. But, it can include numerical digits.
  • A variable name must not be one of the pre-defined keywords such as static, void, main, this etc.


Operators are defined as special symbols which are used to perform specific operations such as arithmetic operations, logical operations and so on. These operations can be performed on one, two or three operands (participants in an operation). The operators are used to manipulate primitive data types (int, char, double and boolean). The general expression of operators is – 

Operand1 operator oeprand2 operator operand3……….so on

Based upon the operands, the operators are classified into –

Unary:  Unary operators take only one operand. These operators appear as either pre-fix or post-fix to the operands.
Binary:  Binary operators take two operands. These operators appear between the operands.
Ternary:  Ternary operators take three operands and they also appear between the operands. 

Types of operators

  • Assignment
  • Arithmetic
  • Relational
  • Logical and 
  • Conditional 

Assignment operator

Assignment operator is the most commonly used operator and it is denoted by the symbol “=”. It is used to assign the value on right to the variable on left. The assignment operator has the following syntax, 

variable = value or expression;

Arithmetic operators

Arithmetic operators are used to performing basic arithmetic operations such as addition, subtraction, multiplication, division etc. There are eight arithmetic operators in Java. The following table shows the details of each arithmetic operator. We have taken variables ‘a’, ‘b’ and ‘c’ for reference.

Operation Symbol Purpose Syntax
Addition + adds two numbers or concatenate two strings a = b + c;
Subtraction - subtracts right side operand (c) from left side operand (b) a = b – c;
Multiplication * multiplies two numbers a = b * c;
Division / divides left side operand by right side operand and returns quotient a = b / c;
Modulus % divides left side operand by right side operand and returns remainder a = b % c;
Increment ++ increases the value by 1 b ++;
Decrement -- decreases the values by 1 c --;
Negation - Unary operator that returns negative value -b;

Relational operators

Relational operators are used to comparing to operands or objects. These operators return boolean values of either true or false when used in an expression. There are six relational operators in Java. The following table shows the details of each relational operator. We have taken variables ‘a’ and ‘b’ for reference.