sukhjinderkahlon
sukhjinderkahlon
Reputation Top 1%
Sukhjinder Singh
3+ Year of Experience as a Software Automation Test Engineer with expertise in Java, Selenium and its other Supporting tools.
6 Snippets  (136th place)
Published
1 Channel
Created
2 Channels
Following
6010 points  (20th place)
Reputation
Junior Code Generator
Serious Code Generator
Junior Publisher
Serious Publisher
Junior Influencer
Junior Judge
Junior Popular Coder
Junior Autobiographer
Serious Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by sukhjinderkahlon

public by sukhjinderkahlon  284  3  5  0

Convert Milliseconds to Time hours, minutes and seconds as String.

public class Main {
    public static void main(String[] args) {
        long totalTimeInMils=6003000;
        convertMillisToTime(totalTimeInMils);
        
        //Covert from Dates
        String strStartDate = "2018/08/16 17:10:00";
        String strEndDate = "2018/08/16 17:20:01";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date date = sdf.parse(strStartDate);
        Date endDate = sdf.parse(strEndDate);
        
        long millis = (endDate.getTime()- date.getTime());
        convertMillisToTime(millis);

    }

    public static String convertMillisToTime(long Millis){
        String convert = String.format("Total Time Taken: %d hour(s), %d minute(s), and %d second(s)",
                Millis/(1000*60*60), (Millis%(1000*60*60))/(1000*60), ((Millis%(1000*60*60))%(1000*60))/1000);
        System.out.println(convert);
        return convert;
    }
}
						
;

public by sukhjinderkahlon  3366  1  5  0

Java Property File Reader

This code will read properties files in java.
package Helper.Functions;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class PropertyFileReader {
	public static String GetValue(String key)
	{
		// Read from properties file
		File file = new File("src\\main\\resources\\constants.properties");

		FileInputStream fileInput = null;
		try {
			fileInput = new FileInputStream(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Properties prop = new Properties();

		//load properties file
		try {
			prop.load(fileInput);
		} catch (Exception e) {
			e.printStackTrace();
		}

		//Get Data from File
		String str = prop.getProperty(key);
		// return the value
		return strL;
	}

}
;

public by sukhjinderkahlon  6492  35  6  0

Extent Report For Selenium Java

This code will work with Extent Report Utility for Java, Selenium, TestNG.
//Download Jars from below website
//http://relevantcodes.com/extentreports-for-selenium/
import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import com.relevantcodes.extentreports.DisplayOrder;
import com.relevantcodes.extentreports.ExtentReports;

	public class ReportFactory {
		private static ExtentReports reporter;
		
		
		public static ExtentReports getReporter() {
			if (reporter == null) {
				reporter = new ExtentReports("Report.html", true, DisplayOrder.NEWEST_FIRST);
			}
			return reporter;
		}
				
		
		//Close Reporter
		
		public static void closeReporter() {
			if (reporter != null) {
				reporter.flush();
				reporter.close();
			}
		}
		
		//Capture ScreenShot
		public static String CaptureScreen(WebDriver driver, String ImagesPath)
		{
		    TakesScreenshot oScn = (TakesScreenshot) driver;
		    File oScnShot = oScn.getScreenshotAs(OutputType.FILE);
		 File oDest = new File(ImagesPath+".jpg");
		 try {
		      FileUtils.copyFile(oScnShot, oDest);
		 } catch (IOException e) {System.out.println(e.getMessage());}
		 return ImagesPath+".jpg";
		        }
		
		

	}//End of class



//Using of Extent in TestNG
import org.testng.annotations.AfterSuite;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

import Damco.Project.ReportFactory;



public class NewTest {
	
	
private ExtentReports reporter =  ReportFactory.getReporter();
private boolean status;
	
	@Test
	public void Login()
	{
		ExtentTest testReporter = reporter.startTest(this.getClass().getSimpleName()+ " Login", "This is a simple simpleTest001");
		testReporter.log(LogStatus.INFO, "Starting test simpleTest001");
		int a = 100;
		int b = 30;
		status=false;
		if(status ){
		testReporter.log(LogStatus.INFO, "Loading the value of a to " + a);
		testReporter.log(LogStatus.PASS, "Loading the value of b to " + b);
		
		}
		else{
			testReporter.log(LogStatus.INFO, "Failed to Loading the value of a to " + a);
			testReporter.log(LogStatus.FAIL, "FAILED due some reason" + b);
			
			}
		reporter.endTest(testReporter);
	}

	@Test
	public void simpleTest002()
	{
		ExtentTest testReporter = reporter.startTest("simpleTest002", "This is a simple simpleTest001");
		testReporter.log(LogStatus.INFO, "Starting test simpleTest002");
		int a = 100;
		int b = 30;

		testReporter.log(LogStatus.INFO, "Loading the value of a to " + a);
		testReporter.log(LogStatus.PASS, "Loading the value of b to " + b);
		reporter.endTest(testReporter);
		
	}
	
	@Test
	public void Register() throws Exception
	{
		ExtentTest testReporter = reporter.startTest("Register", "This is a simple simpleTest001");
		testReporter.log(LogStatus.INFO, "Starting test simpleTest002");
		int a = 100;
		int b = 30;

		testReporter.log(LogStatus.INFO, "Loading the value of a to " + a);
		testReporter.log(LogStatus.PASS, "Loading the value of b to " + b);
		reporter.endTest(testReporter);
		

		
		
	}
	
	
	@AfterSuite
	public void afterSuite()
	{
		reporter.close();
	}
	
	
}


;

public by sukhjinderkahlon  326312  7  5  0

How to Work with Excel Files Using Apache POI

This code will work as a utility to read and write excel files using Apache POI Library. 3.13
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtils {

	private static HSSFSheet ExcelWSheet;
	private static HSSFWorkbook ExcelWBook;
	private static HSSFCell Cell;

	private static HSSFRow Row;
	
	private static String filePath;
	public static void setExcelFile(String Path,String SheetName) throws Exception {

			try {

   			// Open the Excel file
				filePath=Path;

			FileInputStream ExcelFile = new FileInputStream(Path);

			// Access the required test data sheet

			ExcelWBook = new HSSFWorkbook(ExcelFile);

			ExcelWSheet = ExcelWBook.getSheet(SheetName);
			

			} catch (Exception e){

				throw (e);

			}

	}

	//This method is to read the test data from the Excel cell, in this we are passing parameters as Row num and Col num

    public static String getCellData(int RowNum, int ColNum) throws Exception{

			try{

  			Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);

  			String CellData = Cell.getStringCellValue();

  			return CellData;

  			}catch (Exception e){

				return"";

  			}

    }


    
  //This method is to write in the Excel cell, Row num and Col num are the parameters

	public static void setCellData(String Result,  int RowNum, int ColNum) throws Exception	{

			try{

  			Row  = ExcelWSheet.getRow(RowNum);

			Cell = Row.getCell(ColNum, Row.RETURN_BLANK_AS_NULL);

			if (Cell == null) {

				Cell = Row.createCell(ColNum);

				Cell.setCellValue(Result);

				} else {

					Cell.setCellValue(Result);

				}

  // Constant variables Test Data path and Test Data file name

  				FileOutputStream fileOut = new FileOutputStream(filePath);

  				ExcelWBook.write(fileOut);

  				fileOut.flush();

					fileOut.close();

				}catch(Exception e){

					throw (e);

			}

		}

	
}
          
;

public by sukhjinderkahlon  4505  2  6  1

Selenium FluentWait

// This method will return WebElement After Given wait           
public static WebElement FindWithWait(WebDriver driver,By locator, int seconds) throws Exception{
		// Sleep until the Element we want is visible or n seconds is over
		WebElement element=null;
		
		//Because if implict wait is set then fluint wait will not work
		driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
		try{
					FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
							.withTimeout(seconds, TimeUnit.SECONDS)
							.pollingEvery(200, TimeUnit.MILLISECONDS)
							.ignoring(NoSuchElementException.class)
							.ignoring(StaleElementReferenceException.class)
							.ignoring(WebDriverException.class);
									 
				element=fluentWait.until(
							
							ExpectedConditions.visibilityOfElementLocated(locator)
							
							);
				return element;
					
		} catch(Exception e){
			
			throw new Exception("Timeout reached when searching for element! Time: " + seconds+" seconds " +"\n" +e.getMessage());
			
		}
		finally {
			driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
		}
		
	}

	            
;