Java Quick Tutorial for Selenium

Java Quick Tutorial for Selenium

Java Environment Setup & Verify

A) Java Fundamentals
i) Comments in Java
ii) Java Data Types
iii) Java Modifiers
iv) Variables
v) Operators
vi) Conditional Statements
vii) Loop Statements
viii) String Handling in Java
ix) Arrays in Java
x) Java IO Operations and File Handling
xi) Java Built-in Methods
xii) Java User defined Methods
xiii) Java Exception Handling

B) Java OOPS (Object Oriented Programming System)
i) Inheritance
ii) Polymorphism
iii) Abstraction
iv) Encapsulation
———————————-
Java Environment Setup & Verify

Steps:
> Download Eclipse IDE and extract
> Download Java and Install
————————-
> Launch Eclipse IDE
> Create Java Project
> Create Java Package
> Create Java Class
Write Java Program and Execute
———————————-
i) Comments in Java

Purpose of Comments in Computer Programming

> To make the Code Readable
> To make the Code disable from Execution.

Java supports Single line comment and multiple line comments.
———————-
Single line Comment- // before the Statement

Multiple lines comment/comment a block of statements
/* Statements
—————
——————
————*/
———————-
Example:
public static void main(String[] args) {
//It is a Sample Java Program
int a=10, b =20;
System.out.println(a+b);

/*if ( a > b){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“B is a Big Number”);
}*/

}
}
———————————-
ii) Data Types in Java

A Data Type is a classification of the type of data that a variable or object can hold in computer programming.

Example for General Data Types:

Character,

Integer,

String

Float

Boolean etc…

Java supports two categories of Data types.

a) Primitive Data Types

b) Reference Data Types.

Example:

int a=10;
char b =’A’;
double c = 123.234;
boolean d = true;
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
———————————-
iii) Java Modifiers

Modifiers are used to set access levels for Classes, Variables and Methods etc…

Two types of Modifiers in Java

1) Access Modifiers (default, public, private and protected)

2) Non Access Modifiers (static, final, abstract, synchronized etc…)

Example:
public class Class1 {
public int a =10, b =20;
public void add(){
int result = a +b;
System.out.println(result);
}
public static void main(String[] args) {
Class1 obj = new Class1();
obj.add();
}
}
———————————-
iv) Variables in Java

A named memory location to store temporary data within a program.

Three types of variables in Java

1) Local Variables

2) Instance Variables

3) Class / Static Variables

Example:

public class Class1 {
public static int a =100, b =20;

public static void main(String[] args) {
int c = 30;
System.out.println(a);
System.out.println(c);

if (a > b){
int d=40;
System.out.println(a + b + d);
System.out.println(d);
}
//System.out.println(d);
System.out.println(b);
}
}
———————————-
v) Operators in Java

Operators are used to perform Mathematical, Comparison and Logical Operations.

Categories of Operators in Java

1) Arithmetic Operators

2) Relational Operators

3) Assignment Operators

4) Logical Operators
Etc…
——————————–
1) Arithmetic Operators

a) Addition + (Addition, String Concatenation)

b) Subtraction – (Subtraction, Negation)

c) Multiplication *

d) Division /

e) Modules %

f) Increment ++

g) Decrement —
————————
2) Relational Operators

a) ==

b) !=

c) >

d) >=

e) <

f) <=
—————————
3) Assignment Operators

a) Assignment =

b) Add and Assign +=

c) Subtract and Assign -=

d) Multiply and Assign *=
————————
4) Logical Operators

a) Logical Not operator !

b) Logical And Operator &&

c) Logical Or operator ||
———————————-
Example:

public class Class1 {
public static void main(String[] args) {
int a=10, b=20, c =30;

System.out.println(a+b);
System.out.println(a*b);
System.out.println(a-b);

System.out.println(a > b);
System.out.println(a < b);

System.out.println(a+10);

if ((c > a) && (c > b)){
System.out.println(“C is a Big Number”);
}
else {
System.out.println(“C is Not a Big Number”);
}

}
}
———————————-
vi) Java Conditional Statements

1) Two types of statements

a) if statement

b) switch statement
———————–
2) Three types of Conditions

a) Single Condition (Positive/Negative)

b) Compound Condition (Positive/Negative)

c) Nested Condition (Positive/Negative)
———————————-
3) Usage of Conditional Statements

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

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

c) Execute a block of statements when a Compound condition is true, otherwise execute another block of statements.

d) Decide among several alternates (Else if)

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

f) Decide among several alternates using Switch statement
—————————-
Example:

int a=100, b=20;

if (a > b){
System.out.println(“A is a Big Number”);
}

if (a > b){
System.out.println(“A is a Big Number”);
}
else {
System.out.println(“B is a Big Number”);
}

if (!(b > a)){
System.out.println(“A is a Big Number”);
}
else
{
System.out.println(“B is a Big Number”);
}
———————————-
vii) Loop Statements

Used for Repetitive execution.

Four types of loop structures in Java

1) for loop

2) while loop

3) do while loop

4) Enhanced for loop(Arrays)
————————-
Examples:

//Print 1 to 10 Numbers
for (int i=1; i<=10; i++){
System.out.println(i);
}
——————–
//Print 1 to 10 Numbers
int i=1;
while (i<=10){
System.out.println(i);
i++;
}
—————————-
//Print 1 to 5 Numbers Except 4
for (int i = 1; i <=5; i++){
if (i != 4){
System.out.println(i);
}
}
———————————-
viii) String handling in Java

> Sequence of characters written “”

> String may contain Alfa bytes or Alfa-numeric or Alfa-numeric and Special characters or only numeric.

“ABCD”
“India123”
“India123$#@”
“123”
————————–
Example:
System.out.println(“India”);
System.out.println(“India123”);
System.out.println(“123”);
System.out.println(“$%^”);
System.out.println(“India123%^&*”);
———————————-
Creating Strings

String myTool = “UFT”;//String Variable
String [] myTools ={“UFT”, “RFT”, “Selenium”};//Array of Strings
System.out.println(myTool);//UFT
————————
Example 3:

String str1 =”Selenium”;
String str2 = ” UFT”;
System.out.println(str1 + str2);//Selenium UFT
System.out.println(str1.concat(str2));//Selenium UFT
System.out.println(“Selenium”+1+1);//Selenium11
System.out.println(1+1+”Selenium”);//2Selenium
———————————-
ix) 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 type.

> Each item in an Array is called an Element.
—————————-
Example:

int a [];
a= new int[3];
a[0]=10;
a[1]=20;
a[2]=30;
System.out.println(a[1] + a[2]);
——————————–
int a [] = new int [3];
a[0]=10;
a[1]=20;
a[2]=3;
System.out.println(a[0] – a[2]);
——————————-
int a [] = {10, 20, 30, 40};
System.out.println(a[1] + a[3]);
——————————–
//Different Types of Arrays

char [] a = {‘A’, ‘B’, ‘C’, ‘d’};//Array of Characters
int [] b ={10, 20, 30, 40}; //Array of Integers
String [] c = {“UFT”, “RFT”, “Selenium”};//Array of Strings
boolean [] d ={true, false, false, true}; //Array of Boolean values.
double [] e ={1.234, 2.345, 5.6}; // Array of decimal point values


x) Java IO Operations

Reading Data (Read Input, Read data from files)
—————————–
Using java.util.Scanner is the easier way and it includes many methods to check input is valid to read.

Examples:

1) Read Input:

Scanner scan = new Scanner(System.in); //System.in is an Input stream
System.out.println(“Enter Your Name”);
String name = scan.nextLine();
System.out.println(“Your Name is “+ name);

System.out.println(“Enter Your Number”);
int num = scan.nextInt();
System.out.println(“Your Number is “+ num);
—————————
2) Display Output on the Console

public static void main(String[] args) {
int a =10, b=20;
System.out.println(a);//10
System.out.println(“welcome to Selenium”);//welcome to Selenium
System.out.println(“Addition of a, b is “+ (a+b));//Addition of a, b is 30
System.out.println(“value of a is “+ a + ” Value of b is “+b);//Value of a is 10 value of b is 20”
}


Selenium Quick Tutorials

Tutorial 1: Introduction to Selenium
http://www.gcreddy.com/2016/06/selenium-the-beginning.html

Tutorial 2: Selenium Test Life Cycle
http://www.gcreddy.com/2016/06/selenium-test-life-cycle-2.html

Tutorial 3: Java Quick Tutorial for Selenium Part-1
http://www.gcreddy.com/2016/06/java-quick-tutorial-for-selenium.html

Tutorial 4: Java Quick Tutorial for Selenium Part-2
http://www.gcreddy.com/2016/06/java-quick-tutorial-for-selenium-part-2.html

Tutorial 5: Selenium Quick Tutorial Part-1
http://www.gcreddy.com/2016/06/selenium-webdriver-quick-tutorial.html

Tutorial 6: Selenium Quick Tutorial Part-2
http://www.gcreddy.com/2016/07/selenium-webdriver-quick-tutorial-part-2.html

Tutorial 7: TestNG Framework in Selenium
http://www.gcreddy.com/2016/07/testng-framework-quick-tutorial-for-selenium.html

Follow me on social media: