23
Oct

custom wait in Selenium

{
    public void wait(String message, long timeoutInMilliseconds, long intervalInMilliseconds) {
        long start = System.currentTimeMillis();
        long end = start + timeoutInMilliseconds;
        while (System.currentTimeMillis() < end) {
            if (until()) return;
            try {
                Thread.sleep(intervalInMilliseconds);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        throw new WaitTimedOutException(message);
    }
    
    public class WaitTimedOutException extends RuntimeException {

        private static final long serialVersionUID = 1L;

        public WaitTimedOutException() {
            super();
        }

        public WaitTimedOutException(String message, Throwable cause) {
            super(message, cause);
        }

        public WaitTimedOutException(String message) {
            super(message);
        }

        public WaitTimedOutException(Throwable cause) {
            super(cause);
        }
        
    }