Skip to content

Commit

Permalink
Merge pull request #9 from Vertispan/gwt-driver-8
Browse files Browse the repository at this point in the history
gwt-driver-8 - Added overloaded waitFor methods to support passing a timeout message
  • Loading branch information
jhickman authored Feb 16, 2022
2 parents 1c6006c + 0240954 commit 0587c1f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@

import java.time.Duration;
import java.time.temporal.TemporalUnit;
import java.util.function.Supplier;

public class GwtWidgetFinder<W extends GwtWidget<?>> {
public static final Duration DEFAULT_WAITFOR_DURATION = Duration.ofSeconds(10);

protected WebDriver driver;
protected WebElement elt;

Expand Down Expand Up @@ -57,16 +60,33 @@ assert getClass()
}

public W waitFor() {
return waitFor(Duration.ofSeconds(10));
return waitFor((Supplier<String>) null);
}

public W waitFor(String message) {
return waitFor(message == null ? null : () -> message);
}

public W waitFor(Supplier<String> messageSupplier) {
return waitFor(DEFAULT_WAITFOR_DURATION, messageSupplier);
}

public W waitFor(long duration, TemporalUnit unit) {
return waitFor(Duration.of(duration, unit));
}

public W waitFor(Duration duration) {
return waitFor(duration, (Supplier<String>) null);
}

public W waitFor(Duration duration, String message) {
return waitFor(duration, message == null ? null : () -> message);
}

public W waitFor(Duration duration, Supplier<String> messageSupplier) {
return new FluentWait<>(driver)
.withTimeout(duration)
.withMessage(messageSupplier)
.ignoring(NotFoundException.class)
.until((Function<WebDriver, W>) webDriver -> done());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@
import org.junit.jupiter.api.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;

import java.time.Duration;
import java.util.List;

import io.github.bonigarcia.wdm.WebDriverManager;
Expand Down Expand Up @@ -219,4 +221,23 @@ void testFindDescedantWidgets() {
assertEquals(5, elements.size());
System.out.println(elements.size());
}

@Test
void waitTimeout() {
driver.get(url);

WidgetContainer widget = new GwtRootPanel(driver);
assert widget.as(GwtRootPanel.class) != null;

String messageInTimeout = "This will be in the timeout message";

try {
GwtWidget.find(Button.class, driver)
.withText("Test Button Doesnt Exist")
.waitFor(Duration.ofMillis(1), messageInTimeout);
fail("This should fail when it doesn't find the widget");
} catch (TimeoutException e) {
assertTrue(e.getMessage().contains(messageInTimeout));
}
}
}

0 comments on commit 0587c1f

Please sign in to comment.