java for selenium
java for selenium
Introduction to Java
Core Java
Introduction to Java
For automating the test cases using Selenium, we need to learn Core Java. (i.e. Advanced Java is not required for
Selenium)
Java Installation
Google 'Eclipse IDE' and select the search result that appears on the top
Select 'Eclipse IDE for Java EE Developers'
Select 'Windows 32 Bit'
Click on 'Download'
Extract the downloaded ZIP file
DAY 13
Java - Part 2
Eclipse IDE Installation (Continuation)
Double Click the Application file to launch Eclipse IDE and specify Workspace path
Pin the Application file to the Task Bar
The below are the steps for creating a Java Project in Eclipse IDE:
Right click in the 'Project Explorer' and select 'New > Project > Java Project'
Provide 'Project Name' and click on 'Finish' button
Right click on the 'src' folder and select 'New > Class'
Provide 'Class Name', select the checkbox for main method and click on 'Finish' button
Write a sample Java statement - System.out.println("Hello World!");
Right click on the .java file and select 'Run As > Java Application' to execute the Java program
Compiler Errors
Java Complier Errors will be displayed when we make syntax mistakes in the Java Code:
Example: All the Java statements in Java should end with ';' symbol
Remove the ; from the end of Java Statement
Example: Java is case sensitive
Replace 'S' with 's' in the statement
Example: Remove any of the closing brace
DAY 14
Java - Part 3
Print Statements
Print statements in Java are used to print the program output to the console.
Comments
Comments provided in a Java program won't be executed and are generally used to explain the underlined code.
In order to store the data in Java programs, we need to use Variables, Data Types, operators and Literals.
Example: int a = 5;
Refer more details here
Demonstrate a program which stores the data into a variable and prints it
Variables
Local Variables
A variable which is declared inside the method is called local variable (Demonstrate here)
Instance Variables
A variable which is declared inside the class but outside the method is called Instance variable (Demonstrate
here)
We have to specify the static text before the instance variable as the method which is going to use this
variable is a static method
This concept will be explained in upcoming sessions.
DAY 15
Java - Part 4
Data Types
We can define the variables with different Data types, based on the type of data to be stored.
But while working with Selenium, we only need int, double, char, boolean and String
int
Integer values can be stored into a variable which is declared with int data type
Example: int a = 5;
Demonstrate storing integer values into a variable which is declared with int data type
double
Decimal values can be stored into a variable which is declared with double data type
Example: double b = 6.73;
Demonstrate storing decimal values into a variable which is declared with double data type
char
Single character can be stored into a variable which is declared with char data type
Example: char c = 's';
Demonstrate storing a single character into a variable which is declared with char data type
boolean
true / false can be stored into a variable which is declared with boolean data type
Example: boolean d = true;
Demonstrate storing a boolean value into a variable which is declared with boolean data type
String
Sequence of characters can be stored into a variable which is declared as String
Example: String e = "This is a sample text";
Demonstrate storing a sequence of text into a variable which is declared as String
Operators
Operators are just symbols used to perform operations on the provided data.
Java - Part 5
Operators (Continued)
Flow Control
Flow Control describes the order in which statements will be executed at run time.
There are different types of flow control statements and can be categorized as below:
Selection Statements
Selection Statements have one or more conditions which return either true or false when evaluated.
if statements
Code inside the if decision making statement executed only when the condition provided inside if decision
making statement returns true. (View screenshot here)
Syntax (View screenshot here)
Demonstrate if decision making statement
Demonstrate the execution of the statements inside if block when the if condition is
true. (Demonstrate here)
Demonstrate that statements inside if block are skipped from execution when the if condition is false.
(Demonstrate here)
if ... else statements
While using if ... else decision making statement, code inside if block gets executed when the if condition is
true and code inside else block gets executed when the if condition is false. (View screenshot here)
Syntax (View screenshot here)
Demonstrate if else decision making statement
Demonstrate the execution of statements inside if block when the if condition is true (Demonstrate
here)
Demonstrate the execution of statements inside else block when the if condition is false (Demonstrate
here)
if ... else if ... else statements
if .. else if .. else statements contains more than one conditions. If the first if condition returns false, the
code inside the if block will be skipped and control will be taken to the next conditions i.e. else if conditions.
If all the else if conditions return false, the code inside else if blocks will be skipped and the code inside the
else block without condition will be executed. (View screenshot here)
Syntax (View screenshot here)
Demonstrate the program when all the if and else if conditions have returned false and code inside
the else block is executed (Demonstrate here)
Demonstrate the program when the if condition has returned true and code inside the if block got
executed skipping all the remaining else if and else blocks (Demonstrate here)
Demonstrate the program when one of the else if condition has returned true and code inside that
else if block got executed skipping all the remaining if, else if and else blocks (Demonstrate here)
switch statements
Based on result of a condition expression, switch case chooses one of many possibilities. (View screenshot
here)
Syntax (View screenshot here)
The result of a condition expression needs to result a int or character or a string value.
Demonstrate switch case (Demonstrate here)
Demonstrate switch case which dont match any case and executes the code in the default section.
(Demonstrate here)
Demonstrate what happens when we dont provide break; statements inside the cases. (Demonstrate
here)
DAY 18
Java - Part 7
Iterative Statements
Iterative Statements helps us in executing the same block of code multiple times.
Iterative Statements executes the same set of code until the loop condition is satisfied. (View screen-shot here)
Different types of Iterative Statements:
while loop
while loop executes the same block of code multiple times i.e. until the boolean condition turns false.
while loop tests the condition before executing the code in loop body. (View here)
Syntax: View here
Demonstrate while loop
Demonstrate the while loop when the condition is always true (Demonstrate here)
Loop will be iterated infinite times as the condition is always true
Demonstrate the while loop when the condition is false (Demonstrate here)
Loop wont be executed at-least once as the condition is false.
Demonstrate the while loop where the condition is initially true and after few iterations is turned false
(Demonstrate here)
Loop will be executed until the condition turns false.
do while loop
do-while loop works similar to while loop, but the block of code will be executed at-least once even after the
condition is false
Unlike while loop, do-while loop tests the condition after executing the code in loop body. (View here)
Syntax: View here
Demonstrate the do-while loop where the condition is initially true and after few iterations has turned false
(Demonstrate here)
Loop will be iterated multiple times until the condition becomes false
for loop
for loop is the most commonly used loop in Java and is used when we know the number of iterations in
advance.
for loop executes the same block of code multiple times, until the boolean condition turns false.
Though while loop and for loop work in the similar manner, while loop is preferred over for loop when we
dont know the number of iterations in advance. (View here)
Syntax: View here
Demonstrate the for loop when the condition is initially true and after few iteration has turned false
(Demonstrate here)
Loop will be iterated multiple times until the condition becomes false
for-each loop
Will be explained later, as it is generally used with Arrays and Collections.
Transfer Statements
Transfer statements are used to transfer the flow of execution from one block of code to a different block of code.
Different types of Transfer Statements:
break;
The purpose of the break; statement is to come out of the statements based on some condition.
Demonstrate the usage of break; statements inside any loop statement (Demonstrate here)
continue;
The purpose of the continue statement is to skip the current iteration of a loop based on some condition and
continue with the next iteration.
Demonstrate on how to use continue; statements inside any loop statement (Demonstrate here)
return
Will be explained later, as it is used with methods.
try, catch, finally
Will be explained later, as it is used in Exception Handling
DAY 19
Java - Part 8
Methods
main() method is a method where the program execution starts and we can write programming logic inside the
main() method - Demonstrate here
Demonstrate creating multiple methods along with the below - Demonstrate here
Creating multiple methods along with main() method
All the method should reside inside the Class
main() method calling other method
non-main() method calling other method
method() calling other method multiple times
Demonstrate single, multiple parameterized methods and passing arguments to those methods - Demonstrate here
Create a single parameterized method
Create a multiple parameterized method
Call the single and multiple parameterized methods by passing the arguments while calling
Demonstrate returning the values back to the calling methods - Demonstrate here
Return nothing from a method
Return int value from a method
Return String value from a method
DAY 20
Java - Part 9
Classes and Objects
Arrays
Using Arrays, multiple values of same data type can be stored into a single variable.
Arrays can be categorized as below:
DAY 21
Java - Part 10
Arrays (Continued)
Java - Part 11
Arrays (Continued)
Disadvantages of Arrays
Arrays are fixed in size. Once the Array is created, there is no chance of decreasing or increasing the size of
the array.
Collections are used to overcome the above disadvantages, which will be explained in the future sessions.
String
String is not a data type, instead it is a predefined class in Java Class Library
Google "Java 8 API" and find the String class in the Java Class Library
Actual Representation of String - String s = new String("Sample Text"); - Demonstrate here
Shortcut Representation of String - String s = "Sample Text"; - Demonstrate here
Concatenate two strings
using '+' operator - Demonstrate here
Predefined methods of String class - Out of all the predefined methods of Strings, the below are the methods which
are useful as part of Selenium Automation:
Using equals() method to compare two strings - Demonstrate here
Using length() method to find the length of String literal text - Demonstrate here
Using substring() method to retrieve the portion from the actual String text - Demonstrate here
Using trim() to remove the spaces before and after the string text - Demonstrate here
Using indexOf() to check whether the provided text is in the provided paragraph. - Demonstrate here
Returns -1 in case the provided text is not available
Using split() method to split the text into different parts based on the provided text, symbol or space.
DAY 23
Java - Part 12
Wrapper Classes and Primitive Data types
In order to use primitive data types as Objects, we have to use Wrapper Classes which help us in converting the primitive data types
into objects.
For, all the above data types, there are corresponding Wrapper Classes as shown below:
Demonstrate a program which uses Wrapper Classes for converting the primitive data types into Objects and Objects into
primitive data types - Demonstrate here
Constructors
this keyword
The purpose of the this keyword is to differentiate the instance variable with the parameterized variables of methods/constructors.
Java - Part 13
Overloading
Duplicate methods/constructor names are allowed inside the same class, as long as their parameters count or declaration
are different.
method overloading
Two or more methods having the same name can be created inside a single class as long as their
parameters count or declaration are different.
In this case, the methods are said to be overloaded and the concept is knows as Method overloading
Compiler error will be displayed when more than one method has the same name - Demonstrate here
Demonstrate how method overloading concept can avoid compiler error - Demonstrate here
constructor overloading
The same concept of method overloading when applied to constructors is known as constructor overloading
In this case, the constructors are said to be overloaded and the concept is known as Constructor
overloading
Packages
We generally group things to organize them better for locating them easily.
Default package - Create a new Java project say Facebook and Create a new Java Class say 'FacebookLogin' and
observed that a default package will be created. - view here
Package creation - Create a new Java project say Facebook and group the Classes under various packages - view
here
Demonstrate - Accessing instances variables and methods from other class which is under the same package
Demonstrate - Importing the Classes in the other packages while accessing the instance variables and methods
created in the Classes which are under other packages
Demonstrate - Using * in the import statements to import all the classes in the package instead of importing a
single class every time
Inheritance
Inheritance is a mechanism in which one class acquires the properties (i.e. variables and methods) of another class
The purpose of this Inheritance is to use the properties (i.e. methods and variables) inside a class instead of
recreating the same properties again in new class.
Child class acquires the properties (i.e. variables and methods) of Parent Class.
Child class uses extends keyword to inherit the properties from parent class
Demonstrate a child class which inherits the properties from Parent Class - Demonstrate here
Child class can have specific properties (i.e. variables and methods) which are not available in the parent
class
Object created for parent class can access the variables and methods that are created in parent class only. It
cannot access the child class properties.
Object created for child class which is inheriting the parent class can access the variables and methods of
both parent class and child class.
DAY 25
Java - Part 14
Overriding
When a method in the Child class (i.e. sub-class) is duplicate of a method in Parent class (i.e. super-class) , then the
method in the sub-class is said to override the method in super-class.
When we create an Object for Sub-class and call the overridden method, the method in the sub-class will be called
- Demonstrate here
Even though the name of the method in the sub-class has the same name as a method in super-class, if the type of
parameters or number of parameters, then the method in the sub-class will overload the method in super-class
instead of overriding
Constructors cannot be overridden as the name of the constructor needs to be same as the name of the Class.
Modifiers
Classes/variables/methods specified with 'public' access modifier can be accessed directly by the classes which are
in the same package - Demonstrate
Classes/variables/methods specified with 'public' access modifier can be accessed by the classes outside the
package after importing the classes - Demonstrate
When no modified is specified before classes/variables/methods, then we name it as default modifier - Demonstrate
default means public to all the classes inside the same package and private to the classes which are outside the
package - Demonstrate
protected means public to all the classes inside the same package and private to all the classes which are outside
the package except child classes
Java classes cannot be specified with 'protected' access modifier - Demonstrate
While accessing the protected variables/methods outside the packages using sub-classes, we don't have to create
an object to access them as they are inherited variables and methods - Demonstrate
DAY 26
Java - Part 15
Modifiers (Continued)
The value of the variable cannot be changed on specifying it with final non-access modifier - Demonstrate here
final modifier specified classes cannot be inherited/extended by other classes - Demonstrate here
final modifier specified methods in a class cannot be overridden by its sub-classes - Demonstrate here
Java - Part 16
Interfaces
The purpose of an interface is to just to declare all the functionalities required before actually implementing them.
Java - Part 17
Exception Handling
Exception is nothing but an error which is occurred during runtime i.e. during program execution
If an exception has occurred during program execution at any step, the steps which are after the exception wont be
executed - Demonstrate here
Exceptions Hierarchy
Demonstrate ArithmeticException and handle it using 'ArithmeticException' class in catch block - Demonstrate here
Demonstrate ArrayIndexOutOfBoundsException and handle it using 'ArrayIndexOutOfBoundsException' class in
catch block - Demonstrate here
Exception class is the parent class of all the Exception Classes like ArithmeticException
and ArrayIndexOutOfBoundsException classes and can handle them
Throwable class is the grant parent class of all the Exception Classes like ArithmeticException
and ArrayIndexOutOfBoundsException classes and can handle them
DAY 29
Java - Part 18
Exception Types
Unchecked exceptions are the exceptions that are not checked by compiler and will occur only during execution -
Demonstrate AirthmeticException
Checked Exceptions are the exceptions that are checked by the compiler - Demonstrate FileNotFoundException
Handling Checked Exceptions using try .. catch block
Ignoring Checked Exceptions using throws keyword
DAY 30
Java - Part 19
Handling Files
The purpose of handling files in Selenium is to read the text from the Files. (Demonstrate here)
Using File Class represent a file in Java, which is available outside the Project workspace.
Using File Class represent a file in Java, which is available inside the Project workspace.
Read a File in Java and print every line in the file to the output console
Optimize the reading and printing from a File using while loop
Collections Framework
ArrayList
ArrayList is nothing but a re-sizable array and is not of fixed size
Demonstrate an ArrayList which stores integer values and uses for loop to print those values - Demonstrate
here
Demonstrate an ArrayList which stores different types of values and uses for each loop to print those values
- Demonstrate here
Assigning the object of ArrayList class to Collection / List Interface - Demonstrate
DAY 31
Java - Part 20
HashSet
Unlike ArrayList, HashSet wont have index values and hence we cannot use for loop with HashSet
Unlike ArrayList, HashSet stores the values in a random order
Demonstrate HashSet which stores integer type of values and uses for each loop to print those values -
Demonstrate here
Assigning the object of HashSet class to Collection / Set Interface - Demonstrate
Iterator interface and iterator() method
iterator() is a predefined method of Collection interface, who's return type is Iterator interface
hasNext() and Next() are the predefined methods of the Iterator interface
Demonstrate using Iterator and iterator() with ArrayList - Demonstrate here
Demonstrate using Iterator and iterator() with HashSet - Demonstrate here
HashMap
Instead of storing the objects as a group of Objects, HashMap stores the objects in the form of key value
pairs.
Demonstrate a HashMap which stores different key value pairs and uses get() method to retrieve a value
based on the provided key - Demonstrate here
Demonstrate a HashMap which stores different key value pairs and uses for each loop to print those values -
Demonstrate here
In Selenium Automation, we need to read the values from the Excel file and call the appropriate methods having the same
names as the values.
Demonstrate a program which calls a method having the same name as String text - Demonstrate here
Demonstrate a program which calls a parameterized method having the same name as String text - Demonstrate
here
Purpose - In Selenium Automation, we need to read the values from the Excel file as Strings and call the
appropriate methods having the same names as the String text.