Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit 5a1c833

Browse files
committed
Extended @Waituntil with the option to configure a custom timeout
1 parent 3cb841d commit 5a1c833

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

webtester-core/src/main/java/info/novatec/testit/webtester/internal/proxies/impls/IdentifyUsingImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.lang.reflect.Method;
44
import java.util.Optional;
5+
import java.util.function.Predicate;
56
import java.util.function.Supplier;
67

78
import org.openqa.selenium.SearchContext;
@@ -87,9 +88,14 @@ private void waitIfAnnotationPresent(Method method, PageFragment fragment) {
8788
}
8889
}
8990

90-
private void doWaitUntil(WaitUntil waitAnnotation, PageFragment fragment) {
91+
private void doWaitUntil(WaitUntil annotation, PageFragment fragment) {
9192
try {
92-
Wait.until(fragment).is(waitAnnotation.value().newInstance());
93+
Predicate<? super PageFragment> condition = annotation.value().newInstance();
94+
if (annotation.timeout() > 0) {
95+
Wait.withTimeoutOf(annotation.timeout(), annotation.unit()).until(fragment).is(condition);
96+
} else {
97+
Wait.until(fragment).is(condition);
98+
}
9399
} catch (InstantiationException | IllegalAccessException e) {
94100
throw new IllegalSignatureException(COULD_NOT_CREATE_PREDICATE_INSTANCE_MSG, e);
95101
}

webtester-core/src/main/java/info/novatec/testit/webtester/pagefragments/annotations/WaitUntil.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
import java.lang.annotation.Retention;
55
import java.lang.annotation.RetentionPolicy;
66
import java.lang.annotation.Target;
7+
import java.util.concurrent.TimeUnit;
78
import java.util.function.Predicate;
89

10+
import info.novatec.testit.webtester.browser.Browser;
911
import info.novatec.testit.webtester.conditions.Conditions;
1012
import info.novatec.testit.webtester.conditions.pagefragments.Disabled;
1113
import info.novatec.testit.webtester.conditions.pagefragments.Editable;
@@ -17,14 +19,16 @@
1719
import info.novatec.testit.webtester.conditions.pagefragments.ReadOnly;
1820
import info.novatec.testit.webtester.conditions.pagefragments.Selected;
1921
import info.novatec.testit.webtester.conditions.pagefragments.Visible;
22+
import info.novatec.testit.webtester.config.Configuration;
2023
import info.novatec.testit.webtester.pagefragments.PageFragment;
2124
import info.novatec.testit.webtester.pages.Page;
2225

2326

2427
/**
2528
* Identification methods of {@link Page pages} and {@link PageFragment page fragments} can be annotation with this
2629
* annotation. With it the framework will wait until the specified {@link #value() predicate} returns true or the timeout is
27-
* reached.
30+
* reached. In case no custom timeout is configured, the defaults of the host {@link Browser browser's} {@link
31+
* Configuration} is used.
2832
* <p>
2933
* <b>Important:</b> The used predicate class must provide a default constructor! Hence not all of our provided {@link
3034
* Conditions} will work. The following conditions can be used:
@@ -63,6 +67,23 @@
6367
*/
6468
Class<? extends Predicate<PageFragment>> value();
6569

66-
// TODO: timeout
70+
/**
71+
* The timeout to use. Defaults to {@code 0} which will signal the framework to use the {@link Browser browser's}
72+
* configured default timeout. The default unit of time is seconds. This can be changed by also setting the {@link
73+
* #unit()} property.
74+
*
75+
* @return the timeout to use
76+
* @since 2.0
77+
*/
78+
int timeout() default 0;
79+
80+
/**
81+
* The {@link TimeUnit} to use for the timeout. This will only be used in case {@link #timeout()} is set to a value
82+
* greater than {@code 0}. The smallest unit that will have an effect is milliseconds!
83+
*
84+
* @return the time unit to use
85+
* @since 2.0
86+
*/
87+
TimeUnit unit() default TimeUnit.SECONDS;
6788

6889
}

webtester-core/src/test/java/features/WaitUntilAnnotationFeatureTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.util.concurrent.TimeUnit;
6+
57
import org.junit.Before;
68
import org.junit.Test;
79

@@ -57,7 +59,7 @@ public interface FeaturePage extends Page {
5759
@IdentifyUsing("#becomesVisible")
5860
Button becomesVisible();
5961

60-
@WaitUntil(Present.class)
62+
@WaitUntil(value = Present.class, timeout = 50, unit = TimeUnit.MILLISECONDS)
6163
@IdentifyUsing("#unknown")
6264
Button neverPresent();
6365

0 commit comments

Comments
 (0)