Tuesday 6 December 2016

How to launch safari browser in selenium webdriver



Please follow belows steps for launching safari browser in selenium webdriver


We need to install safari driver extension in safari browser. please follow the below steps for launch safari browser.

1) Go to http://docs.seleniumhq.org/download/

2) Scroll down -> Go to the section "SafariDriver" and download "SafariDriver.safariextz"

3) Double click on "SafariDriver.safariextz" (previously downloaded)

4) Safari would open with a pop up containing "Install" button -> Click Install button

5) Now go to Preferences of Safari and you would see WebDriver (in my case WebDriver 2.48.0) is installed (Enable WebDriver checkbox is checked))

6) It's now time to instantiate SafariDriver and get the desired URL by using Java code:

WebDriver driver = new SafariDriver();
driver.get("https://www.google.com");




Error :

If we are not install SafariDriver.safariextz extension in safari browser, we will get the below error in safari browser during the automation

"Unable to establish a connection with the SafariDriver Extension"


Solution : Follow the above steps to get install "SafariDriver.safariextz" extension in safari browser.



Exception :


If we are not install SafariDriver.safariextz extension in safari browser, we will get the below exception in console during the automation

Driver instantiation Exception Remote Client is not accessible



Solution : Follow the above steps to get install "SafariDriver.safariextz" extension in safari browser.

Log4j - How to disable all log details




We can logging the step by step activity during automation. If any instance we want to disable all log details then follow the below steps


1) go to the //log/log4j.properties file

2) And set the log4j.rootLogger=OFF


log4j.rootLogger=OFF


we can set the restriction to display the log messages



1) If log4j.rootLogger= DEBUG

then it display the all level

log.debug(),log.info(), log.warn(),  log.error(), log.fatal();


2)  If log4j.rootLogger= INFO

then it will display

log.info(), log.warn(),  log.error(), log.fatal();


3)  If log4j.rootLogger= WARN

then it will display

log.warn(),  log.error(), log.fatal();


4)  If log4j.rootLogger= WARN

then it will display

log.warn(),  log.error(), log.fatal();


5)  If log4j.rootLogger= ERROR

then it will display

log.error(), log.fatal();


6)  If log4j.rootLogger= FATAL

then it will display

log.fatal();


7)  If log4j.rootLogger= OFF

then it will NOT display



http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/log4j/log4j.html#LOG4J-Basics-Logger


How to convert Java source file to Jar and working with function inside java file.




Type of JAR file

1) Executable JAR
2) Non-Executable JAR


Follow the below steps for convert JAVA source file to Non-Executable JAR file.
------------------------------------------------------------------------------

1) Create java file by using below code


public class SampleCode{



// create new Excel sheet in workbook

public static void createExcelSheet(Workbook wb, String SheetName, String ExcelPath) throws IOException
{
wb.createSheet(SheetName);
FileOutputStream output_file =new FileOutputStream(new File(ExcelPath));
wb.write(output_file);
output_file.flush();
output_file.close();

}


// verify Excel version

public static Workbook verifyExcelVersion(String FilePath) throws FileNotFoundException, IOException
{
Workbook book1=null;
String fileExtn=FilenameUtils.getExtension(FilePath);

if(fileExtn.equals("xls"))
book1=new HSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsx"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
else if(fileExtn.equals("xlsm"))
book1=new XSSFWorkbook(new FileInputStream(FilePath));
return book1;
}




}


2) Save above source file under one package.
Example (Package) -> SampleCode.java

3) Right on  Example (Package) -> Export -> Java -> JAR file -> Click Next

4) In JAR File Specification Window

i)   verify expected Package selected or not.
ii)  select "Export Generated class files and resources".
iii) select "compress the contents of the JAR file".
iv)  provide the path in "Select the export destination"
v)   click Next.

5) In JAR Packaging Options window

i) select "Export class files with compile Errors"
ii) Select "Export class files with compile warnings"
ii) Select "Save the descriptions of this JAR in workspace"
iv) provide the path in "Description file".
v)  click Next

6) In JAR Manifest Specification window

i)   click "Generate the Manifest file".
ii)  click "seal some packages".
iii) click finish

7) Once we cick finsh two files withh get genrated in mentioned path

i) FileName.jar
ii) FileName.jardesc

8) The above JAR is ready to some other project to use existion code. This JAR is called Non Executable JAR file.




How to use and execute function with in JAR file to another package.
-------------------------------------------------------------------

1) create the project

2) Add FileName.jar file in to configure Build path.

3) Then we can use existing functions which available in FileName.jar file.



public static void main(String agrs[])
{

SampleCode obj1=new SampleCode();
Workbook book2=obj1.verifyExcelVersion(ToFilePath);
Sheet sheet2=null;


obj1.createExcelSheet(wb,"Sheet2",Download_FilePath);

}

4) By using above method we can use functions which available in FileName.jar file.

How to get Hashmap key and Hashmap value





By using below below code we can get Hashmap key and Hashmap value


Map,.




for (Map.Entry<String,String> entry : map1.entrySet()) {

System.out.println("HashMap key="+entry.getKey());
System.out.println("HashMap value="+entry.getValue());

}


How to change font color in Excel Sheet by using Apache POI




By using below code we can change the font color in Apache POI



private static void updateColor(String FilePath,int row,int col,String Sheet, String color1) throws FileNotFoundException, IOException {

Workbook book2=verifyExcelVersion(FilePath);
Sheet sheet2=book2.getSheet(Sheet);

// update Font with colors

CellStyle style=book2.createCellStyle();
Font font=book2.createFont();

if(color1.equals("RED"))
font.setColor(IndexedColors.RED.getIndex());
if(color1.equals("GREEN"))
font.setColor(IndexedColors.GREEN.getIndex());
if(color1.equals("BLUE"))
font.setColor(IndexedColors.BLUE.getIndex());

style.setFont(font);


    sheet2.getRow(row).getCell((short) col).setCellStyle(style);
   
     FileOutputStream output_file =new FileOutputStream(new File(FilePath));
     book2.write(output_file);
     output_file.flush();
     output_file.close();
     sheet2=null;
     book2=null;


}

How to display message box in java




We can display the message box during the selenium automation by using Java Swing


public static void main(String args[])
{
msgbox("The verification part is done. We can procced with update the timesheets");
}

private static void msgbox(String string) {
JOptionPane.showMessageDialog(null, string);

}