Introduction
One should familiar with at least core Java also to build automation using Selenium. Here you will not get details about what is Java and in details of it but ensure that you will get the concept that you should know to build the robust automation suit. If you are familiar with any one of the language already then it will not take to understand more than one day. And, you need to take out you time to practice few java example by your own samples and exercise for at least one week on any online compilers available over Internet.For your practice the basic concepts or keywords you should aware are
- Class
- Object
- Types of Variable
- Method
- Modifiers
- Data Abstraction
- Data Encapsulation
- Constructor
- Method Overriding
- Method Overloading
- Polymorphism
- Inheritance
- Interfaces
- Packages
- Collections
- Enumeration
- ArrayList
- HashMap
- Vector
- HashTable
- Conditional Statements
- Loop Statements
Class
A class can be defined as a template. It contains methods and variable. Example, a template is a vacated house containing rooms (methods) and doors (variables) in it.public class MethodDemo{ //code } Public class VariableDemo{ // code }
Object
An object is an instance of a class. Example, house can be given rent to multiple people consider the house belongs to Paying Guest (PG) house.VariableDemo obj1 = new VariableDemo();VariableDemo obj2 = new VariableDemo(); VariableDemo obj3 = new VariableDemo();
Types of Variables
- Class/Static variables − declared with static keyword within a class, outside any method.
- Local variables − Local variables defined inside methods, constructors or blocks are called local variables. NOTE: The local variable will be declared and initialized within the method and the variable will be destroyed when the method is completed.
- Instance/Non-Static variables − Instance variables will be declared within a class but outside any method. NOTE: These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks that class only.
Example:
Public class VariableDemo{
int myVar; //instance variable
static int data = 30; //static variable
public static void main(String args[]){
int a = 100; //local variable
VariableDemo obj = new VariableDemo();
System.out.println("Value of instance variable myVar: "+ obj.myVar);
System.out.println("Value of static variable data: "+ VariableDemo.data);
System.out.println("Value of local variable a: "+a);
}
}
Result:
Value of instance variable myVar: 0
Value of static variable data: 30
Value of local variable a: 100
Method
A method is where logic's are written, data is manipulated. See the below examplepublic class MethodDemo{ int Age; // Constructor public MethodDemo(String name) { // This constructor has one parameter, name. System.out.println("Name is :" + name ); } // Class Method-I public void setAge( int age ) { Age = age; } // Class Method-II public int getAge( ) { System.out.println("Display age as :" + Age ); return Age; } public static void main(String []args) { /* Object creation */ MethodDemo MyMethodDemo = new MethodDemo( "Vasu Chiluveru" ); /* Call class method to set age */ MyMethodDemo.setAge( 2 ); /* Call another class method to get age */ MyMethodDemo.getAge( ); /* You can access instance variable as follows as well */ System.out.println("Variable Value :" + MyMethodDemo.Age ); } }
Result:
Name is :Vasu Chiluveru
Display age as :2
Variable Value :2
Modifiers
It is possible to modify classes, methods, etc., There are two categories of modifiers −- Access Modifiers (default, public, protected, private) - Java provides a number of access modifiers to set access levels to class, variable, method and constructor. The four access levels are −
- Visible to the package by default. No modifiers are needed here.
- Visible to the class only (private).
- Visible to the world (public).
- Visible to the package and all sub classes (protected).
- static modifier for creating class methods and variables.
- final modifier for finalizing the implementations of classes, methods, and variables.
- abstract modifier for creating abstract classes and methods.
- synchronized and volatile modifiers, which are used for threads.
Data Abstraction
Abstraction is a process of hiding the implementation details from the user. It can be achieved using Abstract classes and interfaces. Just use the abstract keyword before the class keyword as shown below
public abstract class AbstractionClassDemo { //Code }
You can't instantiated the "AbstractionClassDemo" as this class has a abstract keyword. Hence, this class has to inherited to any other class using the keyword extends as shown below
public class AnotherNewClass extends AbstractionClassDemo { //Code here }
Then, finally abstract class can be instantiate as shown below
public class AbstractDemo { public static void main(String [] args) { AnotherNewClass anc = new AnotherNewClass (); AbstractionClassDemo acd = new AnotherNewClass (); System.out.println("Abstract class Demo"); } }
Data Encapsulation
Data Encapsulation mechanism binds the data and methods together as single unit. In encapsulation, the variables of a class will be hidden from other classes, and can be accessed only through the methods of their current class.To achieve encapsulation in Java −
- Declare variables as private.
- Provide public setter and getter methods to view and modify the variables values.
public class EncapsulationDemo { private String name; private String cardno; public void setCardNo(int cardnos) { cardno = cardnos; } public String getName() { return name; } public int getCardNo() { return cardNo; } public void setName(String names) { name = names; } public static void main(String args[]) { EncapsulationDemo encap = new EncapsulationDemo (); encap.setName("Vasu Chiluveru"); encap.setCardNo("123456789"); System.out.print("Name : " + encap.getName() + "Card No : "+encap.getCardNo()); } }
Results:
Name : Vasu Chiluveru Card No : 123456789
Constructor
Every class has a constructor. Java compiler builds a default constructor for that class. Constructor should have same name as Class is called as Constructor. It might have no parameter or can have same or different data types as parameters in it.public class ConstructorDemo{ public ConstructorDemo() { } } public ConstructorDemo(String name) { //This constructor has one parameter } public ConstructorDemo(String name, int no) { // This constructor has two parameters, name & no. } }
Method Overriding
If a class inherits a method from its superclass, then there is a chance to override the method provided that it is not marked final.
class UFTExpert{ public void AutomationExpert() { System.out.println("UFT Expert"); } } class SeleniumExpert extends UFTExpert{ public void AutomationExpert() { System.out.println("Selenium Expert"); } } public class AutoExpert { public static void main(String args[]) { UFTExpert a = new UFTExpert(); // UFTExpert reference and object UFTExpert b = new SeleniumExpert (); // UFTExpertreference but SeleniumExpert object a.AutomationExpert(); // runs the method in UFTExpert class b.AutomationExpert(); // runs the method in SeleniumExpert class }
Result:
UFT Expert Selenium Expert
Method Overloading
It allows to have more than one method with same method name with different arguments in it. There are three different ways for method overloading.
Example 1 - Number of Parameters
overloadingMethod(int, int) overloadingMethod(int, int, int)
Example 2 - Data Type of Parameters
overloadingMethod(int, int) overloadingMethod(int, float, double)
Example 3 - Sequence of Data Type of Parameters
int overloadingMethod(int, int) float overloadingMethod(int, int)
Polymorphism
Polymorphism is the ability of an object that takes many forms.
public interface SeleniumExpert{} public class UFTExpert{} public class AutomationTester extends UFTExpert implements SeleniumExpert{}
Above, now the AutomationTester class IS-A UFTExpert
IS-A SeleniumExpert
IS-A Object
IS-A AutomationTester
So, it has many forms as stated, see the below example
AutomationTester at = new AutomationTester (); UFTExpert uftExp = at; SeleniumExpert selExp = at; Object obj = at;
Inheritance
One class acquires the properties (methods and fields) of another class is called Inheritance. extends is the keyword used to inherit the properties of a class.class SuperClass { // Code Here }
public class SubClass extends SuperClass { // Code Here }
Interfaces
A class uses the implements keyword to implement an interface. Interface is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface as shown in the examplepublic interface InterfaceClassDemo { // Code here }
Interface class InterfaceClassDemo implements on newClass that was defined as shown below
public class NewClass implements InterfaceClassDemo { //Code Here public static void main(String args[]) { NewClass nc = new NewClass (); // Code Here } }
Packages
It is a way of categorizing or organizing the classes and interfaces to makes life much easier. You might come across with already defined packages by java or user-defined packages. Mostly they will called as showed in the below example.import org.openqa.selenium.By; //Selenium packages import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.interactions.Actions; import org.openqa.selenium.support.ui.ExpectedConditions; import org.openqa.selenium.support.ui.Select; import org.openqa.selenium.support.ui.WebDriverWait; import javax.swing.*; //Java packages import java.awt.event.KeyEvent; import java.io.File; import java.lang.reflect.Field; import java.text.SimpleDateFormat; import java.util.List; import com.common.java.pages.UserDefined1; //User defined import com.common.java.steps.UserDefined2; //User defined
Collections
Java provided adhoc classes; they are Dictionary, Vector, Stack, and Properties to store and manipulate groups of objects. Fundamental goal of collection are to implement dynamic arrays, linked lists, trees, and hash-tables efficiently.
There are so many Collection Interfaces and Class are available but here specifying the collections that mostly useful in Selenium.Those examples are shown below.
- Enumeration
import java.util.Vector; import java.util.Enumeration; public class EnumerationDemo { public static void main(String args[]) { Enumeration autoExp; Vector autoExpert= new Vector(); autoExpert.add("UFT"); autoExpert.add("Selenium"); autoExpert.add("RPA"); autoExp= autoExpert.elements(); while (autoExp.hasMoreElements()) { System.out.println(autoExp.nextElement()); } } }
- ArrayList
import java.util.*; public class ArrayListDemo { public static void main(String args[]) { ArrayList autoExpert= new ArrayList(); System.out.println("Initial size " + autoExpert.size()); // add elements autoExpert.add("UFT"); autoExpert.add("Selenium"); autoExpert.add("RPA"); autoExpert.add(1,"Performance") System.out.println("Size after additions: " + autoExpert.size()); // display System.out.println("Contents " + autoExpert); // Remove autoExpert.remove("UFT"); System.out.println("Size after deletions: " + autoExpert.size()); System.out.println("Contents " + autoExpert); } }
- HashMap
import java.util.*; public class HashMapDemo { public static void main(String args[]) { // Create hash map HashMap hm = new HashMap(); // add elements to the map hm.put("Vasu", new Double(1111.11)); hm.put("Chilu", new Double(222.22)); // Get entries Set set = hm.entrySet(); // Get an iterator Iterator itr = set.iterator(); // Display elements while(itr.hasNext()) { Map.Entry mentr = (Map.Entry)itr.next(); System.out.print(mentr.getKey() + ": "); System.out.println(mentr.getValue()); } } }
- Vector
import java.util.*; public class VectorDemo { public static void main(String args[]) { // initial size : 3, increment by 2 Vector v = new Vector(3, 2); System.out.println("Initial size: " + v.size()); System.out.println("Initial capacity: " + v.capacity()); v.addElement(new Integer(1)); v.addElement(new Integer(2)); System.out.println("Capacity after 2 additions: " + v.capacity()); // enumerate the elements in the vector. Enumeration vEnum = v.elements(); System.out.println("\nElements in vector:"); while(vEnum.hasMoreElements()) System.out.print(vEnum.nextElement() + " "); } }
- HashTable
import java.util.*; public class HashTableDemo { public static void main(String args[]) { // Create a hash map Hashtable hashtab= new Hashtable(); Enumeration names; String str; hashtab.put("Vasu", new Double(1111.11)); hashtab.put("Chilu", new Double(222.22)); // Show all balances in hash table. names = hashtab.keys(); while(names.hasMoreElements()) { str = (String) names.nextElement(); System.out.println(str + ": " + hashtab.get(str)); } } }
Conditional Statements
Example - ? : Operator
10>20 ? "10 is small":"20 is greater"
int x = 10; if( x < 20 ) { System.out.print("This is if statement"); }
int x = 40; if( x < 30 ) { System.out.print("This is if statement"); }else { System.out.print("This is else statement"); }
Example - Nested if statement
int x = 20; int y = 10; if( x == 20 ) { if( y == 10 ) { System.out.print("X = 20 and Y = 10"); } }
Example - switch statement
char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent Performance!"); break; case 'B' : case 'C' : System.out.println("Good - Well done"); break; case 'D' : System.out.println("Satisfied - You passed"); case 'F' : System.out.println("Not Satisfied - Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade);
Loop Statements
Example 1 & 2 : for Statement
public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x : numbers ) { System.out.print( x ); System.out.print(","); }
public static void main(String args[]) { int [] numbers = {10, 20, 30, 40, 50}; for(int x =0;x<5;x++) { System.out.print( x ); System.out.print(","); }
while( x < 20 ) { System.out.print("value of x : " + x ); x++; System.out.print("\n"); }
Example : do ... while Statement
do { System.out.print("value of x : " + x ); x++; System.out.print("\n"); } while( x < 20 );
Thanks Ruby
ReplyDeleteI was very interested in the article , it’s quite inspiring I should admit. I like visiting your site since I always come across interesting articles like this one. Keep sharing! Regards. Read more about
ReplyDeleteVery valuable post...! This information shared is helpful to improve my knowledge skill. Thank you...!
Offshore software testing services
software testing services company
software testing services
Software Qa Services
quality assurance service providers
Performance testing services
Security testing services
software testing Companies
regression testing services