Monday, 24 November 2014

Creating Hierarchy of Packages in Selenium

We can create packages at different hierarchy level (package and its sub-package etc). Lets create the packages at different hierarchy levels as shown below -
Lets implement this on Eclipse IDE -
1. Launch Eclipse IDE and create a Java Project ‘Project 41′ as shown below -
2. Now lets change the display mode of the package hierarchy by following the below steps.
3. Select the ‘view’ menu icon of the ‘Package Explorer’ tab as shown  below -
4. In ‘View Menu’ options change the Package Presentation option from Flat to Hierarchical as shown below -
Now with this step, the package display mode got changed to hierarchical mode. Lets create hierarchy of packages by following the below steps.
5. Create two packages ‘package1′ and ‘package2′ as shown below -
Now lets create the packages ‘packageA’ and ‘packageB’ as sub-packages under ‘package2′ by following the below steps.
6. Now right click on the ‘package2′ and select ‘New’ -> ‘Package’ from the right click options as shown below -
7. In New Package dialog, enter the package name as package2.packageA and click on ‘Finish’ button  as shown below -
8. Observe that the package ‘package2′ got changed to ‘package2.packageA’ as shown below -
When you create a single sub-package, the original package will get replaced with a single package ‘package.subpackage’. But when you create more than one sub-packages for a package, you can clearly see the hierarchy. Please follow the below steps to create another sub-package for ‘package2′.
9. Right click on the ‘package2.packageA’ package and select ‘New’ -> ‘Package’ from the right click options as shown below -
10. Ensure that the ‘New Java Package’ dialog is displayed with ‘package2.packageA’ in the name field as by default as shown below -
11. Change the packageA to packageB in the default displayed name and click on ‘Finish’ button as shown below -
12. Observe that the two sub-packages ‘packageA’  and ‘packageB’ are displayed under the ‘package2′ package as shown below -
This is how we create packages at different hierarchy levels and view them in the Hierarchical package display mode.
Now lets find out the package statements created in the Class files which are created under the package which is not having any sub-pakcage by following the below steps.
13. Create a Class ‘ClassOne’ under ‘package1′ page and observe that the package statement is displayed in the Class file as shown below -
Hence when you create a Class file under a package directly, the package statement syntax will be package packageName; 
Now lets create a Class file under a sub-package ‘packgeB’ of ‘package2′ package by following the below steps.
14. Create a Class ‘ClassX’ under ‘packageB’ of ‘package2′ and observe that the package statement is displayed in the Class file as shown below -
Hence when you create a Class file under a sub-package of any package, the package statement syntax will be package packageName.subPackageName; 
 Now lets change the package view from Hierarchical mode, back to the Flat mode by following the below steps.
15. Select ‘Flat’ option from the ‘View Menu’ options as shown below -
16. Observe that the hierarchical mode got changed to the Flat mode as shown below -
When you create a package in Eclipse IDE, a folder will be created in the workspace in your computer (i.e. Inside Eclipse IDE Workspace -> Project folder). Now lets view the workspace of this project to verify the same by following the below steps.
17. Right Click on the Project, select ‘Properties’ option and view the location of your project on your computer as shown below -
18. Navigate to the location on your computer and view the Project files as shown below -
19. Open the ‘src’ folder and observe that the Packages ‘package1′ and ‘package2′ are displayed as folder as shown below -
20. Open the ‘package2′ folder and observe that ‘packageA’ and ‘packageB’ sub folder for the sup-packages in Eclipse IDE are displayed as shown below -
21. Open the ‘packageB’ sub-folder and observe that ClassX created under the sub-package ‘packageB’ is displayed as shown below -

How to Rename the Packages in Hierarchy - QuontraSolutions

we can rename the packages which are not in hierarchy without any problems. But when you want to rename a package which has sub-packages under it, it is not possible to do it from Eclipse IDE. Instead we have to go to the Project location on your computer, rename the folder with package name outside Eclipse IDE and refresh the Project in Eclipse IDE.
Lets implement this on Eclipse IDE -
1. Launch Eclipse, Create a Java Project ‘Project 42′ as shown below -
Now lets create a package under Project 42 and rename it by following the below steps. 2. Create a Package ‘packageA’ as shown below -
3. Now Right click on the Package and select ‘Refactor’ -> ‘Rename’ option as shown below -
4. Observe that ‘Rename Package’ dialog is displayed with the ‘packageA’ name as shown below -
5. Now change the package name in the ‘New Name’ field from ‘packageA’ to ‘packageX’ and click on ‘OK’ button as shown below -
6. Observe that the package name got changed from ‘packageA’ to ‘packageX’ as shown below -
Now lets create sub-packages under the package ‘packageX’ and try to rename it by following the below steps. 7. Create two packages package1 & package2 under ‘packageX’ as sub-packages to it as shown below -
In Eclipse IDE, rename the package ‘packageX’ , which is having two sub-packages ‘package1′ and ‘package2′ by following the below steps. 8. Right click on the ‘packageX’ and select ‘Refactor’ -> ‘Rename’ option as shown below -
9. Observe that ‘Rename Pakcage’ dialog is displayed with ‘packageX’ name as shown below -
10. Change the package name displayed in the ‘New Name’ field from ‘packageX’ to ‘packageG’ and click on ‘OK’ button as shown below -
11. Observe that a new package ‘packageG’ got added instead of renaming the ‘packageX’ to ‘packageG’ as shown below -
Hence we are not able to rename the package having the sub-packages using Eclipse IDE. So renaming the packages having the sub-packages from Eclipse IDE is not possible. Now lets find out the project location and change the package folder name to change it by following the below steps. 12. Right click on the Project and select ‘Properties’ option as shown below -
13. In the ‘Properties for Project 42′ dialog, select ‘Resources’ option from the left and view the location of the project on your computer as shown below -
14. Go to the Project location on your computer and open the ‘src’ folder to view the package folder ‘packageX’ inside it as shown below -
15. Rename the ‘packageX’ folder manually to ‘packageM’ as shown below -
16. Observe that the package name is still not changed in the Eclipse IDE as shown below -
Package name wont be changed in Eclipse IDE until you refresh the Project. Refresh the project by following the below steps. 17. Right click on the Project in Eclipse IDE and select ‘Refresh’ option as shown below -
18. Observe that the package name got changed from ‘packageX’ to ‘packageM’ in Eclipse IDE as shown below -
Hence renaming the package having the sub-packages from Eclipse IDE is not possible. In order to rename, we have to change the name of the package folder from the Project location in our computer and refresh the Project in Eclipse IDE to see the changes. Can we rename any package having Class files under it from Eclipse IDE ?
Yes, we can rename the package name in Eclipse which has any Class files under it. But we cannot rename the package name in Eclipse IDE if it has any sub-packages under it.
Lets implement this on Eclipse IDE -
1. In Eclipse IDE, create two Class files ‘ClassOne’ & ‘ClassTwo’ under the package ‘packageG’ in the existing Java Project ‘Project 42′ as shown below -
2. Right click on the ‘packageG’ which is having two Classes ‘ClassOne’ & ‘ClassTwo’ and select ‘Refactor’ -> ‘Rename’ option as shown below -
3. Observe the ‘Rename Package’ dialog is displayed with ‘packageG’ name as shown below -
4. Change the name of the package in ‘New Name’ field from ‘packageG’ to ‘packageS’ and click on ‘OK’ button as shown below -
5. Observe that the package name got changed from packageG to packageS without any problems in Eclipse IDE as shown below -
Hence we can renaming the packages having Class under it from Eclipse IDE without any problems.


Exception is an Object - Selenium

A Java exception is an object that describes an exceptional condition (i.e. error) that has occurred in the piece of code. When an exceptional condition arises, an object representing that condition is created and thrown.
What happens when we don’t handle that exception ?
The program execution will stop at the line of the code where the exceptional condition (i.e. error) got occurred.
How does the program execution stops when exceptional condition got occurred ?
As our program, don’t have any code to handle the occurred exceptional condition (i.e. error), JVM stops our program execution and prints what when wrong using the thrown exception (i.e object).
How to handle the exception ?
We have candle the exception using the try{ } catch{ } blocks.
What happens when we use the try{ } catch{ } blocks to handle the exceptions ?
When an exception occurs in try { } block,  instead of passing the exception (i.e. object) to JVM, the try{} block will pass it to its associated catch{ } block where we can write our code to handle the occurred exceptional condition. catch{ } block catches the exception (i.e. object) that describes the occurred exceptional condition in the program code of the try{ } block and executes the program code that is written in its block to handle the occurred exception condition (i.e. error).
Lets understand it using the below code -
try
{
    int a = 1/0;  //Gives Arithmetic exception
}
catch(Exception e)   //Here e is an object of Exception Class type which receives the exception (i.e. object) that is occurred in and passed by try block.
{
     System.out.println(” 1/0  is not a valid statement “);  //This statement will be printed and the program execution will continue
Lets understand the above code step by step -
1. int a = 1/0;  statement gives divide by zero error and hence throws an exception (i.e. object). But as we have provided the above statement in the try{ } block, instead of passing the exception (i.e. object) to the JVM and terminating the program, try{ } block will pass the exception (i.e. object) to its corresponding catch { } block where we have written the code to handle the exception (i.e. object) to overcome its exceptional condition (i.e. occurred error).
2. catch(Exception e) -> catch block catches the exception(i.e. object) passed by the try{ } block and assigns it to the Exception Class type object ‘e’. You can create Exception Class type object with any name. Lets say, If I want to give the object name as  ‘object1′ instead of ‘e’ , I can as -> catch(Exception object1). And also Exception is a Parent Class of all its Sub-Classes which handles different types of  Exceptions.
In this example, we are using Exception Class i.e parent class, as we know that arithmetic exception is thrown by the statement in the try{ } block, we can use the Class which can receive the exception directly instead of up-casting the received object to the Exception Parent Class and using it. So in order to deal with the exception (i.e. object) directly we can modify the statement as -> catch(ArithmeticException object1). I will explain the Exception Hierarchy in our upcoming posts to explain the parent classes and child classes which are used in catch{ } blocks to handle the exceptions.
3. No programmer will intentionally write the statement int a = 1/0; in his program code. I have intentionally used this statement to make the program throw an exception in this example. But in real time, when a User runs the program created by a programmer, where in the code, the programmer has specified the statement as int c = a/b; and while the User is executing the program created by the programmer, the User may be asked to input a value for a & b variables, and unknowing the User inputs the a variable as 1 and b variable as 0.
The exception will occur in the statement int a=b/c and the program created by the programmer may not be run properly at the User end. In order to avoid this kind of situations, the programmer will handle the exceptions in his code by printing the message ‘0 is not valid’ at User end. When User runs the properly handled code at his end, and in case he enter ‘0’ for b variable, the executing program asks the User not to enter the value ‘0’ for the variable b.