Java for Selenium Part 1


Java Environment Setup, Java Syntax and Java program Structure.

Java can be used in Selenium to write Programs (Test Scripts), Selenium supports other Programming languages also but most of the Selenium Testers (nearly 77%) using Java Language.

i) Java Environment Setup

ii) Java Syntax

iii) Java Program Structure
---------------------------------------
i) Java Environment Setup

Steps:

1) Download Java Software (jdk) and Install

2) Set Environment Variable path (Path Variable)

3) Download Eclipse IDE and Extract
------------------------------------------------------------
Download Java from either java.com or oracle.com, and install

After Installation you can get Java folder in C:\program Files.
-------------------------------------------------------------
Set Environment Variable Path

Navigation:

Select myComputer and right click
> Properties
> Advanced System Settings
> Environment Variables
> Select Path Variable from System Variables
> Edit
> Copy java bin directory path and paste in variable value field
> OK
> OK
> OK
> Close
---------------------------------------------------------
1) Write and Execute a Java Program from Command prompt

2) Write and Execute a Java Program using Eclipse IDE
-----------------------------------------------------------------
1) Write and Execute a Java Program from Command prompt

Step 1: Write a Program

Step 2: Compile the Program

Step 3: Run / Execute the Program
----------------------------------------------
Step 1: Write a Program

public class Sample {
public static void main (String [] args) {
System.out.println ("Hello Selenium World");
}
}

Save with java extension
--------------------------------------------------
Step 2: Compile the Program

> Launch Command prompt
> Change to Java Program file directory
> Type javac ProgramName.java

* It creates Java Class file
------------------------------------------------------
Step 3: Run / Execute the Program

> Launch Command prompt
> Type java ProgramFileName

Then it displays Output on the Console
-----------------------------------------------
Edit Java Program

Open the program file and Edit
> Compile the file
> Run / Execute the Program
---------------------------------------
2) Write and Execute a Java Program using Eclipse IDE

Eclipse IDE:

Eclipse Integrated Development Environment

> Eclipse IDE is platform to write and execute computer programs like Java, Perl, Python, Ruby, PHP etc...

> It provides editor for writing programs, Syntax guidance, Context Help, Auto Compilation etc...

> Eclipse IDE is an Open Source Software.
------------------------------------------------------------------
Download Eclipse IDE and Extract 

Steps to Write and execute a Java program using Eclipse IDE

> Launch Eclipse IDE
> Create Java Project
> Create Java Package
> Create Java Class / Java Interface

> Write Java Program
Eclipse IDE provides Auto Compilation
> Run/Execute the Program
----------------------------------------------------------
ii) Java Syntax Rules:

1) Java is a Case sensitive Language.

2) First letter of Class name should be in Upper Case.

3) Method names should start with Lower case letter

4) Java program file name should exactly match with class name.

5) Java Program execution starts from main method, which is mandatory in every Java program.

6) Every Statement / Step should end with Semo colon (;).

7) Code blocks (ex: Conditions, Loops, method declarations etc...) enclosed with {}.
----------------------------------------------
iii) Java Program Structure

1) Documentation Section

2) Package declaration statement

ex: package javaExamples;

3) Import Statements

We import Buil-in and User defined libraries using import keyword.

Ex:

import java.io.Console;
import java.lang.*;

import - It is a Java Keyword

java - Project

io - Package

Console - Class

lang.*; -  All Classes from the Package

4) Class declaration Statement

public class Sample {}

public - Access Modifier

class - Java Keyword / Reserved word to declare the Class.

Sample - Class Name 

5) main Method

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

String [] args - is the parameter to the main method.

6) Declarations

We declare Variables and Constants.

int a;
a=100;

or
int a = 100;

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

Or
int a=10, b=20, c=30;
--------------------
char a ='A';
--------------------
final int b=100; //Correct
--------------------
final int c; //Incorrect
c=100;

7) Normal Statements

d = a+b;
System.out.println (d);

System - Class Name
out - Object
println - Method

8) Code Blocks 

Condition blocks

Loop Blocks

Method Blocks

Constructer blocks etc...

Note: 

1) Every Normal Statement should end with semi colon (;).

2) Every code block should enclose with {}.
------------------------------------------------------------------
Syntax to Create Object
Classname objectName = new Classname();
---------------------------------
A Sample Java Program 

// It is a Sample Program to understand Java program Structure 
//and Syntax

package javaExamples;

import java.io.Console;
import java.lang.*;

public class Sample2 {
//Create a Method with returning a value (It is to multiply three numbers)
public int multiply(int a, int b, int c){
int result = a * b * c;
return result;
}
// Create a Method without returning any value
public void comparison(){
int x, y;
x=10; y=20;
if (x > y){
System.out.println("X is a Big Number");
}
else
{
System.out.println("Y is a Big Number");
}
}

public static void main(String[] args) {
//Create Object
Sample2 abcd = new Sample2();
//Acess user defined methods using Object
int z = abcd.multiply(123, 987, 455);
System.out.println(z);
abcd.comparison();

//Variables and Constants declaration
int a =10, b=20, c, d; //variables Declarations and Initialization for some variables.
c=30; //Initialization

final int money =100; //Constant Declaration

d= a + b + c;
System.out.println("Addition of a, b, c is: "+ d);//Addition of a, b, c is: 60

System.out.println(money);//100
System.out.println(c); //30
c=123;
System.out.println(c); 

//Condition Block
if (a>b){
System.out.println("A is a Big Number");
}
else {
System.out.println("B is a Big Number");
}

//Loop Block
for (int i=1; i <=10; i++){
System.out.println(i);
}
}
}
---------------------------------------------------
Java for Selenium Part 2 Link
 

3 comments:

Respected sir,

Your classes are excellent and easily understanding. Thank you so much for sharing your valuable classes and notes.

If there is any possibility , Can we have any project on manual testing with tutorial video.

Thanks,
Venkatesh.

Software Testing Live Project Part 1:
(http://www.gcreddy.com/2016/04/software-testing-live-project.html)

Hello sir

thank you for easy steps

please share Hadoop testing tutorials if possible

Post a Comment