Skip to content

Commit

Permalink
Add in Supplier method to time limit, allowing function to be execute…
Browse files Browse the repository at this point in the history
…d while supplier is true
  • Loading branch information
Danphillipz committed Aug 16, 2022
1 parent 9671839 commit fb96e75
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
15 changes: 6 additions & 9 deletions src/main/java/com/ensono/playwright/SmartTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,10 +281,8 @@ public SmartElement getColumn(int index) {
public Validate.ValidationResult validateTable(List<Map<String, String>> expectedData, Validate.Method method) {
LinkedList<Map<String, String>> remainingData = new LinkedList<>(expectedData);
if (navigationSet()) navigate().toFirstPageIfSet();
remainingData = validatePage(remainingData, method);
while (!remainingData.isEmpty() && navigationSet() && navigate().toNextPageIfSet()) {
remainingData = validatePage(remainingData, method);
}
do validatePage(remainingData, method);
while (!remainingData.isEmpty() && navigationSet() && navigate().toNextPageIfSet());
return remainingData.isEmpty() ? Validate.ValidationResult.pass() : Validate.ValidationResult.fail("Matches not found for the following data: %s", remainingData.toString());
}

Expand Down Expand Up @@ -650,8 +648,7 @@ public Navigator toFirstPage() {
if (isSet(firstPageLocator)) {
firstPageLocator.click();
} else if (isSet(previousPageLocator)) {
TimeLimit limit = new TimeLimit(timeoutLimit);
while (toPreviousPage() && limit.timeLeftElseThrow());
TimeLimit.of(timeoutLimit).doWhileTrue(()-> toPreviousPage());
} else {
throw new NullPointerException("Neither the 'First' or 'Previous' page locators have been set");
}
Expand Down Expand Up @@ -689,8 +686,7 @@ public Navigator toLastPage() {
if (isSet(lastPageLocator)) {
lastPageLocator.click();
} else if (isSet(nextPageLocator)) {
TimeLimit limit = new TimeLimit(timeoutLimit);
while (toNextPage() && limit.timeLeftElseThrow()) ;
TimeLimit.of(timeoutLimit).doWhileTrue(()-> toNextPage());
} else {
throw new NullPointerException("Neither the 'Last' or 'Next' page locators have been set");
}
Expand Down Expand Up @@ -744,8 +740,9 @@ public Navigator toPage(int page) {
*
* @param timeoutLimit The duration of time to wait by default
*/
public void withTimeoutLimit(Duration timeoutLimit) {
public Navigator withTimeoutLimit(Duration timeoutLimit) {
this.timeoutLimit = timeoutLimit;
return this;
}
}
}
24 changes: 22 additions & 2 deletions src/main/java/com/ensono/utility/TimeLimit.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.function.Supplier;

/**
* Utility class used to set time limits and check if a time limit has been reached
*
*/
public class TimeLimit {

Expand All @@ -15,6 +15,7 @@ public class TimeLimit {

/**
* Create a TimeLimit in seconds
*
* @param seconds - Amount of seconds in this limit
*/
public TimeLimit(int seconds) {
Expand All @@ -24,13 +25,18 @@ public TimeLimit(int seconds) {

/**
* Create a TimeLimit with a specified {@link Duration}
*
* @param time {@link Duration} to set as the limit
*/
public TimeLimit(Duration time) {
this.time = time;
reset();
}

public static TimeLimit of(Duration time) {
return new TimeLimit(time);
}

/**
* Resets the timer
*/
Expand All @@ -40,6 +46,7 @@ public void reset() {

/**
* Checks to see whether the time limit has been reached
*
* @return true if the limit has not yet been reached
*/
public boolean timeLeft() {
Expand All @@ -48,14 +55,27 @@ public boolean timeLeft() {

/**
* Checks to see whether the time limit has been reached, if it has been reached, an {@link TimeLimitReachedError} is thrown
*
* @return true if the limit has been reached
* @throws TimeLimitReachedError if the time limit has been reached
*/
public boolean timeLeftElseThrow() {
if(!timeLeft()) throw new TimeLimitReachedError("The specified time limit of %d seconds has been reached", time.toSeconds());
if (!timeLeft())
throw new TimeLimitReachedError("The specified time limit of %d seconds has been reached", time.toSeconds());
return true;
}

/**
* Given a {@link Supplier} which supplies a Boolean value, this function will continue to invoke the supplier method until it returns false,
* or until the timelimit has been reached where an error will be thrown {@link #timeLeftElseThrow()}
*
* @param method {@link Supplier} which must return a Boolean value. i.e. return true if the method needs to be called again as X process has not finished or reached the desired result
*/
public void doWhileTrue(Supplier<Boolean> method) {
do timeLeftElseThrow();
while (method.get());
}

public static class TimeLimitReachedError extends Error {

public TimeLimitReachedError(String s, Object... format) {
Expand Down

0 comments on commit fb96e75

Please sign in to comment.