Saturday, 23 November 2019

SE - 42 - Major interfaces used in Selenium

Lets see few of the interfaces which are used in Selenium. The list also contains few of the JAVA interfaces which may be used in terms of Selenium :

1) WebDriver
2) WebElement
3) Alert
4) TakesScreenshoot
5) All the Listners (Eg : WebdriverEventListener , ItestListener ,etc)
6) SearchContext
7) Iterable , Collection,List,Queue, Set , etc
8) Wait
9) Comparator
10)JavaScriptExecutor
11) Row , Cell (Apache POI)

SE - 41 - Island of isolation in JAVA

Island of isolation is a term given to a condition where 2 objects are ready for garbage collection when the following conditions are met :

1) Object 1 references to Object 2
2) Object 2 references to Object 1

      Test i;
      public static void main(String[] args) 
      {
          Test t1 = new Test();
          Test t2 = new Test();
          t1.i = t2;
          t2.i = t1;
          t1 = null;
          t2 = null;
          System.gc();
      }

SE - 13 - Static and non-Static Methods !!

- We can call static methods directly while we can not call non static methods directly. You need to create and instantiate an object of class for calling non static methods.

- Non static stuff (methods, variables) can not be accessible Inside static methods Means we can access only static stuff Inside static methods. Opposite to It, Non static method do not have any such restrictions. We can access static and non static both kind of stuffs Inside non static methods

- Static method Is associated with the class while non static method Is associated with an object.

- Static methods can't be overrided in java. If we try to override a static method Java treats it as a new method and rule of parent/child doesn't applies to it

-Static methods can be overloaded in JAVA with rules of overloading

Monday, 18 November 2019

SE - 16 - Caching in Java for Integers

In java "==" is used to compare object references. The caching in Java allows to reuse the referenced objects and save memory. So in the given range of caching the same object reference is used and the comparison results as true while beyond the range it returns false.

Note :
1) Caching was introduced in Java 5
2) It basically improves the performance
3) Caching range lies between -127 to +127
4) Caching applies for Integer , Byte , short , Long and Character objects too
5) It only works for autoboxed type of objects. It is not applicable for normal object declaration(eg : Integer integer1 = new Integer(123);.


Example :
 public static void main(String[] args) {
  Integer integer1 = 123;
  Integer integer2 = 123;
  
  Integer integer3 = 128;
  Integer integer4 = 128;
  
  if (integer1 == integer2)
   System.out.println("integer1 == integer2");
  else
   System.out.println("integer1 != integer2");
  
  if (integer3 == integer4)
   System.out.println("integer3 == integer4");
  else
   System.out.println("integer3 != integer4");
  
 }


Output:
integer1 == integer2
integer3 != integer4





SE - 15 - String methods in Java !

1) To compare two strings
 - str1.compareTo(str2) [3-way comparison (positive ,negative,0)]
 - str1.equals(str2) [2-way comparison(true,false)]

2) To join 2 strings
 - str1.concat(str2) [Its a method]
 - str1 + str2 [Its a operator]

3) to find the character by index position
 - str1.charAt(1)

4) to compare 2 strings ignoring the case
 - str1.equalsIgnoreCase(str2);

5) to convert a string to upper or lower case
 - Str1.toUppercase();
 - Str1.toLowercase();

6) to remove spaces from both sides of string
 - str1.trim();

7) to trim a part of string
 - Str1.substring(2,12); [based on index]

8) It checks that a string ends\starts with specified suffix and it result boolean result
 - str.endsWith("ABC") [checks if the string ends with ABC]
 - str.startsWith("ABC") [checks if the string starts with ABC]

9) to find the length of a string
- str.length();

10) to replace any word with given word in a string
 - str1.replace("AB","CD")


SE - 40 - Copy Constructor in Java

We can create a copy constructor by giving the parameter as object of same class . Once created then we copy each field  of the input object into the new instance

Note :
1) Java never creates a copy constructor by default rather we need to manually create it.
2) Copy constructor and cloning in Java are not same

public class Student {
    private int roll;
    private String name;
     
    public Employee(Student stu) {
        this.id = stu.roll;
        this.name = stu.name;
    }
}



SE - 39 - Some basics about thread in Java

Lets see some of the basics about thread in java :

1) Thread can't be started twice. It can be started only once by using start() method. If we attempt to start the thread twice we get IllegalThreadStateException.

2) We can create Thread in Java in 2 ways :
     a) By implementation of Runnable class
     b) By extending Thread class

3) Only start() method is used to start the thread. If we call the run() method then the Thread will not be called rather it will behave as normal method calling

4)  Suppose if there is any statement written (Eg : printing some text , etc) after start() method in thread then in output we will see the thread initiated after the execution of next statement.  Java will execute the next statement then start the thread.

Sunday, 10 November 2019

SE - 7 - Methods and constructors calling in Inheritance

In inheritance the Methods are called in sequence of Bottom to Top(Child to Base) and Constructors are called from Top to Bottom (Base to Child). Lets see through an example.

Lets assume there are 2 classes , Base and Child with one constructor each and one common(overridden) method in each of them.


public class ABBA 
 { 
     public static void main(String[] args) 
     { 
         Base x = new Base(); 
         Base y = new Child(); 
         Child z = new Child(); 
         System.out.println("Object of base class");
         x.Print();
         System.out.println("Object of child class by giving reference of base class");
         y.Print();
         System.out.println("Object of Child class");
         z.Print();
     } 
 } 
 class Base { 
   Base(){
    System.out.println("Base Constructor"); 
   }
      public void Print() { 
          System.out.println("Base Method"); 
      }          
  } 
    
  class Child extends Base { 
   Child(){
    System.out.println("Child Constructor"); 
   }
      public void Print() { 
          System.out.println("Child Method"); 
      } 
  } 

Output : 

Base Constructor
Base Constructor
child Constructor
Base Constructor
child Constructor
Object of base class
Base Method
Object of child class by giving reference of base class
Child Method
Object of Child class
Child Method

Lets understand the program. So when there is a base and child , there can be 3 possibilities of object creation : 

1) Creating object of child class
2) Creating object of base class
3) Creating object of child class by giving reference of base class.


What will happen : 
1)  In all the 3 scenarios the base constructor will be called for sure since we have inherited base class and constructor is called from Top to Bottom , that is from base to child.

2) When we will create object of base class then only the Base constructor is called. The child constructor is never called.

3) When we create object of child class in scenario 1 and 3 above the base and child constructors will be called.

4) When object of base class is called by giving reference of base class then only method of base class will be called.

5) In case of child class object(scenario 1 and 3), if the method is called which is overridden by child class then only child class method is called. Whenever we create object of child class then only the overridden methods are called. Method calling works from bottom to top.

6) If we call a method which is not present in child class then base class method will be called.

7) If a method is not present in base class and present in child class , then it will give error when we try to access it in scenario 3 (Creating object of child class by giving reference of base class)

8) In scenario 1 and 3 , only the overridden method will be called. Base class method which are overrided will never be called.

SE - 38 - Static Initialisation block.Vs Instance Initialisation block vs Constructor

The order in which the Static Initialisation block ,  Instance Initialisation block and Constructors are called is as below:

1) Static Initialisation block - This is called when memory is allocated , so this one is called very first. If there are multiple blocks then it is called in the order they are written.

2) Instance Initialisation block - This is called before the Constructors.If there are multiple blocks then it is called in the order they are written.

3) Constructors : Constructors are called at last.

Lets quote an example to clearly understand difference between these 2 type of blocks:

public class Test 
 { 
     public static void main(String[] args) 
     { 
         A a = new A(); 
     } 
 } 
   
class A 
{ 
  
//first static initialization block
static {
 System.out.println("Static-1");
}

//first Instance Initialization bloc
{
 System.out.println("Init-1");
}

//Constructor
A(){
 System.out.println("constructor");
}

//second static initialization block
static {
 System.out.println("Static-2");
}

//second Instance Initialization bloc
{
 System.out.println("Init-2");
}} 


Output : 
Static-1
Static-2
Init-1
Init-2
constructor

So here we can see that Static blocks are called first in the order they were written , then instance blocks were called and at last constructors were called.


Tuesday, 20 August 2019

SE - 37 - Switching between the frames(defaultContent() and parentFrame())

I'd like to mention few important things regarding switching and toggling between frames :

i) If the frame is at same level we can't jump between the frames while we are already on frames. Suppose 2 frames frame1 and frame2 are popped on a button click. We used switchTo() method to switch to the frame1. Now we want to switch to frame2. This is not possible since we are on frame1 and both the frames are at same level. We will get NoSuchFrame Exception if we try to switch to frame2 if we are on frame 1 and both the frames are at same level.
Here we need to switch back to the parent window or frame by using defaultcontent()/parentFrame() then again we can use swichTo() to switch to second frame.

2) If frames are at multiple levels then we can use switchTo() method to switch between multiple frames. Suppose we click on button and frame1 is generated. Now we click on a button on frame1 then frame 2 is generated. If we click on a button on frame 2 then frame3 is generated and if click on button on frame3 then a new frame frame4 is generated.
In this scenario if we want to switch to frame 4 we need to switch to multi level frames :
-> use switchTo() to switch to frame1
-> When you are on frame 1, use switchTo() to switch to frame 2
-> When you are on frame 2, use switchTo() to switch to frame 3
-> When you are on frame 3, use switchTo() to switch to frame 4

In this way we can switch between the frames at multi and single levels

Now the question arises , How to switch back to the initial frames?
To answer this I'd like to include 2 methods defaultContent() and parentFrame().

->defaultContent() - This method will allow you to switch to the initial or very first window from where the frames were generated.

->parentFrame() - This method will allow you to switch to the immediate parent frame in case of multi level frames. As discussed above in scenario 2 if we want to switch from frame2 to frame1 or frame3 to frame 2 we can use this method. If we are on frame2 and use switchTo().parentFrame(), it will switch to the frame1.

Sunday, 19 May 2019

SE - 8 - Close() and Quit() in Selenium WebDriver!

Close() - It just closes focused browser.If there are more than one Browser window opened by the Selenium Automation, then the close( ) command will only close the Browser window which is having focus at that time. It wont close the remaining Browser windows.

Quit() - It closes all browsers. quit( ) WebDriver command is generally used to shut down the WebDrivers instance. Hence it closes all the Browser windows that are opened by the Selenium Automation.

SE - 36 - Different types of Xpath in Selenium

Xpath is one of the commonly used locators in Selenium across many projects because of its flexibility in different variants. Due to these variants we get the flexibility to use xpath in different ways. Lets discuss each one of them:

1) Tag with attribute - This is the simplest form of Xpath in which we use tag with any attribute in the given html :
Syntax :
Xpath = //input[@id= 'systemId']


2) Tag with multiple attribute - We can provide multiple attributes in a tag based on requirement by using OR and AND 
Syntax :

Xpath = //input[@id= 'systemId' OR @name ='TheName']
Xpath = //input[@id= 'systemId' AND @name ='TheName']


3) Tag with any attribute - We can also locate any element in the tag by giving "*" at the place of attribute. It will search for all the attributes in the particular tag.
Syntax :
Xpath = //input[@*= 'systemId']


4) Any Tag with given attribute - We can also locate a particular element in  any of the the tag by giving "*" at the place of tag. It will search for all the tags in the particular node with the given attribute.
Syntax :
Xpath = //*[@id= 'systemId']


5) Any Tag with any attribute - We can also locate any element in any of the the tag by giving "*" at the place of tag and attribute. It will search for all the tags and all the attributes in the node.
Syntax :
Xpath = //*[@*= 'systemId']


6) Using contains method - There are many instances when the xpath keep on changing but value remains same. Sometimes the text or partial text remains same and other changes. In such situations we use contains keyword. We can also use "*" to look for all the tags else we can also give a static tag
Syntax :
Xpath = //*[contains (@*,'systemId')]
Xpath = //*[contains (@id,'systemId')]
Xpath = //input[contains(@*,'systemId')]
Xpath = //input[contains(@id ,'systemId')]
Xpath = //*[contains(text(), 'systemId')]
Xpath = //input[contains(text(), 'systemId')]

we can also use not before contains if required.
Syntax :
Xpath = //*[not(contains (@*,'systemId'))]

7) Using text method - This method allows to look for a text in any attribute to locate it. We can give full or partial text as well 
Syntax :
Xpath = //*[text() = 'systemId']
Xpath = //input[text(), 'systemId']


8) Using starts-with method - This is also similar to above methods where the xpath is not static. 
Syntax :
Xpath = //input[ starts-with (@id = 'systemId')]


9) Using last method - This is used to locate the last element of a node. we can use (last()-n) if we want to locate any element before the last one.It is mostly used in locating tr[table row] and td[table data] .
Syntax :
Xpath = //input[@id ='systemId']//tr[last()-1]


10) Using parent method - This method is used to locate a parent node through a child node. This may be used when we are not directly able to locate a object and we need to navigate through parent :
Syntax :
Xpath = //input[@id ='systemId']/parent::td/parent::tr2]


11) Using child method - This method is used to locate all the child elements from the parent or current node.
Syntax :
Xpath = //input[@id ='systemId']/child::td[2]


12) Using following method - This method is used to locate all the elements from the current node. Descendants are not selected using this method .
Syntax :
Xpath = //input[@id ='systemId']/following::td[2]


13) Using preceding method - This method is used to locate all the nodes that is before the current node. Ancestors are not selected using this method .
Syntax :
Xpath = //input[@id ='systemId']/preceding::td[2]


14) Using ancestor method - This method is used to locate all the ancestor nodes like parent grandparent that is before the current node.
Syntax :
Xpath = //input[@id ='systemId']/preceding::td[2]


15) Using following-sibling method - This method is used to locate all the following siblings which are at same level
Syntax :
Xpath = //input[@id ='systemId']/following-sibling::td[2]


16) Using preceding-sibling method - This method is used to locate all the preceding siblings which are at same level
Syntax :
Xpath = //input[@id ='systemId']/preceding-sibling::td[2]


17) Using descendant method - This method is used to locate all the descendant nodes from the current node like child, grandchild , etc
Syntax :
Xpath = //input[@id ='systemId']/descendant::td[2]


18) Using self method - This is used to select the current node
Syntax :
Xpath = //input[@id ='systemId']/self::td[2]

Saturday, 4 May 2019

SE - 35 - How to check that bottom of page has reached ?


In selenium we can check that we have reached the bottom of page by using the javascript commands :
br.execute_script("if (document.body.scrollHeight == document.body.scrollTop + window.innerHeight) 

return true; 
} else 

return false;
}")