|
| 1 | +/* |
| 2 | +Copyright 2006-2014 Selenium committers |
| 3 | +Copyright 2006-2014 Software Freedom Conservancy |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | + |
| 19 | +package com.thoughtworks.selenium.webdriven; |
| 20 | + |
| 21 | +/** |
| 22 | + * A utility class, designed to help the user automatically wait until a condition turns true. |
| 23 | + * |
| 24 | + * Use it like this: |
| 25 | + * |
| 26 | + * <p> |
| 27 | + * <code>new Wait("Couldn't find close button!") {<br/> |
| 28 | + * boolean until() {<br/> |
| 29 | + * return selenium.isElementPresent("button_Close");<br/> |
| 30 | + * }<br/> |
| 31 | + * };</code> |
| 32 | + * </p> |
| 33 | + */ |
| 34 | +public abstract class Wait { |
| 35 | + public Wait() { |
| 36 | + } |
| 37 | + |
| 38 | + public Wait(String messageToShowIfTimeout) { |
| 39 | + wait(messageToShowIfTimeout, DEFAULT_TIMEOUT, DEFAULT_INTERVAL); |
| 40 | + } |
| 41 | + |
| 42 | + /** Returns true when it's time to stop waiting */ |
| 43 | + public abstract boolean until(); |
| 44 | + |
| 45 | + /** The amount of time to wait before giving up; the default is 30 seconds */ |
| 46 | + public static final long DEFAULT_TIMEOUT = 30000L; |
| 47 | + |
| 48 | + /** The interval to pause between checking; the default is 500 milliseconds */ |
| 49 | + public static final long DEFAULT_INTERVAL = 500L; |
| 50 | + |
| 51 | + /** |
| 52 | + * Wait until the "until" condition returns true or time runs out. |
| 53 | + * |
| 54 | + * @param message the failure message |
| 55 | + * @throws WaitTimedOutException if "until" doesn't return true until the timeout |
| 56 | + * @see #until() |
| 57 | + */ |
| 58 | + public void wait(String message) { |
| 59 | + wait(message, DEFAULT_TIMEOUT, DEFAULT_INTERVAL); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Wait until the "until" condition returns true or time runs out. |
| 64 | + * |
| 65 | + * @param message the failure message |
| 66 | + * @param timeoutInMilliseconds the amount of time to wait before giving up |
| 67 | + * @throws WaitTimedOutException if "until" doesn't return true until the timeout |
| 68 | + * @see #until() |
| 69 | + */ |
| 70 | + public void wait(String message, long timeoutInMilliseconds) { |
| 71 | + wait(message, timeoutInMilliseconds, DEFAULT_INTERVAL); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Wait until the "until" condition returns true or time runs out. |
| 76 | + * |
| 77 | + * @param message the failure message |
| 78 | + * @param timeoutInMilliseconds the amount of time to wait before giving up |
| 79 | + * @param intervalInMilliseconds the interval to pause between checking "until" |
| 80 | + * @throws WaitTimedOutException if "until" doesn't return true until the timeout |
| 81 | + * @see #until() |
| 82 | + */ |
| 83 | + public void wait(String message, long timeoutInMilliseconds, long intervalInMilliseconds) { |
| 84 | + long start = System.currentTimeMillis(); |
| 85 | + long end = start + timeoutInMilliseconds; |
| 86 | + while (System.currentTimeMillis() < end) { |
| 87 | + if (until()) return; |
| 88 | + try { |
| 89 | + Thread.sleep(intervalInMilliseconds); |
| 90 | + } catch (InterruptedException e) { |
| 91 | + throw new RuntimeException(e); |
| 92 | + } |
| 93 | + } |
| 94 | + throw new WaitTimedOutException(message); |
| 95 | + } |
| 96 | + |
| 97 | + public class WaitTimedOutException extends RuntimeException { |
| 98 | + |
| 99 | + private static final long serialVersionUID = 1L; |
| 100 | + |
| 101 | + public WaitTimedOutException() { |
| 102 | + super(); |
| 103 | + } |
| 104 | + |
| 105 | + public WaitTimedOutException(String message, Throwable cause) { |
| 106 | + super(message, cause); |
| 107 | + } |
| 108 | + |
| 109 | + public WaitTimedOutException(String message) { |
| 110 | + super(message); |
| 111 | + } |
| 112 | + |
| 113 | + public WaitTimedOutException(Throwable cause) { |
| 114 | + super(cause); |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | +} |
0 commit comments