Sunday, 5 July 2015

Java Details used in reporting results - Selenium

package com.utilities;

import java.net.InetAddress;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class reporters {
    public static String getTime(){
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss a");
        String formatDate = format.format(date);
        return formatDate;
    }   
   
    public static String getDate()
    {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd:MMM:YYYY");
        String formatDate = format.format(date);
        return formatDate;
    }
   
    public static String getTimeStamp()
    {
//        java.util.Date today = new java.util.Date();
//        return new java.sql.Timestamp(today.getTime()).toString();
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd-MMM-YYYY HH:mm:ssss a");
        String formatDate = format.format(date);
        return formatDate;
    }
   
    public static String getDateTime()
    {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("dd:MMM:YYYY HH:mm:ss a");
        String formatDate = format.format(date);
        return formatDate;
    }
   
    public static long currentMilliSeconds()
    {
        return System.currentTimeMillis();
    }
   
    public static void sleep(int seconds)
    {
        try{
            Thread.sleep(seconds);
        }
        catch(Exception oExp)
        {
            System.out.println(oExp.getMessage());
        }
    }
   
    public static String getExecutionTime(long seconds1, long seconds2)
    {   
        long duration = seconds2-seconds1;
        return String.valueOf(TimeUnit.MILLISECONDS.toSeconds(duration));
    }
   
    @SuppressWarnings("finally")
    public static String getIPAddress(){
        InetAddress addr = null;
        try{
            addr = InetAddress.getLocalHost();
            String hostname = addr.getHostName();
            System.out.println("Hostname: "+hostname);
            System.out.println("IP Address: "+addr.getHostAddress());
//            System.out.println("Local Host: "+addr.getLocalHost());
        }
        catch(Exception oExp)
        {
            System.out.println(oExp.getMessage());
        }
        finally{
            return addr.getHostAddress();
        }
    }
   
   
    public static void main(String args[])
    {       
        System.out.println("getTime: "+reporters.getTime());
        System.out.println("getDate: "+reporters.getDate());
        System.out.println("getTimeStamp: "+reporters.getTimeStamp());
        System.out.println("getDateTime: "+reporters.getDateTime());
        long l1= reporters.currentMilliSeconds();
        reporters.sleep(3500);
        long l2 = reporters.currentMilliSeconds();
        System.out.println("getExecutionTime: "+reporters.getExecutionTime(l1, l2));
        System.out.println(reporters.getIPAddress());
    }
   
     
}


getTime: 22:31:43 PM
getDate: 05:Jul:2015
getTimeStamp: 05-Jul-2015 22:31:0043 PM
getDateTime: 05:Jul:2015 22:31:43 PM
getExecutionTime: 3
Hostname: Adi-PC
IP Address: xxx.xxx.x.x
xxx.xxx.x.x

No comments:

Post a Comment