Java for Selenium

Java Language for Selenium

Why Java for Selenium?

> Selenium written in Java, it doesn't mean that Java only more compatible with selenium, we can use other supported languages also.

> Good support for Selenium with Java, You can get more help documents and implementations from Internet.

> Majority of Selenium Testers(nearly 77%) using Java, so knowledge sharing is very easy.

> Java is platform independent language, we can use it on any Operating environment.

How much Java is required for Selenium?
> Fore Test Automation using Selenium Core Java knowledge is sufficient, Advanced Java not required.

> Java Basics and Object Oriented Concepts are required.

We can segregate Java for Selenium into 2 categories.

A) Java Basics
B) Java Object Oriented Programming----------------------

1) Overview on Java
> Java Programming Language was developed by Sun Microsystems in 1995, Now it is subsidiary of Oracle corporation.

> Java is an Object Oriented programming Language, In Java, everything is an Object. Java can be easily extended since it is based on the Object model.

> Java is a Platform independent Language. It can be compiled and interpreted.

> Java is Simple, It is easy to learn and implement.

> java is Securable, using Java we can develop virus free and tamper free systems.

2) Java Environment Setup
We have to setup java environment to write and execute Java programs, It varies from one operating system to another.

> If it is DOS (from Command prompt in Windows), install java Software(JDK) and set Environment variables.

> If it is Windows GUI, install Java and Eclipse(Java Editor) to write and execute Java programs

3) Java Syntax
> Java is case sensitive language(deference is there in between upper and lower case letters)

Ex: Identifiers UFT and uft have different meaning in java

> First letter of a Class Name should be in upper case.

> Method names should start with lower case letter

> Java Program file name should exactly match with class name.

> public static void main (String args[])- Java program execution starts from main method, which is mandatory in every java program.

> Every statement should end with semi colon symbol.

4) Usage of Java 
> Developing Desktop Applications (Ex: Acrobat Reader)

> Developing Web Applications

> Developing Enterprise Applications (ex: banking, Insurance Applications)

> developing mobile applications

> Embedded systems

> smart cards

> Games etc...
A) java Basics

1) Comments in Java
Purpose of comments

    > To make the code readable
    > To make the Disable from execution

Java supports Single line comment and multiple lines comment.

2) Data Types in Java

There are two categories of Data types in Java

a) Primitive data types

b) Reference data types

3) Variables

a) Local variables

b) Instance variables

c) Class / Static variables

4) Java Modifiers

Modifiers are used to set access levels for Classes, variables, methods etc...

a) Access Modifiers (Default, Public, Private, Protected)

b) Non-access Modifiers (Static, Final, Abstract, Synchronized)

5) Operators in java

Operators are used to perform mathematical, comparison and logical operations.

Categories of operators:

a) Arithmetic

b) Relational operators

c) Bitwise operators

d) Logical operators

e) Assignment operators

f) Mise Operators

6) Conditional statements

Used to insert verification points and for error handling.

Types of conditional statements in Java:

a) If Statement

b) Switch statement

Types of Condition:

a) Single condition

b) Compound conditions

c) Nested conditions

Usage of conditional statements:

a) Executing a block of statements when condition is true.

b) Execute a block of statements when condition is true otherwise execute another block of statements.

c) Decide among several alternates (Else if)

d) Executing a block of statements when more than one condition is true.(Nested if)

e) Decide among several alternates (Switch statement)

7) Loop statements

used for repetitive execution.

Types of Loop Statements

a) For Loop

b) While Loop

c) Do...While Loop

d) Enhanced For Loop (mainly used for Arrays)

8) Arrays in Java
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 based, index starts from zero.

The length of an Array is established when the Array is created and Array length is fixed.

Each item in an Array is called an Element.

Declaration of Arrays

Assigning values to elements

Advantages and Disadvantages of Arrays.
9) Java Methods
Methods are also known as procedures or functions

A Java method is a collection of statements that are grouped together to perform an operation.



If you call the method, the system actually executes several statements, in order to display a message on the console.

We can create methods with or without returning any value.

Basically Methods are two types

a) Built-in or pre-defined
    Number Methods
    Character methods
    String methods
    Date & Time Methods

b) User defined
    Method with returning value
    Method without returning any value
10) Regular Expressions
It is a formula for matching patterns, A Regular expression defines search pattern for strings.

Java provides the java.util.regex package for pattern matching with regular expressions.
11) File and I/O operations in Java.
The package contains classes to perform input and output (I/O) operations in Java.

Reading using input devices

Displaying on the console

Reading and writing text files

Working with Excel files.
12) 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 disrupts the normal flow of instructions.

The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.
Java object oriented Programming
1) Class, Object, Method
In object oriented programming system(OOPS), we design a program using Objects and classes.


It is an entity that has states and behaviors is known as Object.

Ex: Pen, Chair, Table, Computer etc...

States / Properties of Dog object are color, height, weight etc... and behaviors moving, barking, jumping etc...

Class is a template or blueprint from with objects are created.

A Class a group of objects that has common properties.
Method: An operation on Object.
2) 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 upon existing classes.

When we Inherit from an existing class, we can reuse methods and fields from parent class and we can add new methods and fields also.

Java Supports:

a) Single Inheritance

b) Multi level Inheritance

c) Hierarchical Inheritance

Note: Java doesn't support Multiple Inheritance.
3) Method Overloading and Overriding-------------------------------------
If a class having multiple methods with same name, but different parameters is known as method overloading.

Method overloading increases the readability of the program.

There are two ways to overload the method in Java

a) by changing number of arguments
add (int a, int b)

add (int a, int b, int c)

b) by changing data types

add (int a, int b)
add (char a, char b)

In Object Oriented programming overriding means to override the functionality of an existing method.

Method overriding provides a specific implementation of a method that is already provided by its super class (Parent class)

It is used for runtime polymorphism.
4) Polymorphism
It is a concept by which we can perform a single in different ways.

Polymorphism derived from two Greek word poly and morphs

Poly means -many
morphs means -forms / ways, so polymorphism means many forms.

we have two types of Polymorphism in java

a) Compile time polymorphism
b) Runtime polymorphism.

We can perform polymorphism in java by method overloading and method overriding.
5) Abstraction
Abstraction is a process of hiding the implementation details and showing functionality to the user.

Another way, it shows important things to the user and hides internal details.

Ex: Sending email

you just type the email and send, you don't internal process of the email delivery.

Abstraction focuses on what the object does instead of how it does.

In two ways we can achieve Abstraction

a) Abstract Class (0 to 100%)

b) Interface (100%)
6) Encapsulation
Encapsulation is one of the four fundamental oops concepts, other three are Inheritance, Polymorphism and Abstraction.

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

Ex: Capsule (mixed of several medicines)

It provides the control over the data, suppose we want to value of the id greater than 100 only, we can write the logic inside the setter method.
7) Java packages
A Java package is a group of similar type of classes, interfaces and sub-packages.

There are two types of java packages

a) Built in packages

Ex: io, util, sql etc...

b) User defined packages

8) Interfaces in Java
An interface is a collection of abstract methods.

An interface is not a class, writing an interface is similar to writing a class, but two are different concepts.

Also Read:
Overview on Selenium

Selenium online Training


Post a Comment