Quality Testing

Quality is delighting customers

Selenium - OOPs Concepts

Object Oriented Concepts of Java for Selenium:
There are 2 types of functions in Selenium.
1) Static functions
2) Non Static functions

Static functions: These functions are those that contain a static keyword. Static functions can access all other static members within the java file directly.However all non static members are accessed by the help of a reference to the object of the class.

example: A class is a collection of functions and variables. 
Public class Addition{
int i; //Global variables
int j;
int sum;
static int total=40;
public static void main(String[]args){
System.out.println("Addition");
sum=i+j;
System.out.println("sum");
System.out.println("total");
}
//Below is a non static function as it does not contain the static keyword. Also note that this function does not return any value back to the main function though it is not mandatory and the description of this function has been given outside the main function and not inside the main function. Remember that any other static or non static function has to be given outside the main function.
public void display(){
System.out.println("Your name is:"+"Arun");
i=10;
j=20;
sum=i+j;
System.out.println(i);
System.out.println(j);
System.out.println(sum);
}

In the above script the main function even though static in nature can access the variables directly since the variables are global variables. It is also possible for the main function to access the static global variables directly. The static main function is also not returning any value back hence the return type of the main function is given as "void". Normally in Java the main function will not return any value however other functions can return a value to the main function.

Also note that the non static function is able to access all the static or non static members of the class irrespective of itself being static or non static function. That means any non static function can access anyother static or non static member directly however any static function must use an object reference to access non static members.

Concept of parameterized functions:
example: A class is a collection of functions and variables. 
Public class Addition{
int i; //Global variables
int j;
int sum;
static int total=40;
public static void main(String[]args){
System.out.println("Addition");
Addition a1=new Addition();
a1.i=30;
a1.j=30;
a1.sum=a1.i+a1.j;
System.out.println("sum");
System.out.println("total"); 
//declaring an object of Addition class and a1 is the reference to //that object. So a1 points to //the location of the Addition object. The Addition Object contains //all the Non static //variables/functions of the class.Purpose is a1 can access the display function and call it inside //main function.
a1.display("Swaroop"); //parameterized non static function called by the object reference.
}
public void display(String name){
System.out.println("Your name is:"+name);
i=10;
j=20;
sum=i+j;
System.out.println(i);
System.out.println(j);
System.out.println(sum);
}

In the above script if you execute you get the latest values of the non static variables i,j and sum.
This is because the values of the global variables can be changed inside each function of the class and hence the global variables can be used to perform different operations inside different functions. In the above script the value inside i,j and sum are changed inside main function by the reference object a1 and inside display function by i,j and sum (Non static members can be accessed directly inside non static functions).

Static global variables can be accessed directly inside both a non static function and a static function.

if you want to call a static function inside the main static function then you can directly call the function by its name or you can call it by using the class name. In java when you want to call any function that is inside another java file then you first take a object reference of that class and then use the object reference to call that function in the present java file.

for example: If there is a Class called pss.java that contains a non static function called writelog() then this Writelog function can be called inside the main function by first declaring a object reference to pss class and then using the reference.writelog(); we can call the writelog function inside main static function. Hence concept of objects is important in Java.
When you implement this to testing, each java file will be a testscript file that contain many actions like Login, createaccounts, deleteaccounts. These functions are generally static functions or non static functions written by the tester and manually put inside a Library file such as pss.java. Once this is done he/she can use the concept of objects to call these functions from the library file thus giving you functional driven concept of automation framework.

Note: It is possible to access the value inside a static variable by using the name of the class and also by using the object reference inside a static function. However doing this will give an error in the script but there is no problem in execution.

A static variable will be in a common location for all the object references.Hence if any of the individual object references change the value of the static variable then this new value will be accessible by all other object references.
For example:
Addition a1=new Addition(); //object reference 1 for the same object Addition b1=new Addition(); //object reference 2 for the same object
Addition c1=new Addition(); //object reference 3 for the same object
a1.total=20; //here it is possible to change the value inside the static variable with the object references.
b1.total=100; //here it is possible to change the value inside the static variable with the object references.
c1.total=300; //here it is possible to change the value inside the static variable with the object references.
System.out.println(Addition.total); //accessing the static variable with the class name
Hence the output for the above script would be 300 since the last object reference c1 is changing the value inside the total static variable.

Note: All static variables are commonly available for each of the object reference.

Now something on the Object references: In the above script if you say

Below is the second java file that contains a class called Object_Reference and this class contains a main static function that uses a Object reference a1, b1 and c1 for the Addition class file inside Addition.java file.
public class Addition{
int i=20; //Global variables
int j=20;
int sum;
int k;
int l;
static int total=40;
public static void main(String[]args){
System.out.println("Addition");
Addition a1=new Addition(); //object reference 1 for the same object
Addition b1=new Addition(); //object reference 2 for the same object
Addition c1=new Addition(); //object reference 3 for the same object

a1.i=30;
a1.j=30;
a1.sum=a1.i+a1.j;
System.out.println(a1.sum);
a1.i=40;
System.out.println(total); 
System.out.println(c1.i); //c1 is another object reference and the variable value is not assigned.
System.out.println(b1.i);//b1 is another object reference and the variable value is not assigned.
System.out.println(Addition.total); //a static variable can be called inside another static function by the name of the class.

a1.total=20; //here it is possible to change the value inside the static variable with the object references.
b1.total=100; //here it is possible to change the value inside the static variable with the object references.
c1.total=300; //here it is possible to change the value inside the static variable with the object references.
System.out.println(Addition.total); //accessing the static variable with the class name

//declaring an object of Addition class and a1 is the reference to //that object. So a1 points to 
//the location of the Addition object. The Addition Object contains //all the Non static 
//variables/functions of the class.Purpose is a1 can access the display function and call it inside 
//main function.

//a1.display("Swaroop");
//Addition.display(99); //a static function called inside main static function through the classname
//display(100); //a static function called inside main static function directly

}
public void display(String name)
{

System.out.println("Your name is:"+name);
System.out.println(total); //a static member directly accessed inside a non static function
//i=10;
// j=20;
// sum=i+j;
// System.out.println(i);
// System.out.println(j);
// System.out.println(sum);
}
public static void display(int number){

System.out.println("Your name is:"+number);
System.out.println(total); //a static member directly accessed inside a non static function

}
}

In simple terms the global non static variables inside Addition.java file are called and assigned values inside the Object_Reference.java file.

So basically a1.i refers to the value inside the variable however a1=b1 means a1 now points to the location of i earlier referred by b1.Remember a1,b1 and c1 are memory locations and not the values of i.

public class Object_Reference {
//int i=10;
//int j=20;
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Object_Reference");
Addition a1=new Addition(); //object reference to another java class
Addition b1=new Addition(); //object reference to another java class
Addition c1=new Addition(); //object reference to another java class
a1.i=20; //this is the value of i in the first object of Addition class
b1.i=30; //this is the value of i in the second object of Addition class
c1.i=50; //this is the value of i in the third object of Addition class
System.out.println(a1.i);
System.out.println(b1.i);
System.out.println(c1.i);
a1=b1;
b1=c1;
c1=a1;

System.out.println("dislaying values after swaping the object references");
System.out.println(a1.i); //this is referring the value inside b1.i after swaping
System.out.println(b1.i); //this is referring the value inside c1.i after swaping
System.out.println(c1.i); //this is referring the value inside b1.i after swaping
//Here after swaping c1.i is referring to a1.i but since a1 refers to b1 c1.i refers to b1.i. (if a=b and b=c then a=c)
//two object references point to the same object of Addition class. In this case if one of the references 
//changes the value inside the Non static variable then it gets affected for all other object references.
//In this case a1.i also changes when c1.i is changed. 
a1.i=300;
System.out.println("You changed the value of i in a1 object reference; observe c1 object reference");
System.out.println("A1:"+a1.i);
System.out.println("C1:"+c1.i);
}

}

Concept of Pass by value and by reference in java: Understanding of this concepts will help you to understand which parameter has been passed into library functions so that tracking becomes easier and debugging can be done easily since selenium Junit framework implements these concepts of java.
public class Point {
int x;
int y;
public static void main(String[]args){
Point p=new Point();
p.x =10;
p.y= 20;

swap(p.x,p.y); //passing the values of x and y to the swap function
System.out.println(p.x);
System.out.println(p.y);

swap(p); //passing the object reference to the swap function
System.out.println(p.x);
System.out.println(p.y);
}
//swapping by value
public static void swap(int a,int b){
int temp=0;
temp=a;
b=a;
a=temp;
}
//Swapping by reference. Usually Objects are passed by reference.
public static void swap(Point q){
int temp= q.x;
q.x=q.y;
q.y=temp;
}
}

In the above script the function swap has been Overloaded. The concept of Overloading means that the function name remains same but the number and type of parameters of the function changes. Any function can be overloaded in Java including a constructor.

Constructor in Java:
A constructor will have the same name as that of the class. These constructors are activated by the objects of the java class whenever they are declared within the main static function.

The main purpose of a constructor is to allow initializing the object references of the non static variables inside the class.
public class Addition{
String Browser;
String Page;
static int total=40;
//Constructor
public Addition(){
System.out.println("Inside the constructor Addition");
}
//Constructor Overloaded
public Addition(String B, String P){
Browser= B;
Page= P;
}

//Using Constructors for assigning the Browser and Page titles into the non static variables //inside the Addition.java file.
public class Constructors {
public static void main(String[] args) {
//Here direct values are passed into the non Static variables Browser and Page that are inside
//Addition class inside Addition.java file.
//The values of the Browser and Page are accessed through the object reference add1
//Only passing the values happens inside Constructors class
//Assigning value actually happens inside the overloaded constructor within the Addition.java file.
Addition add1= new Addition("Gmail from Google","Gmail-Email from Google"); 
System.out.println(add1.Browser);
System.out.println(add1.Page);
//Below it is possible to capture the value of the GUI component and then compare it with the 
//Expected values and then validate the property values of the application using Selenium.
}
}

TTWT Magazine


Advertisement

Advertisement

Advertisement

Advertisement

© 2021   Created by Quality Testing.   Powered by

Badges  |  Report an Issue  |  Terms of Service