Professional Documents
Culture Documents
Datatypes in Java: Selenium Webdriver
Datatypes in Java: Selenium Webdriver
SELENIUM WEBDRIVER
Datatypes in Java
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 d1 = 56.2354;
double d2 = 12456;
char datatype
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 :
Created bellow given example for all these datatypes. Run it in your eclipse
and verify results.
Bellow given result will display in your console at the end of execution.
Page 2 of 14
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
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.
//Comparing two strings. Return true If both match else return false.
System.out.println("st1 equals to st2? -> "+st1.equals(st2));
Page 3 of 14
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
//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());
}
}
Page 4 of 14
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
Length Of String
st1.length() will return lenght of string st1.
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.
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.
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
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.
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+")." );
Page 7 of 14
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
Bellow given output will be printed in your eclipse console when you will run
above example.
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.
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
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.
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.
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 {
//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);
}
}
Page 11 of 14
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
Object In Java
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..
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.
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.
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 {
Page 13 of 14
SUDHEER REDDY ANKIREDDYGARI
SELENIUM WEBDRIVER
Page 14 of 14