Java Tutorial for Selenium

Java Tutorial for Selenium
Overview of 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 object.

> Java is platform independent language. It can be compiled and interpreted.

> Java is simple, easy to learn and use.

> Java is securable, using Java we can develop virus free and tamper free systems.
Usage of Java:
> To develop Desktop Applications (Ex: Acrobat Reader)

> To develop Web Applications

> To develop enterprise Applications (Ex: Banking, Insurance etc...)

> To develop Mobile Applications

> To develop Embedded Systems.

> To develop Smart cards

> To develop Games software

* Test Automation (To Enhance Test cases)
A) Java Fundamentals
1) Comments
Purpose of Comments:

i) To make the code readable.

ii) To make the code disable from execution.

Note: Java supports single line comment and multiple line comments.
2) Data Types
There are two categories of Data Types.

a) Primitive Data Types

b) Reference Data Types.
3) Java Modifiers
Modifiers are used to set access levels for Classes, variables, Methods etc...

There are two categories of Modifiers

i) Access Modifiers (default, public, private, protected)

ii) Non-Access Modifiers (static, final, abstract, synchronized)
4) Variables
There are three types of variables

i) Local variables

ii) Static / Class variables

iii) Instance variables
5) Operators in Java
Operators are used to perform mathematical, comparison and Logical operations.

Categories of Operators

i) Arithmetic

ii) Relational

iii) Bitwise

iv) Logical

v) Assignment

vi) Mise operators
6) Conditional statements
In Selenium Test Automation, we use java conditional statements to insert verification points and for error handling.

i) Two types of conditional statements in Java
a) if statement

b) switch statement

ii) Types of Conditions
a) Single condition (Positive/Negative)

b) Compound condition

c) Nested Condition

iii) Usage of Conditional statements
a) Execute 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) Execute a block of statements when more than one condition is True(Nested if)

e) Decide among several alternates (Using 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 data 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.
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

Types of Methods

i) Built-in/Pre-defined methods
    Number methods
    Character methods
    String methods
    Date & Time methods etc...

ii) User defined methods

    Method with returning value
    Method without returning any value.
10) File and I/O operations in Java package contains classes to perform input and output operations in Java.

> Reading input using Input devices

> Displaying output on the Console.

> Handling Files
11) Exception handling in Java.
> In Computer programming exception is an abnormal condition.

The exception handling in Java to handle run-time errors so that normal flow of the application can be maintained.
B) Java Object Oriented Programming
In Object Oriented programming System (OOPS), we design programs using objects and classes.

1) Inheritance
> Inheritance is a mechanism in which one object acquires all 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, we can reuse methods and fields from parent class

Java Supports

i) Single Inheritance

ii) Multi level Inheritance

Note: Java doesn't support multiple Inheritance.
2) Polymorphism
It is a concept by which we can perform an operation in different ways.

Polymorphism derived from two Greek words

Poly - many

Morphs - forms/ways

Polymorphism means many forms/ways.

Two types of Polymorphism in java.

i) Compile time Polymorphism

ii) Runtime Polymorphism

we can achieve Polymorphism by Method loading and Method overriding.
3) Abstraction
> Abstraction is 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.

In two ways we can achieve Abstraction.

i) Abstract class

ii) Interface.
4) Encapsulation
Encapsulation is a process of wrapping code and data together into a single unit.
Java Packages
A Java package is a group of similar type of classes, Interfaces and sub-packages.

There are two types of Java packages

i) Built in packages

Ex: io, util, sql etc...

ii) User defined packages

Java Interface
An Interface is a collection of abstract methods.

An Interface is not a Class, writing an Interface is similar to writing a Class.
Java Environment Setup and write first Java Program
> Download Java (JDK) software and Install.

> Set Environment Variable (Path variable) to access Java form any directory.

How to set path variable in MS Windows 7.

Select MyComputer and right click

> Properties

> Advanced System Settings

> Environment Variables

> Select System variables.

> Select Path variable

> Enter JDK bin directory path.

>OK > OK > OK
Verify the Java Environment setup

Launch command prompt

> Type java (it provides details)
Three steps for writing and executing Java programs.
i) Write Java program in Notepad (Editor)

ii) Compile the Program

Command prompt > change to Program file directory)

> Type javac (java is program file extension, javac is Java command for compilation)

iii) Run / Execute the Program

> Command prompt > Type java Program Name

Sample Java Program

package JavaExamples;

public class Sample {

    public static void main(String[] args) {
        System.out.println("Hello Java");
Download Eclipse software and unzip

Eclipse IDE (Integrated Development Environment)

It is platform to create and execute Java programs, Perl, Python, Ruby, PHP etc... programs.

It provides Editor, help for syntax errors, Auto compilation and debugging.

Open Eclipse main folder

> Click Eclipse file (It launched eclipse IDE)
Create Java Project
    > Create a package
        > Create class /Interface

Java program Structure / Java Program Syntax

1) Package declaration statement
package JavaExamples;

2) Import statements
We can import Built in and User defined Libraries using "import" keyword.



java - Project

io - package

Console - Class

io.* - all classes from io package.
import java.lang.*;
3) Class declaration statement

public class Sample {

public - Access modifier

class - Java keyword to declare a class

Sample - Name of the class
4) Comments sections
// This is a sample Program

5) main Method - Program execution starts from main method
(* It is the mandatory statement in every java program.)

public static void main(String[] args) {

public - Access Modifier

static - Non Access Modifier (use main method without invoking any object)

void - returns nothing

main - Method name
6) Declarations
we can declare Variables and constants


int a = 10;

int a =10, b =20, c =30;


int a;
a =100;

final int x =100; // Constant declaration
System.out.println("Hello Java");

System - Class

out - object

println - Method

"hello Java" -message
7) Conditional blocks
Loop blocks

Method declarations

A Sample Java Program:

package JavaExamples;

import java.lang.*;

public class Sample {
    // This is a Sample Program

    public static void main(String[] args) {
        int a = 100, b, c; //Variables Declaration
        b = 20;
        final int Money = 100; // Constant declaration
        System.out.println("Addition of a, b is: " + (a+b));
        if ( a > b){
            System.out.println("A is a Big Number");
        else {
            System.out.println("B is a Big Number");
        for (c =1; c <=10; c++){


Post a Comment