From accc4dc18cd61f4976eb73741f03cae65a0affbb Mon Sep 17 00:00:00 2001 From: gkunze Date: Fri, 18 Jun 2021 16:49:11 +0200 Subject: [PATCH] #148 refactored waitUntilCondition to have maxWaitingTime --- .../neodymium/util/SelenideAddons.java | 76 +++++++++++++++---- .../neodymium/util/SelenideAddonsTest.java | 21 ++--- 2 files changed, 74 insertions(+), 23 deletions(-) diff --git a/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java b/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java index d088f7713..cec27728b 100644 --- a/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java +++ b/src/main/java/com/xceptance/neodymium/util/SelenideAddons.java @@ -512,23 +512,47 @@ public static boolean optionalWaitUntilCondition(SelenideElement element, Condit * the element to match * @param condition * the condition for the element - * @param waitingTime - * the time to wait between retries + * @param maxWaitingTime + * the maximum amount of time to wait * @return if the element did match the condition within the given retries */ - public static boolean optionalWaitUntilCondition(SelenideElement element, Condition condition, long waitingTime) + public static boolean optionalWaitUntilCondition(SelenideElement element, Condition condition, long maxWaitingTime) + { + return optionalWaitUntilCondition(element, condition, maxWaitingTime, + Neodymium.configuration().optionalElementRetryTimeout() * Neodymium.configuration().optionalElementRetryCount()); + } + + /** + * Waits until an optional element matches a condition. This function will return false if the element does not + * match the given condition or can not be found in the given timeframe. + *

+ * The following settings can be configured within the Neodymium configuration to tune the retry behavior: + *

+ * + * + * @param element + * the element to match + * @param condition + * the condition for the element + * @param maxWaitingTime + * the maximum amount of time to wait + * @param pollingInterval + * the amount of time to wait in between retries + * @return if the element did match the condition within the given retries + */ + public static boolean optionalWaitUntilCondition(SelenideElement element, Condition condition, long maxWaitingTime, long pollingInterval) { boolean result = false; - int counter = 0; - while (counter < Neodymium.configuration().optionalElementRetryCount()) + long start = System.currentTimeMillis(); + while (!result && System.currentTimeMillis() - start < maxWaitingTime) { - counter++; if (element.has(condition)) { result = true; - break; } - Selenide.sleep(waitingTime); + Selenide.sleep(pollingInterval); } return result; } @@ -553,7 +577,31 @@ public static boolean optionalWaitUntilCondition(SelenideElement element, Condit */ public static boolean optionalWaitWhileCondition(SelenideElement element, Condition condition) { - return optionalWaitWhileCondition(element, condition, Neodymium.configuration().optionalElementRetryTimeout()); + return optionalWaitWhileCondition(element, condition, + Neodymium.configuration().optionalElementRetryTimeout() * Neodymium.configuration().optionalElementRetryCount()); + } + + /** + * Waits while an optional element matches a condition. This function will return false if the element does match + * the given condition or can not be found after the given timeframe. + *

+ * The following settings can be configured within the Neodymium configuration to tune the retry behavior: + *

+ * + * + * @param element + * the element to match + * @param condition + * the condition for the element + * @param maxWaitingTime + * the maximum amount of time to wait + * @return if the element did stop matching the condition within the given retries + */ + public static boolean optionalWaitWhileCondition(SelenideElement element, Condition condition, long maxWaitingTime) + { + return optionalWaitUntilCondition(element, not(condition), maxWaitingTime); } /** @@ -570,12 +618,14 @@ public static boolean optionalWaitWhileCondition(SelenideElement element, Condit * the element to match * @param condition * the condition for the element - * @param waitingTime - * the time to wait between retries + * @param maxWaitingTime + * the maximum amount of time to wait + * @param pollingInterval + * the amount of time to wait in between retries * @return if the element did stop matching the condition within the given retries */ - public static boolean optionalWaitWhileCondition(SelenideElement element, Condition condition, long waitingTime) + public static boolean optionalWaitWhileCondition(SelenideElement element, Condition condition, long maxWaitingTime, long pollingInterval) { - return optionalWaitUntilCondition(element, not(condition), waitingTime); + return optionalWaitUntilCondition(element, not(condition), maxWaitingTime, pollingInterval); } } diff --git a/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java b/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java index ac4785f97..3bea57cec 100644 --- a/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java +++ b/src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java @@ -642,7 +642,7 @@ public void testOpenHtmlContentWithCurrentWebDriver() Assert.assertEquals(text, $("body").getText()); } - private int customWaitingTime = 3000; + private int customWaitingTime = 10000; @Test public void testOptionalWaitUntil() @@ -662,15 +662,14 @@ public void testOptionalWaitUntil() // check that the is false as is expected Assert.assertFalse("the privacy message dialog was unexpectedly hidden during the timeframe", isHidden); - validateTimeWithinRange(startTime, endTime); + validateTimeWithinRange(startTime, endTime, customWaitingTime); } - private void validateTimeWithinRange(long startTime, long endTime) + private void validateTimeWithinRange(long startTime, long endTime, long maxWaitingTime) { // check that runtime of the wait until method was as long as expected - int waitingTime = customWaitingTime * Neodymium.configuration().optionalElementRetryCount(); - Assert.assertTrue("Runtime was not within the expected range", Range.between(waitingTime, waitingTime + customWaitingTime) - .contains(Math.toIntExact(endTime - startTime))); + Assert.assertTrue("Runtime was not within the expected range", Range.between(maxWaitingTime, maxWaitingTime * 2) + .contains(endTime - startTime)); } @Test @@ -687,7 +686,7 @@ public void testOptionalWaitWhile() // check that the result is false as expected Assert.assertFalse("the privacy message dialog was unexpectedly hidden during the timeframe", isHidden); - validateTimeWithinRange(startTime, endTime); + validateTimeWithinRange(startTime, endTime, customWaitingTime); // click the opt out button privacyDialog.find(".btn-link").click(); @@ -709,13 +708,14 @@ public void testOptionalWaitUntilWithDefaultTimeout() // try wait until the optional privacy dialog is hidden, which will fail long startTime = new Date().getTime(); - boolean isHidden = SelenideAddons.optionalWaitUntilCondition(privacyDialog, hidden, Neodymium.configuration().optionalElementRetryTimeout()); + boolean isHidden = SelenideAddons.optionalWaitUntilCondition(privacyDialog, hidden); long endTime = new Date().getTime(); // check that the is false as is expected Assert.assertFalse("the privacy message dialog was unexpectedly hidden during the timeframe", isHidden); - validateTimeWithinRange(startTime, endTime); + validateTimeWithinRange(startTime, endTime, + Neodymium.configuration().optionalElementRetryCount() * Neodymium.configuration().optionalElementRetryTimeout()); } @Test @@ -732,7 +732,8 @@ public void testOptionalWaitWhileWithDefaultTimeout() // check that the result is false as expected Assert.assertFalse("the privacy message dialog was unexpectedly hidden during the timeframe", isHidden); - validateTimeWithinRange(startTime, endTime); + validateTimeWithinRange(startTime, endTime, + Neodymium.configuration().optionalElementRetryCount() * Neodymium.configuration().optionalElementRetryTimeout()); // click the opt out button privacyDialog.find(".btn-link").click();