Skip to content

Commit

Permalink
#148 refactored waitUntilCondition to have maxWaitingTime
Browse files Browse the repository at this point in the history
  • Loading branch information
gkunze committed Jun 18, 2021
1 parent 16adc6a commit accc4dc
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 23 deletions.
76 changes: 63 additions & 13 deletions src/main/java/com/xceptance/neodymium/util/SelenideAddons.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* The following settings can be configured within the Neodymium configuration to tune the retry behavior:
* </p>
* <ul>
* <li>neodymium.selenideAddons.optional.retry.count (default 5 retries)</li>
* </ul>
*
* @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;
}
Expand All @@ -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.
* <p>
* The following settings can be configured within the Neodymium configuration to tune the retry behavior:
* </p>
* <ul>
* <li>neodymium.selenideAddons.optional.retry.count (default 5 retries)</li>
* </ul>
*
* @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);
}

/**
Expand All @@ -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);
}
}
21 changes: 11 additions & 10 deletions src/test/java/com/xceptance/neodymium/util/SelenideAddonsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ public void testOpenHtmlContentWithCurrentWebDriver()
Assert.assertEquals(text, $("body").getText());
}

private int customWaitingTime = 3000;
private int customWaitingTime = 10000;

@Test
public void testOptionalWaitUntil()
Expand All @@ -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
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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();
Expand Down

0 comments on commit accc4dc

Please sign in to comment.