4. Java for Selenium

Overview of Java Programming

Java is a Programming Language and a Platform

  • Java is used as a programming language to develop Software Applications
  • Java is used as Software Platform to run Java Applications

Java has three important editions

1) Java Standard Edition / Core Java (Old name J2SE)
2) Java Enterprise Edition / Advanced Java (Old name J2EE)
3) Java Micro Edition (Old name J2ME)

Java Standard Edition or Core Java is enough for Automated Testing using Selenium.

Note 1: In Computer Programming 80% concepts are common (in every programming language), Syntax may vary from one language to another

Example: Data Types, Variables, Operators, Control Flow (Conditional, Loop and Branching) etc…

Note 2: Some features may vary from one language to another,

Example: Functions in C Languages, Methods in Java….

Overview of Java Standard Edition / Core Java

1) Comments in Java

2) Java Data Types

3) Java Modifiers

4) Java Variables

5) Operators in Java

6) Java Control Flow Statements
(Conditional, Loop and Branching Statements)

7) String handling in Java

8) Java Arrays, ArrayList

9) Java IO Operations (includes File Handling)

10) Java Methods
(Built-in Methods and User defined Methods)

11) Exception Handling in Java

12) Java Object Oriented Programming (OOPS).
(Inheritance, Polymorphism, Abstraction and Encapsulation)

1) Comments in Java

  • To make the code readable
  • To make the code disable from execution

Java supports Single line and multiple line comments

Note: We use comments in Selenium Test Scripts for readability

2) Java Data Types

  • A Data type is a classification of the type of data that a variable or object can hold in Computer programming

Float, Double….
Boolean etc…

Java supports two categories of Data Types,

i) Primitive Data Types
ii) Non Primitive Data Types / Reference Data Types

Note: We use Java Data Types to handle different type of data in Selenium Test cases.

3) Java Modifiers

Modifiers are used to set access levels for classes, variables and methods,

Java supports two categories of Modifies

i) Access Modifiers (default, public, private, and protected)
ii) Non Access Modifiers (static, final and abstract)

Note: We use “public” access modifier, static modifier for creating static methods and static variables, final modifier for finalizing variables.

4) Java Variables

A named memory location to store or hold temporary data within a program

ROM – HDD, CD, DVD, USB Drive etc…)

Java Supports three types of variables,

i) Local Variables
ii) Instance Variables
iii) Static Variables/Class Variables

Note: Java supports explicit declaration of variables

In Java:
int a;
b=200; //Incorrect
int c=a+b; //Incorrect

int a;
int a, b, c;
int d=100;
int e=200, f=300, g=400;

In VBScript:
Dim a
a=100 ‘Correct (Explicit variable)
b=200 ‘Correct (Implicit variable)
Msgbox a+b ‘300

Note: VBScript supports Implicit and Explicit declaration of variables

5) Operators in Java

Operators are used to perform Mathematical, Comparison and Logical operations

Categories of Operators in Java,

i) Arithmetic Operators
ii) Assignment Operators
iii) Relational / Comparison Operators
iv) Logical Operators
v) Bitwise Operators
vi) Miscellaneous Operators

6) Java Control Flow Statements

i) Conditional / Decision Making statements (if, switch,)
ii) Loop Statements (for, while, do while and enhanced for)
iii) Branching statements (break, continue and return)

a) Java Conditional Statements / Decision Making Statements

i) Two Types of Conditional Statements

1) if statement
2) switch statement

ii) Three types of Conditions

1) Single Condition (Positive / Negative Condition)
if (a>b){
if (b<a){

2) Compound / Multi Condition

if ((a>b) && (a>c) && (a>d)){

3) Nested Condition

if (a>b){
if (a>c){
if (a>d){


iii) Usage of Conditional statements

1) Execute a block of statements when a condition is true
2) Execute a block of statements when a condition is true otherwise execute another block of statements
3) Execute a block of statements when a compound condition is true otherwise execute another block of statements
4) Decide among several alternates (else if)
5) Execute a block of statements when more than one condition is true (nested if)
6) Decide among several alternates (using switch statement)

b) Loop Statements

Used for repetitive execution

i) for loop
ii) while loop
iii) do while loop
iv) enhanced for loop (Array)

c) Branching Statements

  • Branching statements are used to transfer control from one point to another in the code
  • Branching statements are defined by three keywords – break, continue and return

7) String handling in Java

  • Sequence of characters written in “”
  • String may contain Alphabets, Numeric, Alpha-numeric and special characters.


123 //Integer

Note: Using String predefined methods we can perform operations on strings.

8) Java Arrays

  • Generally, Array is a collection of similar type of elements
  • In Java Array is an Object that contains elements of similar data types
  • Java Array is index bases and index starts from zero
  • The length of an Array is established when they are is created and Array length is fixed,
  • Each item in an Array is called as Element

9) Java IO Operations (includes File Handling)

  • Read Data using Input devices
  • Display Output on the Console
  • Read data from files, Write data to files

10) Java Methods

What is Method?
A Set of statements to perform an Operation

Why Methods?
Methods are used for code reusability

When we choose methods?
Whenever we want to perform any operation multiple times then we choose methods

Note: All programming features can be used in methods also

Two Types of Methods in Java,

i) Built-in Methods

Number Methods
Character Methods
String Methods
Array Methods Etc…

ii) User Defined Methods

Method with returns a value (Static and Non Static methods)
Method with returns nothing (Static and Non Static methods)

java tutorial
Overview of Java Programming

11) Exception Handling in Java

  • In Computer programming exception is an abnormal condition,
  • An exception is an event that occurs during the execution of a program that disturbs the nor flow of instructions
  • The exception handling in Java is one of the powerful mechanisms to handle run-time errors

int a=10; //No Error
int b=10 //Syntax Error
int c= 10/2; //No Error
int d=10/0; //Run-time Error (* Exception handling is required)

12) Java Object Oriented Programming (OOPS)

i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation

i) Inheritance

  • Inheritance is a mechanism in which one object acquires all the properties and behaviors of parent object
  • Using Inheritance we can create classes that are built-in upon existing classes
  • When we inherit from an existing class then we can reuse methods and fields from the parent class

Java Supports,

a) Single Inheritance
ClassB extends ClassA

b) Mluti Level Inheritance
ClassB extends ClassA
ClassC extends ClassB

c) Multiple Inheritance (* Java doesn’t support multiple Inheritance)
ClassB extends ClassA
ClassB extends ClassZ

ii) Polymorphism

Performing task/s in different ways,
Polymorphism derived from two Greek words,

Poly – Many
Morphs – Forms / ways

So Polymorphism means “Many Ways”

Two types of Polymorphism in Java,

a) Compile Time Polymorphism / Method OverLoading
b) Run-time Polymorphism / Method Overriding

iii) Abstraction

  • Abstraction is a process of hiding implementation details and showing functionality to the user
  • In another way, it shows important things to the user and hides internal details Ex: Sending Email
  • Abstraction focuses on what the object does instead of how it does

Abstraction can be achieved in two ways,

1) Abstract Class
2) Interface

iv) Encapsulation

Encapsulation is a process of wrapping code and data together into a single unit

Ex: Capsule

It provides control over the data

Java Programming Hierarchy

Java Project > Java Packages > Java Classes / Java Interfaces

We write Java code in Java Class/Java Interface



Follow me on social media: