Sunday 26 October 2014

Parameterizing Tests in Junit Selenium

Parameterizing Tests :
Parameterizing tests in Junit selenium is a flexible thing. When we need test data to be supplied from outside not to be hard-coded then here we use parameterizing tests.

Parametrizing tests needs 4 steps :
Step 1:
Standard annotation should be written above the class
@RunWith(Paramterized.class)

Step 2:
Declare some global variables which will be the input data to the function in test.

Step 3:
Build constructor of that class, pass variables inside it to initialize the class with those declared global variables means whenever the object of the class is made then the object will have the passed parameters.

Step 4:
Make another annotation known as @Parameters, it should be static method.

Here is one parameterized test showing all steps :
import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

//step 1
@RunWith(Parameterized.class)
public class SecondTest {
   
    //step 2
    public String bankName;
    public int actNum;
   
    //step 3
    public SecondTest(String bankName,int actNum){ //here , whenever the object of class is made,then the object will have a bankName and actNum.
        this.bankName=bankName;
        this.actNum=actNum;
        //here, this keyword is used to distinguish between the local & global variable, it means here we are initializing the global variable to local variable.
      }
   
    @Test
    public void checkBalance(){
       
        /*String bnk="citi";
        int act=1233;*/
       
        //bnk name
        //act number
       
        System.out.println("Executing the test with "+ bankName+ "--" +actNum);
    }
   
    //step 4
    @Parameters
    public static Collection<Object[]> getData(){ //here Collection<Object[]> is the return type
        Object data[][] = new Object[2][2];
       
        //first row
        data[0][0] = "HSBC";
        data[0][1] = 1234;
       
        //second row
        data[1][0] = "Citi";
        data[1][1] = 123411;       
               
        return Arrays.asList(data); //It will convert the 2-Dimensional array into arraylist
        //returning the object of arraylist over here and here arraylist is of type collection
        //Arrays-inbuilt class
        //aslist - static function
    }

}



Wednesday 15 October 2014

Features & Shortcuts of Eclipse

Here are some basic features & shortcuts of Eclipse :

1) Going to the class where function is present from a different class
Hold on CNTRL button , you can see everything will show you as links. On clicking these links,it will take you to that class where function body is present.

2)Going to the page from where you navigated to other page



If you want to go back to the place from where you came, then click on BACK button present.

3)Searching a file



 Click on CNTRL  + SHIFT + R , a window will come, type the file name there, then it will come up and double click on it and that file will come.

4)Highlighting file name in Project Explorer menu



Click on ARROW LINK EDITOR from the open file in current tab, then moving mouse to any class, it will highlight it in the Project Explorer left menu.

5)Opening Project Explorer or Console 


If you the Project Explorer or Console, click on a small icon present on the buttom left corner, right click on it, uncheck the fast view, then it will come back.
You can also drag windows.

6)Changing Font & all 
For changing font or text size, go to Windows->Preferences->General->Appearences->Colours and Fonts->Basic->Text Font->Edit.

7)Renaming the Class Name
For renaming a class name, click on the class , press  F2 button . Press F2 on any class to rename the class name.
OR
Another way of renaming the class name, in Class -> Rename the class_name -> Hover the mouse on the updated class_name -> Click on Rename compilation unit to updated class.java.

8)Shortcut for print statement
For writing System.Out.Println, there is a shortcut type - syso + CNTRL + SPACE.  

9)Commenting single & multiple lines
For commenting single line, put double slash(//) before the starting of that line and
For commenting multiple lines, put slash(/*) before the starting of that line and after the ending of the lines.

10)Tips for debugging java code in eclipse
Go through this link, http://soumyam1.blogspot.com/2014/09/debugging-java-code-in-eclipse.html.

11)How to get the project location path
Go to Project -> Right click -> Properties, there you can get the project location, the path where you had told eclipse to store the Java code.

12)Tip while naming a Java class & Java Project
Make sure the Java class has no space,no numbers,no special characters and if you want numbers to include in the class name then you can add numbers in the end of the name.
 Make sure the Java project has no special characters, if we are giving special characters then eclipse will throw you an error. And name given for Java project or class should be a logical one.

13) Creating Java Project
For creating Java project, Eclipse -> Project Explorer -> Right click -> New -> Project -> Java Project -> Next -> Project_Name -> Next -> Finish .
If a popup[do you want to open perspective now?] pops up then click on NO.

14)Creating Java Class 
For creating a Java class, Project -> src -> Right click -> New Class -> Class_Name, then check public static void main check-box -> Finish .
Java class means a simple java file or .java extension file which is also know as Java Class File.

 15)How to run the Java program 


There is one run button on the top, to run the program after clicking on that button output will show in the console tab below.
 OR 
There is another way for running the Java program, Java class -> Right click -> Run as -> Java application.
  
16)Creating Package 
For creating package in eclipse, src -> Right click -> New -> Package.
OR
While creating java class, we can create package & class at a time.  

17)Specifying path in Java
In Java, we need to give  double slash (\\) not single slash (\) while writing path in eclipse.

18)Creating Properties File
For creating properties file, go to Package -> Right Click -> New -> Other -> General -> File -> Next -> File_Name -> Finish . Eg. file_name - Employee.Properties, in Properties file, you can specify the properties of the file.

19)Commenting a line in Properties File
If you want to delete a line from Properties file and you may need it that line in future, then put a hash(#) in-front of it rather deleting the line.Here, hash(#) will comment-out that line in the Properties file. 

20)Statement to print Current Project Directory Path
System.out.println(System.getProperty("user.dir")); , this will give you the current project in which you are in or the current project directory path.

21)How to show all methods & global Variables in a file
Press Cntrl + O, a window will come which will show all the global variables and functions inside it.

22)Some Basic Tips while reading & writing data in excel sheet
When reading data from xlsx file, it is not mandatory to close the xlsx file even if it is opened you can able to read & in order to write data in excel sheet, you need to close the sheet.

23)How to run Junit code
For running Junit code, click on run button -> select Junit Test -> ok .Here, the function name should be having 'test' at beginning or at middle or at end .It is not a standard but a requirement by Junit.
 
24)How to know TestNg is installed in eclipse or not 
For knowing TestNg is installed, go to windows in top bar of eclipse -> Show View -> Other -> Expand the Java folder -> you will find there is a TestNg option, that means TestNg is successfully installed in eclipse as a plug-in.




25)Advantage of TestNg
Right click on the project folder -> refresh, you will automatically get a test.output folder created, expand that folder you will find index.html -> go to its physical location, you will find the html report. So, automatically html reports are generated by TestNg but in Junit you have to run with ANT to get the html report. Here, reports are refreshed every time and the old reports are lost. So, you should keep backup of old reports.

26)Creating a TestNg Class
For creating a TestNg class, go to src -> Right Click -> New -> Other -> TestNg -> TestNg Class -> Next, a window will come in which browse the source folder, give the package_name & class_name, check if any annotations required -> Finish.