org.openqa.selenium.NoSuchWindowException: Unable to find element on closed window

Required Configuration For IE While Working With Selenium

  • The IEDriverServer exectuable must be downloaded and placed in your PATH.
  • On IE 7 or higher on Windows Vista or Windows 7, you must set the Protected Mode settings for each zone to be the same value. The value can be on or off, as long as it is the same for every zone. To set the Protected Mode settings, choose "Internet Options..." from the Tools menu, and click on the Security tab. For each zone, there will be a check box at the bottom of the tab labeled "Enable Protected Mode".
  • Additionally, "Enhanced Protected Mode" must be disabled for IE 10 and higher. This option is found in the Advanced tab of the Internet Options dialog.
  • The browser zoom level must be set to 100% so that the native mouse events can be set to the correct coordinates.
  • For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. For 64-bit Windows installations, the key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE. Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.



Examples :-



For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates. For 32-bit Windows installations, the key you must examine in the registry editor 

This can be done in  2 ways:
Way 1: Setting INITIAL_BROWSER_URL:

File ieFile = new File("D:\\IEDriverServer_x64_2.53.0\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", ieFile.getAbsolutePath());
DesiredCapabilities ieCaps = DesiredCapabilities.internetExplorer();
ieCaps.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.bing.com/");
driver = new InternetExplorerDriver(ieCaps);
//some operations on that site
driver.findElement(By.id("sb_form_q")).clear();
driver.findElement(By.id("sb_form_q")).sendKeys("Ripon Al Wasim");
driver.findElement(By.id("sb_form_go")).click();

Way 2: To set a registry entry on the target computer:
For IE 11 only, you will need to set a registry entry on the target computer so that the driver can maintain a connection to the instance of Internet Explorer it creates.
For 32-bit Windows: The key you must examine in the registry editor is HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE.
For 64-bit Windows: The key is HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BFCACHE.
Please note that the FEATURE_BFCACHE subkey may or may not be present, and should be created if it is not present. Important: Inside this key, create a DWORD value named iexplore.exe with the value of 0.

EXCEL CUSTOM FUNCTIONS FOR JAVA APACHE POI


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;


public class Xls_Reader {

public  String path;
public  FileInputStream fis = null;
public  FileOutputStream fileOut =null;
private XSSFWorkbook workbook = null;
private XSSFSheet sheet = null;
private XSSFRow row   =null;
private XSSFCell cell = null;

public Xls_Reader(String path) {

this.path=path;
try {
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
sheet = workbook.getSheetAt(0);
fis.close();
} catch (Exception e) {

e.printStackTrace();
}

}


// returns the row count in a sheet
public int getRowCount(String sheetName){
int index = workbook.getSheetIndex(sheetName);
if(index==-1)
return 0;
else{
sheet = workbook.getSheetAt(index);
int number=sheet.getLastRowNum()+1;
return number;
}

}

// returns the data from a cell
public String getCellData(String sheetName,String colName,int rowNum){
try{
if(rowNum <=0)
return "";

int index = workbook.getSheetIndex(sheetName);
int col_Num=-1;
if(index==-1)
return "";

sheet = workbook.getSheetAt(index);
row=sheet.getRow(0);
for(int i=0;i if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
col_Num=i;
}
if(col_Num==-1)
return "";

sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum-1);
if(row==null)
return "";
cell = row.getCell(col_Num);

if(cell==null)
return "";

if(cell.getCellType()==Cell.CELL_TYPE_STRING)
return cell.getStringCellValue();
else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){

String cellText  = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {

double d = cell.getNumericCellValue();

Calendar cal =Calendar.getInstance();
cal.setTime(HSSFDateUtil.getJavaDate(d));
cellText =
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
cellText = cal.get(Calendar.DAY_OF_MONTH) + "/" +
cal.get(Calendar.MONTH)+1 + "/" +
cellText;

}

return cellText;
}else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
return "";
else
return String.valueOf(cell.getBooleanCellValue());

}
catch(Exception e){

e.printStackTrace();
return "row "+rowNum+" or column "+colName +" does not exist in xls";
}
}



// returns the data from a cell
public String getCellData(String sheetName,int colNum,int rowNum){
try{
if(rowNum <=0)
return "";

int index = workbook.getSheetIndex(sheetName);

if(index==-1)
return "";


sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum-1);
if(row==null)
return "";
cell = row.getCell(colNum);
if(cell==null)
return "";

if(cell.getCellType()==Cell.CELL_TYPE_STRING)
return cell.getStringCellValue();
else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC || cell.getCellType()==Cell.CELL_TYPE_FORMULA ){

String cellText  = String.valueOf(cell.getNumericCellValue());
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// format in form of M/D/YY
double d = cell.getNumericCellValue();

Calendar cal =Calendar.getInstance();
cal.setTime(HSSFDateUtil.getJavaDate(d));
cellText =
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
cellText = cal.get(Calendar.MONTH)+1 + "/" +
cal.get(Calendar.DAY_OF_MONTH) + "/" +
cellText;

}

return cellText;
}else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
return "";
else
return String.valueOf(cell.getBooleanCellValue());
}
catch(Exception e){

e.printStackTrace();
return "row "+rowNum+" or column "+colNum +" does not exist  in xls";
}
}

// returns true if data is set successfully else false
public boolean setCellData(String sheetName,String colName,int rowNum, String data){
try{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);

if(rowNum<=0)
return false;

int index = workbook.getSheetIndex(sheetName);
int colNum=-1;
if(index==-1)
return false;


sheet = workbook.getSheetAt(index);


row=sheet.getRow(0);
for(int i=0;i if(row.getCell(i).getStringCellValue().trim().equals(colName))
colNum=i;
}
if(colNum==-1)
return false;

sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum-1);
if (row == null)
row = sheet.createRow(rowNum-1);

cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);


cell.setCellValue(data);

fileOut = new FileOutputStream(path);

workbook.write(fileOut);

fileOut.close();

}
catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}



// returns true if data is set successfully else false
public boolean setCellData(String sheetName,String colName,int rowNum, String data,String url){

try{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);

if(rowNum<=0)
return false;

int index = workbook.getSheetIndex(sheetName);
int colNum=-1;
if(index==-1)
return false;


sheet = workbook.getSheetAt(index);

row=sheet.getRow(0);
for(int i=0;i
if(row.getCell(i).getStringCellValue().trim().equalsIgnoreCase(colName))
colNum=i;
}

if(colNum==-1)
return false;
sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum-1);
if (row == null)
row = sheet.createRow(rowNum-1);

cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);

cell.setCellValue(data);
XSSFCreationHelper createHelper = workbook.getCreationHelper();

CellStyle hlink_style = workbook.createCellStyle();
XSSFFont hlink_font = workbook.createFont();
hlink_font.setUnderline(XSSFFont.U_SINGLE);
hlink_font.setColor(IndexedColors.BLUE.getIndex());
hlink_style.setFont(hlink_font);

XSSFHyperlink link = createHelper.createHyperlink(XSSFHyperlink.LINK_FILE);
link.setAddress(url);
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);

fileOut = new FileOutputStream(path);
workbook.write(fileOut);

fileOut.close();

}
catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}



// returns true if sheet is created successfully else false
public boolean addSheet(String  sheetname){

FileOutputStream fileOut;
try {
workbook.createSheet(sheetname);
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();  
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}

// returns true if sheet is removed successfully else false if sheet does not exist
public boolean removeSheet(String sheetName){
int index = workbook.getSheetIndex(sheetName);
if(index==-1)
return false;

FileOutputStream fileOut;
try {
workbook.removeSheetAt(index);
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();  
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
// returns true if column is created successfully
public boolean addColumn(String sheetName,String colName){


try{
fis = new FileInputStream(path);
workbook = new XSSFWorkbook(fis);
int index = workbook.getSheetIndex(sheetName);
if(index==-1)
return false;

XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

sheet=workbook.getSheetAt(index);

row = sheet.getRow(0);
if (row == null)
row = sheet.createRow(0);


if(row.getLastCellNum() == -1)
cell = row.createCell(0);
else
cell = row.createCell(row.getLastCellNum());

cell.setCellValue(colName);
cell.setCellStyle(style);

fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();  

}catch(Exception e){
e.printStackTrace();
return false;
}

return true;


}



// removes a column and all the contents
public boolean removeColumn(String sheetName, int colNum) {
try{
if(!isSheetExist(sheetName))
return false;
fis = new FileInputStream(path);

workbook = new XSSFWorkbook(fis);
sheet=workbook.getSheet(sheetName);
XSSFCellStyle style = workbook.createCellStyle();
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
style.setFillPattern(HSSFCellStyle.NO_FILL);



for(int i =0;i row=sheet.getRow(i);
if(row!=null){
cell=row.getCell(colNum);
if(cell!=null){
cell.setCellStyle(style);
row.removeCell(cell);
}
}
}
fileOut = new FileOutputStream(path);
workbook.write(fileOut);
fileOut.close();
}
catch(Exception e){
e.printStackTrace();
return false;
}
return true;

}


// find whether sheets exists
public boolean isSheetExist(String sheetName){
int index = workbook.getSheetIndex(sheetName);
if(index==-1){
index=workbook.getSheetIndex(sheetName.toUpperCase());
if(index==-1)
return false;
else
return true;
}
else
return true;
}


// returns number of columns in a sheet
public int getColumnCount(String sheetName){
// check if sheet exists
if(!isSheetExist(sheetName))
return -1;

sheet = workbook.getSheet(sheetName);
row = sheet.getRow(0);

if(row==null)
return -1;

return row.getLastCellNum();



}


public boolean addHyperLink(String sheetName,String screenShotColName,String testCaseName,int index,String url,String message){


url=url.replace('\\', '/');
if(!isSheetExist(sheetName))
return false;

sheet = workbook.getSheet(sheetName);

for(int i=2;i<=getRowCount(sheetName);i++){
if(getCellData(sheetName, 0, i).equalsIgnoreCase(testCaseName)){

setCellData(sheetName, screenShotColName, i+index, message,url);
break;
}
}


return true;
}
public int getCellRowNum(String sheetName,String colName,String cellValue){

for(int i=2;i<=getRowCount(sheetName);i++){
if(getCellData(sheetName,colName , i).equalsIgnoreCase(cellValue)){
return i;
}
}
return -1;

}


// to run this on stand alone
public static void main(String arg[]) throws IOException{


Xls_Reader datatable = null;

datatable = new Xls_Reader("D:\\rahul\\testData.xlsx");

for(int col=0 ;col< datatable.getColumnCount("TC5"); col++){
System.out.println(datatable.getCellData("TC5", col, 1));
}
}


}

Day 5: Exception Handling in Java


First of all lets understand why we need exception handling in Java.

See there are many chances that you will get run time error while executing you code and your program terminates abnormally, to avoid these situation java provide us a mechanism which is known as exception handling.

What is exception :- An Exception can be anything which interrupts the normal flow of the program.

In any program exception can occur at run time as well as at compile time.
  • Exception comes at run time, known as run time exception. also known as Unchecked Exception.
  • Exception comes at compile time, known as compile time exception, also known as checked exception.
  Let take and example for compile time exceptions.


As you can see, we are getting an exception over here saying that there might be a chance that file will not found so compiler force us to write the code in try catch block or declare the method using throws keyword.

It means that all exceptions which are reflecting while compilation known as checked exception.

Now let take another example to under what is meant by run time error.


As you can see in the above example we tried to divide a number via zero  and while compilation we did not get to see any exception however when we run the program we get to see that program throws an error message saying that "Exception in thread "main" java.lang.ArithmeticException: / by zero".

So these type of exception which comes after running the program are known as run time exceptions.

Now let see how we can handle checked and unchecked exceptions in java.

There are 5 keywords used in java exception handling.
  • try
  • catch
  • finally
  • throw
  • throws
Note:- Java try block must be followed either by catch or by finally.


Now lets start with try and catch block first.

let me take an example to show you how we can handle this with try and catch block.



now see the below attached screen print after using try and catch block, remember Always remember that a catch block must always be associated with a try bloc, it can't be used on its own.





now lets understand how catch block actually works.


Declaring an instance of type Exception ensures that any type of exception can be trapped using object e, therefor when the type of execution being thrown is not known at design time,the class Exception can be used to trap the error.


and get message is a function of exception class which will provide the message.


NOTE:- We can have multiple catch blocks with one try block.


Now lets understand the use of Finally keyword.


finally block is something which will always be executed whether we get the exception or not.



let take the example.






now lets discussed about the other two keywords i.e. throw and throws.



Throw keyword is always used to throw the exception explicitly.


lets take an example



so i have explicitly created a exception and throw the same.

now let have a look on throws keyword.

look throws keyword can be used instead of try and catch.

however each time when we use the method in some other method then in that method too we need to throws the exception.


lets take an example






now lets handle this exception using throws in main as well.










Day 04:- Object Oriented Programming Continue

DAY 04- Object Oriented Programming Continue

========================================================================
TOPICS

  • Dynamic Binding
  • Abstract Classes
  • Interfaces In Java till Java-7
  • Final Keyword
  • Interface in Java with Java-8
  • Packages
  • Exception handling
========================================================================

DYNAMIC BINDING

The reference of child class object can be hold in the reference variable of parent class this is known as a Dynamic binding.

When  type of a object is determined at compile level then it is known as compile time binding or static binding.

However when type of the object is determined at run-time, it is known as dynamic binding.

Lets understand with the help of example having a child class and a parent class.


When compiler is not able to resolve the call/binding at compile time, such binding is known as Dynamic or late Binding. Overriding is a perfect example of dynamic binding as in overriding both parent and child classes have same method. Thus while calling the overridden method, the compiler gets confused between parent and child class method.

Now lets understand how it works, look show() method is actually called by parent class but since this method is overridden thus JVM run the overridden method  i.e. child class method.

Because if reference of child class object is hold by parent class then only methods which are present in parent class can only be run, with the help of parent we can't call method written in child class except overridden methods.



ABSTRACT CLASS

A class that is declared with abstract keyword, is known as abstract class in java. It can have abstract and non-abstract methods (method with body).

Before learning java abstract class, let's understand the abstraction in java first.

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Examples :-

A.  Bank ATM Screens (Hiding the internal implementation and highlighting set of services like withdraw, money transfer, mobile registration).


B. Mobile phones (The mobile persons are hiding the internal circuit implementation and highlighting touch screen).

C. Syllabus copy (the institutions persons just highlighting the set of contents that persons provided the persons are not highlighting the whole content).


In Java Abstraction can be achieved via 2 ways.

1. Abstract class
2. Interface 

Lets first discussed about the abstract class then will cover the interface part.

Note:- 
  • Abstract classes are declared with abstract keyword.
  • Abstract class may or may not be contain abstract method.
  • Abstract methods do not contain body.
  • If we declare any method as Abstract, then the class must be declared as abstract.
  • child class is responsible to provide the implementation for parent class abstract method.



Now make the class as abstract to remove the error.



let see we can have non abstract methods as well in abstract class


  • Abstract classes can’t be instantiated, because it contains unimplemented methods.


  • Abstract class can have a constructor.

  • To use an abstract class, we must inherit this class from another normal class.
  • The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.


Now let implement the abstract method and see that error will remove.


  • We can’t create a static method as abstract.
  • Abstract method can’t be private.
  • Data members can't be abstract.

Note:- Since abstract classes may or may not contain abstract methods that is the reason abstract class is used to achieve partial abstraction.
INTERFACE IN JAVA

Lets understand why we need interfaces instead of abstract class ?

Since java does not support multiple inheritance we need to go for interface.

Interface in java is like a blue print, it means in interface we can only declare methods we can't define them.

Note:- All methods present in interface are by default abstract and we know that abstract can't have definition, thus we need a class to provide the definition of interface methods.

Note:- To inherit an interface java provided us a keyword known as implement, just like we are implementing the blueprints.

lets take an example.



now create a class to implement the methods of Interface01.



As you can see when we try to implement interface it is a restriction for the class to provide implementation for all the methods present in the class.




As we can see we can call show() method with the help of child class as well as interface reference variable.

This is how a class implements an interface. It has to provide the body of all the methods that are declared in interface.

Note: class implements interface but an interface extends another interface and we can achieve multiple inheritance using interface.

lets take an example




Now let see why java allows us to use multiple inheritance.

Because parent interface does't have any definition, so there is no chance for ambiguity or diamond problem.


So basically there are two benefits of interfaces.

1. It allows us to use multiple inheritance.
2. To achieve abstraction.

Note: 
  • Always remember a interface can't implement another interface.
  • Interface methods are always public.
  • Interface methods are by default abstract. 
  • Interface does not allowed constructor. 
  • When a class implements an interface user should define all the methods of class.
  • When a class implements an interface all the data members are available for class. 
  • The java compiler adds public and abstract keywords before the interface method. More, it adds public, static and final keywords before data members.
  • Interface does not contain main method because main is a static method and static method can't be abstract. 

To understand the data members in Interface lets first take a look for final keyword.


FINAL KEYWORD

Final keyword can be used with Classes, Methods, Variables.

Final keyword is basically use to provide restrictions.



 lets take an example of final variable.



DATA MEMBERS IN INTERFACES

All the data members in interfaces are by default public static and final.

It means the value of data member set in interface can't be change.



Also since variables are final we can't change the value of these variables.

INTERFACE IN JAVA 8

Till java 7 we are not allowed to provide definitions for method which we create in interface, since all the methods are abstract however in java 8 we can provide the definitions for the methods via two ways.

In Java 8 interface changes include static methods and default methods  that means we can create and provide definitions for the methods if they are either static or default.

lets have a look to below mention example for static and default method.


Note:- 

If a class implements two interfaces and both interfaces are having default method then we compiler won't allow us to do the same.

However if a class implements two interfaces and both interfaces are having static method then there would be no compilation error because in this case method can only be accessed with the help of class name.

========================================================================

Links for the files (.Java File)

https://drive.google.com/drive/folders/0B5M4BcbfVN07OEN3UVlUYkRHOTg

Links for recorded Video

https://www.youtube.com/watch?v=wvydHAyFoyc&feature=youtu.be


========================================================================