Wednesday, 18 July 2018

SE - 23 - Nested class in Java

Java supports two type of nesting :

1) Static nested class
2) inner nested class

Lets take a code example :

Here we have created a inner nested class and static nested class
public class mainclass {
 //inner nested class
 public class subclass{
  //method inside inner nested class
  public void method1() {
    
    System.out.println("inner nested class");
   }
  }
 //static nested class
 public static class substaticclass{
  //method inside static nested class
  public void method2() {
   
   System.out.println("nested static class");
  }
 } 
}

In the below code we are calling the methods created in the inner nested class and static nested class in above code :
public class classCall {

 @Test
 public void callm() {
  //calling a inner class
  mainclass x = new mainclass();
  mainclass.subclass z = x.new subclass();
  z.method1();
  
  //calling a static class
  mainclass.substaticclass c = new mainclass.substaticclass();
  c.method2();
 }
}

Output:
inner nested class
nested static class

So here we can see clearly that the members of static classes can be called directly by class name. For the inner class object we need to create reference of the outer class then call the members of the inner class.


SE - 22 - Shadowing in Java

Shadowing is a common concept in java. It may refer to classes or variables. The concept of shadowing comes when a variable of same name overlaps within different scopes. For example :

public class A{

int z = 10;
system.out.println(z);

public void M()
{
int z = 20;
system.out.println(z);
}
}

Output :
10
20

In the above example we saw clearly that the variable z is same in class and method level but its scope are different. When we try to print the variable from the method , the value 20 is printed . So here the variable of higher- scope level is hidden and variable of lower scope overrides it. This is called shadowing.

Similarly this concept is used as class level too. When we use nested class then the variable in the parent class is suppressed by the variable of the nested inner class.  For Example :

public class A{

int z = 10;
system.out.println(z);

public class B{
int z = 20;
system.out.println(z);

public void M()
{
int z = 30;
system.out.println(z);
}
}
}

Again the variable z is same in 2 different classes and 1 method but its scope is limited. When we call a lower lever variable it suppresses the higher ones.

Sunday, 15 July 2018

SE - 21 - Static Methods - OverLoad and OverRide

This is one of the confusing concept that whether we can overload or override static methods. so lets discuss at solution.

-> Can Static methods be overloaded?
The answer to this question is "YES. The static methods can be simply overloaded like other methods. For static method overloading we just need to keep the name same and signature/parameter different.

Eg: In the below example static method ABC is overloaded
public class Testing {
    public static void ABC() {
        System.out.println("Method ");
    }
    public static void ABC(int a) { 
        System.out.println("OverLoaded Method with argument" + a);
    }
    public static void main(String args[])
    { 
        Test.ABC();
        Test.ABC(10);
    }
}

-> Can we override Static methods ?
The answer to this question is "NO". It can not be overridden. If we make a method with same name in new class then it is considered as new method by java. Compiler will not give any error when we make a method in extended class with the same name as the parent class. Few more points to be considered :

Here a base class is created with overloaded methods named "a"
public class  Base{
 public static void a(int a) {
  System.out.println("static method 1");
 }
 
 public static void a() {
  System.out.println("static method 2");
 }
}

Here we extend the base class mentioned above and made a method with the same name as "a"
public class Overr extends Base{

public static void a() {
 System.out.println("Static Method 3");
}

@Test
public void b() {
 
 Base.a();
 Overr.a();
 
Base a1 = new Base();
a1.a();

Overr a2 = new Overr();
a2.a();

Base a3 = new Overr();
a1.a();

 }
}

Now we called the method in 5 different ways and found the output below as:
Output:
static method 2
Static Method 3
static method 2
Static Method 3
static method 2

Lets discuss each approaches:

1) Since static methods can be called directly by class name , so if we call the static method of base class by class name then only the method present in base will execute

 Base.a();//output static method 2

2) In this case we called the method of the extended/overridden class by class name. Now the static method present in overridden class will called.

 Overr.a();// output Static Method 3
3) This is similar to 1. We can also call the static method by making object of class. So if we create object of base class then only the method present in base will execute 

Base a1 = new Base();
a1.a(); //output static method 2

4) This is similar to 2. We have created object of extended class "Overr" and called the method "a". Here static method present in overridden class will be called.

Overr a2 = new Overr();
a2.a();  // output Static Method 3

5) In this approach we have created the object of child class by giving reference of  base class. As per overriding rule the method which is overridden should be called, but static methods doesn't supports overriding so only base method will be called. This is the most important concept of static class.

Base a3 = new Overr();
a1.a();//output static method 2
Hence from this logic we can say that static method cant be overridden . If we try to override it a new method is created.


SE - 20 - Some tricks about Interface

Interface is one of the general concepts of Java but there are some tricky questions based on it:

1) One interface class can be extended by another interface class. It is a wrong perception of lot of people that interface can only be implemented.

2) When an interface I1  is extended by another interface I2 and I2 is implemented in class C1 , then all the methods of interface I1 and I2 has to be implemented in class C1. This is best used in the scenario when there are two methods of same name and different signature in I1 and I2 because JAVA doesn't allows a class directly to implement two interfaces having same method name and different signature type.

3) When an interface I1  is extended by another interface I2 and we try to implement I1 in class C1 , then all the methods of interface I1  has to be implemented in class C1. No need to implement methods of I2 interface.

4) As of now we were knowing that Interface does not have defined methods and it contains only declared methods. In Java 8 we have option to give method body in an Interface. It can be done in two ways :
-> a) By defining the method as Default - It can be called by implementing in child object and creating object of child class
-> b) By defining the method as Static - It can be called directly by class name.

5) If we want to implement only few of the methods of an interface and leave the remaining ones then we have to create an abstract class for implementing the partial methods of interface.

6) Multiple inheritance is not supported by java directly but it can be done by interfaces.

7) All the variables in interface are static constants

8) All the methods in interface are abstract and public

9) Java doesn't allows a class to implement two interfaces that have methods of same name but different signature

10) The interface variable has to be initialized otherwise compiler will throw an error.

11) We can't create object of interface class

SE - 19 - Difference between ScrollTo and ScrollBy in JavaScript

Both the methods are almost same with a slight difference in functionality . Let's see the syntax of both the scroll commands :

1) Window.scrollTo(x-Pixel, y-pixel);

2) Window.scrollBy(x-Pixel, y-pixel);

Difference :The scrollTo is for absolute scrolling and scrollBy is for relative scrolling. In a nutshell we can say that scrollTo will scroll to the (x-Pixel, y-pixel) position of the entire webpage while ScrollBy will move to the (x-Pixel, y-pixel) of the webpage from the current location of cursor.

Eg: Suppose we are at position (100,100) of the webpage , now

Case 1 : If we use Window.scrollTo(200,200) - The window will scroll to position 200,200 of the webpage in spite of the current position of (100,100). At the end of the execution of this command our position will be (200,200)

Case 2 :  If we use Window.scrollBo(200,200) - The window will scroll relatively from the current location to further 200,200 pixels.. At the end of the execution of this command our position will be (300,300) since we started at (100,100).

SE - 18 - Different Scroll and other Commands used in selenium using JavaScriptor

Selenium webdriver works on DOM so it not always needs scroll bar to locate the objects, but there are certain instances where the object becomes visible only after scrolling. In these situations we need to scroll through the webpage to perform any action on the objects.

There could be 2 types of scrolling : Horizontal and Vertical

To achieve the horizontal and vertical scrolling we use javascriptexecutor in Selenium. The scrolling is done based on pixels which is given as parameter..

The syntax is as below :
JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Script,Arguments);
There are different situations in which we need different types of scrolling . They are : 

1) To scroll to a particular position by giving the exact pixels in the parameters :
JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Window.ScrollBy(400,500)");
The above command will scroll the window to 400 Horizontal(x-pixels) and 500 vertical(y-pixels) pixels.

Note: If we want to scroll only horizontally or vertically then we can give that location only and keep the other one as zero. we can give the argument as :

Window.scrollBy(0,500) - to scroll 500 pixel vertically

2) To scroll on a webpage  till a element becomes visible and can be located
        //Give the locator of the  "Element" present on the webpage           WebElement Element = driver.findElement(By.name("ABCD"));

        //Now scroll on the window till the element is found  
        js.executeScript("arguments[0].scrollIntoView();", Element);
Here we locate the element which has to be found on webpage by a unique locator. Then we use the given argument in the javascriptexecutor. It will keep on scrolling till the element is found.

Note : This can be done horizontally or vertically till the element is found

3) To scroll till the end of the page
JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Window.ScrollTo(0,document.body.scrollheight)");
This command will keep on scrolling till the end of the webpage

4) To scroll Up,Down , Left and Right from current Location : 

Left
JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Window.ScrollBy(-2000,0)");

Right


JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Window.ScrollBy(2000,0)");

Up


JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Window.ScrollBy(0,2000)");
Down


JavascriptExecutor js = (JavascriptExecutor) driver;  
   js.executeScript(Window.ScrollBy(0,-2000)");


    Saturday, 14 July 2018

    SE - 17 - Run BDD(cucumber) tests in Parallel

    We will discuss the approaches to run Cucumber test cases in parallel in BDD framework. The first approach is pretty simple where we can create a separate runner class for each feature file. This approach is not so good as we will have to create too many runner classes. There will be hundreds of runner classes which will be hard to maintain.

    Now the second approach is better as compared to the first one. We need 2 plugins for parallel execution through cucumber. They are :
    (i) Cucumber JVM Parallel Plugin
    (ii) maven-failsafe-plugin

    The maven-failsafe-plugin is used to configure the parallel execution. We can configure the threadcount and parallel classes. T
    he Cucumber JVM Parallel Plugin is used to generate the runners. We need to set parallelScheme and the customVmTemplate in it. The parallelScheme must be Feature because we run feature files in parallel. There are few more configurations which needs to be done here. We need to set the Glue package where step definitions are located.