Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 14

SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Datatypes in Java

In java or any other programming languages, data types represents that


which type of values can be stored and what will be the size of that value or
how much memory will be allocated. There are nearest eight different data
types in java like byte, short, int, long, float, double, char, Boolean. byte and
short datatypes are not much more useful in selenium webdriver so I am
skipping them here to decrease your confusions.

Int datatype
Int data type is useful to store 32 bit integer (Example: 4523) values only. We
can not store decimal (Example 452.23) values in int data type.
Example:

int i = 4523;

long datatype
long datatype is useful to store 64 bit integer(Example : 652345) values. You
can use it when your value is more larger and cannot hold it in int. same as
int datatype, we cannot store decimal (Example 452.23) values in long
datatype
Example:

long l = 652345;

double datatype

double datatype is useful to store 64 bit decimal(Example : 56.2354) values.


We can store integer (Example 12456) values too in double datatype.
Example :

double d1 = 56.2354;
double d2 = 12456;

char datatype

char datatype is useful to store single character(Example : 'd'). It can not


store more than one (Example 'dd') character in it.
Example :
char c = 'd';

boolean datatype
boolean datatype is useful to store only boolean(Example : true) values.

Page 1 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Example :

boolean b = true;

String Class
String is not a data type but it is a class which is useful to store string in
variable.
Example :

String str = "Hello World";

VIEW DIFFERENT FUNCTIONS OF STRING CLASS

Created bellow given example for all these datatypes. Run it in your eclipse
and verify results.

public class datatypes {

public static void main(String[] args) {


int i = 4523; //Can store 32 bit integer values only.
long l = 652345; //Can store 64 bit integer values only.
double d1 = 56.2354; //Can store 64 bit decimal values.
double d2 = 12456; //We can use it for integer values too.
char c = 'd'; //Can store single character only.
boolean t = true; //Can store only boolean values like true or false.
String str = "Hello World"; //Can store any string values.

System.out.println("Integer Var Is --> "+i);


System.out.println("Long Var Is --> "+l);
System.out.println("double Var d1 Is --> "+d1);
System.out.println("double Var d2 Is --> "+d2);
System.out.println("char Var c Is --> "+c);
System.out.println("boolean Var b Is --> "+t);
System.out.println("boolean Var str Is --> "+str);
}
}

Bellow given result will display in your console at the end of execution.

Integer Var Is --> 4523


Long Var Is --> 652345
double Var d1 Is --> 56.2354
double Var d2 Is --> 12456.0
char Var c Is --> d
boolean Var b Is --> true

Page 2 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

String Var str Is --> Hello World

String Class In Java

We have learnt about DIFFERENT DATA TYPES IN JAVA In my past post. Now,
Many peoples are understanding that String Is also a data type. Let me
correct them -> String Is not data type. If string Is not data types then what
Is String? This question can be asked by Interviewer too. String Is an
Inbuilt class of

java. String class has many Inbuilt functions which we can use to perform
different actions on string.

If you don't know, Let me tell you that we have to work with strings very
frequently In selenium WebDriver tests. So you must have knowledge of
useful functions of String class to use them In your selenium webdriver test
development. Let us take simple example of String to understand different
methods of String class.

public class Strng_Example {

public static void main(String[] args) {

String st1 = "This World is Very Nice";


String st2 = " And Beautiful.";

//Comparing two strings. Return true If both match else return false.
System.out.println("st1 equals to st2? -> "+st1.equals(st2));

//Concatenates st2 with st1.


System.out.println("Concatenation of st1 and st2 Is -> "+st1.concat(st2));

//Retrieve the 9th Indexed character from string.


System.out.println("Character at Index 9 Is -> "+st1.charAt(9));

//Find the length of string.


System.out.println("Length Of St1 -> "+st1.length());

//Converting whole string In lower case.


System.out.println("String In Lowercase -> "+st1.toLowerCase());

//Converting whole string In upper case.


System.out.println("String In uppercase -> "+st1.toUpperCase());

Page 3 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

//Retrieve the Index of first 'i' character.


System.out.println("Index of 1st charater i Is -> "+st1.indexOf('i'));

//Retrieve the index of 2nd most 'i' character.


System.out.println("Index of 2nd charater i Is -> "+st1.indexOf('i', 3));

//Retrieve the Index of word 'Very' from string.


System.out.println("Index of word Very Is -> "+st1.indexOf("Very"));

//Converting value From int to string.


int j = 75;
String val2 = String.valueOf(j);
System.out.println("Value Of string val2 Is -> "+val2);

//Converting string to integer.


String val1="50";
int i = Integer.parseInt(val1);
System.out.println ("Value Of int i Is -> "+i);

//Print the String starting from 5th Index to 12th Index.


System.out.println("Retrieving sub string from string -> "+st1.substring(5,
13));

//Split string.
String splt[] = st1.split("Very");
System.out.println("String Part 1 Is -> "+splt[0]);
System.out.println("String Part 2 Is -> "+splt[1]);

//Trim String.
System.out.println("Trimmed st2 -> "+st2.trim());
}
}

If you will look at above example, I have prepared different examples of


string method to take some action or we can say operations on string. Sort
explanation of all above string methods are as bellow.

Two String Comparison


To compare two strings, we can use syntax like st1.equals(st2). It will return
True If both strings are same else It will return False.

Two String Concatenation


Syntax st1.concat(st2) will concatenate st1 with st2.

Page 4 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Retrieving Character from Index


Syntax st1.charAt(9)) will retrieve the character from string st1 located at 9th
Index. Index Starts from 0.

Length Of String
st1.length() will return lenght of string st1.

Convert String In Lower Case Letters


st1.toLowerCase() will convert whole string letters In lower case.

Convert String In UpperCase Letters


st1.toUpperCase() will convert whole string letters In upper case.

Retrieving Index Of 1st most character


st1.indexOf('i') will retrieve Index of first most character 'i' from string.

Retrieving Index Of 2nd most character


st1.indexOf('i', 3)) will retrieve Index of second most character 'i' from string.
3 described the from Index means It will start finding character 'i' from Index
3. First 'i' has Index 2 so we need to use 3 to find 2nd most character.

Retrieving Index Of specific word from string


st1.indexOf("Very") will retrieve index of word 'Very' from string.

Convert from Integer To String


String.valueOf(j) will convert value of 'j' from int to string.

Convert from String To Integer


Integer.parseInt(val1) will conver value of 'val1' from string to int.

Retrieving sub string from string


Syntax st1.substring(5, 13) will retrieve string from Index 5 To Index 13.

Split String
String splt[] = st1.split("Very") will split string st1 from word 'Very' and store
both strings In array.

Trim String
If string has white space at beginning or end of the string then you can use
trim function like st2.trim() to remove that white space.

if, if else and nested if else In Java

Page 5 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

If, if else and nested if else statements are useful to take the decision based
on conditional match. When you wants to execute some part of code if
condition returns true then you need to use this kind of conditional
statements.

Simple If Statement
Part of code will be executed only if specified condition returns true. If
condition will return false then that code will be not executed.
Example :

if (i<j)
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
In above given example, message will be printed only and only if value of
variable i is less than value of variable j.

If else Statement
If condition returns true then part of if block will be executed. If condition
returns false then part of else block will be executed.
Example :

if (i>=j)
{
System.out.println("Value Of i("+i+") Is Greater Than Or Equals To Value Of
j("+j+")." );
}else
{
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );
}
In above given example, if block's message will be printed if value of variable
i is greater than or equals to value of variable j. else block will be executed if
value of variable i is less than value of variable j.

Nested If Else Statements


You can use nested if else statement when you wants to check multiple
conditions and take decision based on it.
Example :

if (k<i)
{
System.out.println("Value Of k("+k+") Is Less Than Value Of i("+i+")" );
}else if (k>=i && k<=j)
{

Page 6 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

System.out.println("Value Of k("+k+") Is In Between Value Of i("+i+") And


Value Of Value Of j("+j+")" );
}else
{
System.out.println("Value Of k("+k+") Is Greater Than Value Of j("+j+")" );
}

In above given example, first (if) block will be executed if value of variable k
is less than the value of variable i. Second (else if) block will be executed if
value of variable k is greater than or equals to value of variable i and less
than or equals to value of variable j. Third (else) block will be executed if
value of variable k is greater than value of value of variable j. You can make
a chain of if else statement if you wants to check more conditions.

This way, You can use any of above conditional statement based on your
requirement. Run bellow given example in your eclipse by changing the
values of variables.

public class IfStatements {

public static void main(String[] args) {

int i = 25;
int j = 50;
int k = 24;
//Simple If statement
System.out.println("***Simple If Statement Example***");
if (i<j) //Bellow given message will be printed only if value of variable i is
less than value of variable j.
System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );

//If Else Statement


System.out.println("");
System.out.println("***If Else Statement Example***");
if (i>=j)//Bellow given message will be printed if value of variable i is
greater than or equals to value of variable j.
{
System.out.println("Value Of i("+i+") Is Greater Than Or Equals To Value Of
j("+j+")." );
}else//Bellow given message will be printed if value of variable i is less than
value of variable j.
{

Page 7 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

System.out.println("Value Of i("+i+") Is Smaller Than Value Of j("+j+")." );


}

//Nested If Else Statement


System.out.println("");
System.out.println("***Nested If Else Statement Part***");
if (k<i)//Bellow given message will be printed if value of variable k is less
than value of variable i.
{
System.out.println("Value Of k("+k+") Is Less Than Value Of i("+i+")" );
}else if (k>=i && k<=j)//Bellow given message will be printed if value of
variable k is greater than or equals to value of variable i and less than or
equals to value of variable j.
{
System.out.println("Value Of k("+k+") Is In Between Value Of i("+i+") And
Value Of Value Of j("+j+")" );
}else //Bellow given message will be printed if value of variable k is greater
than value of variable j.
{
System.out.println("Value Of k("+k+") Is Greater Than Value Of j("+j+")" );
}
}
}

Bellow given output will be printed in your eclipse console when you will run
above example.

***Simple If Statement Example***


Value Of i(25) Is Smaller Than Value Of j(50).

***If Else Statement Example***


Value Of i(25) Is Smaller Than Value Of j(50).

***Nested If Else Statement Part***


Value Of k(24) Is Less Than Value Of i(25)

for loop In Java

for Loop - Basic Java Tutorials For Selenium WebDriver

Page 8 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Now let we move to loops in java. Loops(for loop, while loop, do while loop)
have very important role in selenium webdriver test case development with
java or any other languages. As you know, sometimes you need to perform
same action
multiple times(Example : 100 or more times) on your web page. Now if you
will write multiple lines of code to perform same action multiple times then it
will increase your code size. For the best practice, you need to use loops in
this kind of situations.

for Loop
There are three parts inside for loop. 1. Variable Initialization, 2. Condition To
Terminate and 3. Increment/Decrements variable. for loop will be terminated
when condition to terminate will becomes false.
Example :
for(int i=0; i<=3; i++){
System.out.println("Value Of Variable i is " +i);
}

Bellow given example is simple example of for loop. run it in your eclipse and
verify the result.

public class forloop {

public static void main(String[] args) {


for(int i=0; i<=3; i++){ //This loop will be executed 4 times
System.out.println("Value Of Variable i is " +i);
}

System.out.println("");
int i=0;
int k = 200;
for(int j=3; j>=i; j--){ //This loop will be executed 4 times
System.out.println("Value Of Variable j is " +j);
k = k-10;
}
System.out.println("");
System.out.println("Value Of Variable k is " +k);

When you will run above given example, you will get bellow given output.

Page 9 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Value Of Variable i is 0
Value Of Variable i is 1
Value Of Variable i is 2
Value Of Variable i is 3

Value Of Variable j is 3
Value Of Variable j is 2
Value Of Variable j is 1
Value Of Variable j is 0

Value Of Variable k is 160


while, do while loops In Java

Now let me describe you while loop and do while loop with practical
examples.

while Loop
Block of code which is written inside while loop will be executed till the
condition of while loop remains true.
Example :
int i = 0;
while(i<=3){
System.out.println("Value Of Variable i Is "+i);
i++;
}
In above given example, while loop will be executed four times.

do while Loop
Same as while loop, do while loop will be executed till the condition returns
true.
Example :

int j=0;
do{
System.out.println("Value Of Variable j Is "+j);
j=j-1;
}while(j>0);
In above given example, while loop will be executed only one time.

Difference between while and do while loop


There is one difference between while and do while loop.

while loop will check condition at the beginning of code block so It will
be executed only if condition (while(i<=3)) returns true.

Page 10 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

do while loop will check condition at the end of code block so It will be
executed minimum one time. After 1st time execution, it will check the
condition and if it returns true then code of block will be executed once
more or multiple time.

Disadvantage of while or do while loop


If you will forget to Increment or decrements variable value inside while loop
block then block of code will be executed infinite time.
Example :

int i = 0;
while(i<=3){
System.out.println("Value Of Variable i Is "+i);
}
Above given while loop will be executed infinite time because variable is not
incremented inside while loop block.

Bellow given full example of while and do while loops will clear out your all
doubts. Simple run it in your eclipse and verify result.
public class Whileloop {

public static void main(String[] args) {

//while loop - will be executed till condition returns true.


System.out.println("***while loop example***");
int i = 0; //Variable initialization
while(i<=3){
System.out.println("Value Of Variable i Is "+i);
i++;//Incrementing value of i by 1.
}

//do while loop - will be executed minimum one time without considering
condition.
System.out.println("");
System.out.println("***do while loop example***");
int j=3; //Variable initialization
do{
System.out.println("Value Of Variable j Is "+j);
j=j-1;//Decrementing value of j by 1;
}while(j>=0);
}
}

Output of above given example will be as bellow.

Page 11 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

***while loop example***


Value Of Variable i Is 0
Value Of Variable i Is 1
Value Of Variable i Is 2
Value Of Variable i Is 3

***do while loop example***


Value Of Variable j Is 3
Value Of Variable j Is 2
Value Of Variable j Is 1
Value Of Variable j Is 0

Object In Java

What Is An Object In Java?


If Interviewer ask you this question then your answer should be like this :
Object Is an Instance of class. In other words we can say object Is bundle of
related methods and variables. Every object has Its own states and behavior.
Objects are generally used with constructors In java. Understanding of object
Is very
Important for Selenium WebDriver learner because we have to create and
use objects In our test case development. We can create multiple objects for
the same class having Its own property.

Let us take one real world scenario to understand object clearly. Simplest
example Is bicycle Is an object of vehicle class having Its own states and
behavior. Same way, motorcycle Is another object of vehicle class. Few
properties for both the objects are same like wheels=2, handle=1. Few
properties for both the objects are different like price, color, speed etc..

How to create object?


You can create object of class vehicle using new keyword as bellow.

public class vehicle {


public static void main(String[] args) {
//Created object for vehicle class using new keyword.
//bicycle is the reference variable of this object.
vehicle bicycle = new vehicle("Black");

Page 12 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

//Constructor with color parameter passed. It will retrieve value from object
vehicle.
public vehicle(String color){
//Retrieved value will be printed.
System.out.println("Color Of vehicle Is "+color);
}
}
Console Output will looks like bellow when you will run above example.

Color Of vehicle Is Black

In above example, Used one constructor to pass the value of object. We will
look about constructor In my upcoming post. Please remember here one
thing - bicycle Is not an object. It Is reference variable of object vehicle.
Based on this example, Now we can say that object has three parts as
bellow.

Declaration : Variable declaration for object. In this example, bicycle is


the reference variable for object.

Instantiation : Object creation using new keyword Is called


as Instantiation.

Initialization : Call to a constructor Is known as object initialization.

This way you can use object of class and also you can create multiple objects
of any class.

Bellow given example will show you how to create multiple object of class to
pass different kind of multiple values In constructor.
public class vehicle {

public static void main(String[] args) {


//Create 2 objects of class. Both have different reference variables.
vehicle bicycle = new vehicle("black", 2, 4500, 3.7);
vehicle motorcycle = new vehicle("Blue", 2, 67000, 74.6);

public vehicle(String color, int wheels, int price, double speed){


System.out.println("Color = "+color+", Wheels = "+wheels+", Price =
"+price+", Speed = "+speed);
}
}

Page 13 of 14
SUDHEER REDDY ANKIREDDYGARI

SELENIUM WEBDRIVER

Output of above program will looks like bellow.

Color = black, Wheels = 2, Price = 4500, Speed = 3.7


Color = Blue, Wheels = 2, Price = 67000, Speed = 74.6

Page 14 of 14

You might also like