Java is one of the widely used programming language which is widely compatible with numerous software’s. For a tester or a beginner java developer here is a list simple yet effective programs that will help you boost your confidence.
Must Know Java programs for Intermediate
Getters and Setters
1 2 3 4 5 6 |
public String getusername() { return username; } public void setusername(String username) { this.username = username; } |
Read Properties File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
public class Readproperties { private ReadProperties(){} private static Properties prop = null; public static String read(String key) { if(prop==null) { prop=new Properties(); prop.load(new FileInputStream("Path")); } String value=prop.getProperty(key); return value; } |
Read Web Elements
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static String getpropertyvalue(String key) { if(prop==null) { prop = new Properties(); prop.load(new FileInputStream(ReadConfigureProperties.read("File_Path"))); } } String value=prop.getProperty(key); return value; |
Common Methods
1 2 3 4 5 6 |
public static void clickButtonByID(String key) { WebElement button=driver.findElement(By.id("ID"))); button.click(); } |
Logger
1 2 3 4 5 6 7 8 9 10 11 12 13 |
public static void appendExceptionError(Exception e) { try { FileWriter fstream = new FileWriter(ReadConfigureProperties.read("File_Path"), true); BufferedWriter out = new BufferedWriter(fstream); PrintWriter pWriter = new PrintWriter(out, true); e.printStackTrace(pWriter); } catch (Exception ie) { throw new RuntimeException("Not able to write Exception to file", ie); } } |
Excel Reader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
public SetTestDataColumnNames[] readTestData() throws Exception { File file = new File(filepath); if(!file.exists()) { throw new Exception("File doesnot exist at the mentioned file path."); } else { try { Workbook workbook = WorkbookFactory .create(new FileInputStream(file)); Sheet sheet = workbook.getSheetAt(0); int r = sheet.getLastRowNum(); SetTestDataColumnNames[] recarray = new SetTestDataColumnNames[r + 1]; for (int i = 0; i <= r; i++) { if(i>0) { SetTestDataColumnNames record = new SetTestDataColumnNames(); Row row = sheet.getRow(i); Cell cell0=row.getCell(0); record.setUsername(setCellType(cell0)); Cell cell1=row.getCell(1); recarray[i] = record; } } return recarray; |
Exception Handling
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class ExcepTest{ public static void main(String args[]) { try{ int a[] = new int[2]; System.out.println("Access element three :" + a[3]); } catch(ArrayIndexOutOfBoundsException e)\ { System.out.println("Exception thrown :" + e); } System.out.println("Out of the block"); } } |
PDF Reader
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
try { document = PDDocument.load(f); document.getClass(); if( document.isEncrypted() ) { try { document.decrypt( "" ); } catch ( InvalidPasswordException e ) { System.err.println( "Error: Document is encrypted with a password." ); System.exit( 1 ); } } PDFTextStripperByArea stripper = new PDFTextStripperByArea(); stripper.setSortByPosition( true ); PDFTextStripper stripper = new PDFTextStripper(); String st = stripper.getText(document); System.out.println(st); |
Note: Its just an example and partial code to read PDF files.
Email Agent
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Message message = new MimeMessage(session); message.setFrom(new InternetAddress(ReadConfigureProperties.read("EMAIL_FROM"))); String[] recipients = ReadConfigureProperties.read( "EMAIL_TO" ).split( "," ); InternetAddress[] addressTo = new InternetAddress[recipients.length]; for( int i = 0; i < recipients.length; i++ ) { addressTo[i] = new InternetAddress( recipients[i] ); } message.setRecipients(Message.RecipientType.TO, addressTo); final MimeBodyPart htmlPart = new MimeBodyPart(); htmlPart.setContent(htmlContent(), "text/html"); |
Note: Its just an example and partial code for sending an Email.
Execute Selenium Using Batch FIle
set javaTestProjectPath=D:\workspace\Projet Name Folder
D:
cd %javaTestProjectPath%
set path=C:\Program Files \Java\jdk1.6.0\bin
1 2 3 4 5 6 7 8 9 10 11 |
set classpath=%javaTestProjectPath%\bin;%javaTestProjectPath%\libs\junit-4.5.jar;% (All Jar Files Path) if exist temp.lock ( echo already executing ) else ( echo > temp.lock java Test.Runner echo executed del temp.lock ) pause |
I hope these common Java programs will help you in your day to day life. Please leave us a comment if you see any issues executing and using these most common Java programs.