December 16, 2016

Java array declarations and instantiations

  • int[ ] a[ ] = new int [5][4] ;

       This will create an array of length 5. Each element of this array will be an array of 4 ints.

  • int a[ ][ ] = new int [5][4] ;

       This will create an array of length 5. Each element of this array will be an array of 4 ints.

  • int a[ ][ ] = new int [ ][4] ;

      The statement int[ ][4] will not compile, because the dimensions must be created
      from left to right.

  • int[ ] a[ ] = new int[4][ ] ;

        This will create an array of length 4. Each element of this array will be null. But you can assign           an array of ints of any length to any of the elements. For example:
        a[0] = new int[10];//valid
        a[1] = new int[4];//valid
       a[2] = new int[]; //invalid because you must specify the length
       a[3] = new Object[] //invalid because a[3] can only refer to an array of ints.
       This shows that while creating a one dimensional array, the length must be specified but while creating multidimensional arrays, the length of the last dimension can be left unspecified. Further, the length of multiple higher dimensions after the first one can also be left unspecified if none of the
dimensions are specified after it. So for example,
a[][][][] = new int[4][3][3][5]; is same as a[][][][] = new
int[4][][][]; (Note that the first dimension must be specified.)
Thus, multidimensional arrays do not have to be symmetrical.

  • int[ ][ ] a = new int[5][4] ;

       This will create an array of length 5. Each element of this array will be an array of 4 ints.
The [] notation can be placed both before and after the variable name in an array
int[] ia, ba; // here ia and ba both are int arrays.
int ia[], ba; //here only ia is int array and ba is an int.
Multidimensional arrays are created by creating arrays that can contain references to
other arrays .

Java superclass reference to subclass object

Given Example,

class SuperClass{
    int intVar = 0;
    void method(){
        //in super class
class SubClass extends SuperClass{
    int intVar = 2;
    void method(){
        //in sub class

SuperClass obj = new SubClass();

int val = obj.intVar;// this is taken from SuperClass as variables are decided on reference basis, if both superclass and subclass contain the same variable it is called shadowing

obj.method();// it is taken from the SubClass as it is method overriding and is decided at runtime based on the object not the reference
However there is one exception, if the method is Static in Superclass, then its decided on base of reference.

In normal scenarios, reference can access variables & methods of both Superclass and Subclass.

December 08, 2016

Difference between Array and ArrayList in Java

Resizable :   Array is static in size that is fixed length data structure, One can not change the length after creating the Array object.
ArrayList is dynamic in size . Each ArrayList object  has instance variable capacity which indicates the size of the ArrayList. As elements are added to an ArrayList its capacity grows automatically.

Performance : Performance of Array and ArrayList depends on the operation you are performing :

resize() opertation : Automatic resize of ArrayList will slow down the performance as it will use temporary array to copy elements from the old array to new array.
ArrayList is internally backed by Array during resizing  as it calls the native implemented method System.arrayCopy(src,srcPos,dest,destPos,length) .

add() or get() operation : adding an element or retrieving an element from the array or arraylist object has almost same  performance , as for ArrayList object these operations  run in constant time.

Primitives :  ArrayList can not contains primitive data types (like int , float , double) it can only contains Object while Array can contain both primitive data types as well as objects.
One get a misconception that we can store primitives(int,float,double) in ArrayList , but it is not true 

Suppose we have ArrayList object ,

ArrayList<int> list1 = new ArrayList<int>();

 JVM through Autoboxing(converting primitives to equivalent objects internally) ensures that only objects are added to the arraylist object.

thus , above step internally works like this :

ArrayList<Integer> list1 = new ArrayList<Integer>();

Iterating the values : We can use iterator  to iterate through ArrayList.
 The iterators returned  by the ArrayList class's iterator and list iterator 
method are fail-fast.We can use for loop or for each loop to iterate through array .  

Type-Safety :  In  Java , one can ensure Type Safety through Generics.
 while Array is a homogeneous data structure , thus it will contain objects of specific 
class or primitives of specific  data type. In array if one try to store
 the different data type other than the specified while creating the 
array object , ArrayStoreException is thrown
 String temp[] =  new String[2];  // creates a string array of size 2
temp[0] = new Integer(12); // throws ArrayStoreException, trying to add Integer object in String[] 
Length :  Length
 of the ArrayList is provided by the size() method while Each array 
object has the length variable which returns the length of the array.
 Integer arrayobject[] = new Integer[3];
arraylength= arrayobject.length   ;  //uses arrayobject length variable 

ArrayList  arraylistobject = new ArrayList();
arraylistobject.size();   //uses arraylistobject size method
Adding elements : We can 
insert elements into the arraylist object using the add() method while  
in array we insert elements using the assignment operator.
import java.util.ArrayList;
import java.util.Iterator;

public class ArrayArrayListExample {
    public static void main(String[] args) { 
        // ArrayList Example   
        ArrayList<String> arrlistobj = new ArrayList<String>();
        arrlistobj.add("Alive is awesome");
        arrlistobj.add("Love yourself");
        Iterator it = arrlistobj.iterator();
        System.out.print("ArrayList object output :");  
          System.out.print( + " ");
        // Array Example
        String[] arrayobj = new String[3];
        arrayobj[0]= "Love yourself";  
        arrayobj[1]= "Alive is awesome";
        arrayobj[2]= "Be in Present"; 
        System.out.print("Array object output :");
        for(int i=0; i < arrayobj.length ;i++)
        System.out.print(arrayobj[i] + " ");   

Output :  ArrayList object output :{ Alive is awesome , Love yourself }

                 Array object output :{ Love yourself , Alive is awesome, Be in present}

December 07, 2016

What is the difference between == vs equals() in Java?

There are some small differences depending whether you are talking about "primitives" or "Object Types"; the same can be said if you are talking about "static" or "non-static" members; you can also mix all the above...
Here is an example (you can run it):

public final class MyEqualityTest
    public static void main( String args[] )
        String s1 = new String( "Test" );
        String s2 = new String( "Test" );

        System.out.println( "\n1 - PRIMITIVES ");
        System.out.println( s1 == s2 ); // false
        System.out.println( s1.equals( s2 )); // true

        A a1 = new A();
        A a2 = new A();

        System.out.println( "\n2 - OBJECT TYPES / STATIC VARIABLE" );
        System.out.println( a1 == a2 ); // false
        System.out.println( a1.s == a2.s ); // true
        System.out.println( a1.s.equals( a2.s ) ); // true

        B b1 = new B();
        B b2 = new B();

        System.out.println( "\n3 - OBJECT TYPES / NON-STATIC VARIABLE" );
        System.out.println( b1 == b2 ); // false
        System.out.println( b1.getS() == b2.getS() ); // false
        System.out.println( b1.getS().equals( b2.getS() ) ); // true

final class A
    // static
    public static String s;
        this.s = new String( "aTest" );

final class B
    private String s;
        this.s = new String( "aTest" );
    public String getS()
        return s;

November 10, 2016

Source Not Found .jar has no source attachment

Source Not Found
The JAR file selenium-server-standalone-3.0.1.jar has no source attachment
You can attach the source by clicking Attach Source below.

This in my case was impact of Chrome driver version. I updated it to latest version and scripts starts running smoothly.

November 03, 2016

Benefits of Automation

List of benefits of automation:

1.     Test execution cost is reduced to zero
2.     Test report creation cost is reduced to zero
3.     Reusable: Tests can be re-used on different versions of the software.
4.     Reliable: Tests perform precisely the same operation each time they are run thereby eliminating human error.
5.     Better Quality: More tests can run with few resources in very less time, which ensures the quality of software better.
6.     Test Coverage: Testers can build test suites that covers wider range of application on various versions.
7.     Resources: Reduction in cost of resources with expanding application.
8.     Faster:  Significant reduction in test cycle time

9.     Major component of Continuous Integration(CI) and DevOps

October 17, 2016

Concepts of Cucumber BDD

Cucumber is successfully implemented only if two scenarios are followed religiously

1. Collaboration: Collaboration between business analysts, programmers and testers. In this case all 3 entities will sit together and create Cucumber scenarios
2. Outside-in: Programmers start with functionality that is closest to the user(eg. user interface) and then move to business logic of code, followed by test case execution

Cucumber scenarios should be written even before writing any piece of code, as rightly said " Your cucumber feature should drive your implementation, not reflect it."

In the nut shell, regression suite are the by product of BDD which came out at the end of cycle.

The BDD Given, When, Then syntax is designed to be intuitive. Consider the syntax elements:

    Given provides context for the test scenario about to be executed, such as the point in your application that the test occurs as well as any prerequisite data.
    When specifies the set of actions that triggers the test, such as user or subsystem actions.
    Then specifies the expected result of the test.

Selenium on Windows and Jenkins on Linux

After making the build you are required to trigger local script via ANT or Maven to start selenium script.

But when you want to execute automation script on Windows server rather than Linux/Ubuntu box, you need to connect Jenkins with Windows by using master or slave concept,

On your master machine go to Manage Jenkins > Manage Nodes.

New Node --> Enter Node Name.
Select Dumb Slave --> Press OK.

Fill out the following:
Set a number of executors (one or more) as needed.
Set a Remote FS Root, a home directory for the master on the slave machine.
For a Windows slave, use something like: "C:\Jenkins\"
TODO: add details.
Select the appropriate Usage setting:
For an additional worker: Utilize this slave as much as possible 
For specialized jobs: Leave this machine for tied jobs only
Launch Method:
An easy way to control a Windows slave is by using Launch slave agents via Java Web Start  (Recommended for Windows)
TODO: add steps for other methods.
Availability --> Keep this slave online as much as possible
TODO: add details for each option.
Press OK.

Now you need to connect your slave machine to the master using the following steps.
Open a browser on the slave machine and go to the Jenkins master server url (http://yourjenkinsmaster:8080).
Go to Manage Jenkins > Manage Nodes, Click on the newly created slave machine. You will need to login as someone that has the "Connect" Slave permission if you have configured global security.
Click on the Launch button to launch agent from browser on slave.

Run the program.

If you encounter connection issue, then you could enlarge the popup windows to see the master port used and check your network configuration (firewall, port forward, ...)

Now you should see the Slave machine connected under Nodes.
If you want the service to run on start-up of the slave machine do the following (Windows only directions):
In the Slave agent program running on your slave machine, 
click File --> Install as Windows Service. 

Note that this feature requires ".Net Framework 3.5"

Start, type Services and Select the Services program.
Find Jenkins Slave in the list, Double click to open.
Select Startup type --> Automatic.
Go to the Log On tab, change the Log on as to a user of your choice (Special user account Jenkins recommended).
Make sure that auto login is set for the slave machine for the user account, then the VM (or physical computer) should connect and be available when needed.

September 29, 2016

Testing in Agile vs DevOps


Test in Agile     Testing in Dev Ops
Test as early  and  as often as possible Test Continuously 
Automate testing as mush as possible   Automated almost everything 
Continuous Integration and testing is a step forward Continuous Integration and testing is mandatory
Potentially  shippable code at the end of the sprint Potentially shippable code following every integration 

August 24, 2016

Optimizing Static Content of Website using PageSpeed

Very often customers complain about slow loading of pages despite of good performance from the server. This slowness in most cases is contributed by heavy loading of static content on browser.

A nice way to get to bottom of this issues is to  use PageSpeed Insights tool from google to optimize static contents

It gives list of suggestions to improve performance of the website with few seconds.