Monday, January 5, 2015

Java: Example of Wait Method in Java

Following is an example to implement Wait Method for predefined seconds in Java. Useful in Selenium, when you don't have control to wait for some element to load in Rich-faces, etc.

 package selenium;  
 import java.util.concurrent.TimeUnit;  
 import org.apache.log4j.Logger;  
 public class WaitMethod  
 {  
   protected void waitForAFewSecond(int timeS)  
   {  
     try  
     {  
       long timeInMS, timeInSec, timeOutMS, timeOutSec;  
       timeInMS = System.currentTimeMillis();  
       timeInSec = TimeUnit.MILLISECONDS.toSeconds(timeInMS);  
       do  
       {  
         timeOutMS = System.currentTimeMillis();  
         timeOutSec = TimeUnit.MILLISECONDS.toSeconds(timeOutMS);  
       }  
       while (timeOutSec - timeInSec < timeS);  
     }  
     catch (Exception e)  
     {  
       System.out.println(e.getMessage());  
     }  
   }  
 }  

Wednesday, November 19, 2014

Python: Find out dates from Last Week.

Following code can be used to findout the Dates of Last week starting from monday
 import time  
 from datetime import date, timedelta  
 def foo(year, week):  
   d = date(year,1,1)  
   d = d - timedelta(d.weekday())  
   dlt = timedelta(days = (week)*7)  
   return d + dlt, d + dlt + timedelta(days=6)  
 todaysDate=time.localtime()  
 weekNumber=time.strftime("%W",todaysDate)  
 yearInTest=time.strftime("%Y",todaysDate)  
 print "Current Week Number is :::" + weekNumber  
 print "Current Year is :::" + yearInTest  
 lastWeeknumber=int(weekNumber)-1  
 print "Last Week Number is :::" + str(lastWeeknumber)  
 yearInTest=int(yearInTest)  
 dateInTest=foo(yearInTest,lastWeeknumber)  
 print str(dateInTest)