Skip to content

Commit 15c8fc1

Browse files
authored
Bug/29 fix conditional wait tests (#36)
* #29 specified intervals for conditional wait tests * #29 specified intervals for conditional wait tests * #29 specified intervals for conditional wait tests
1 parent 6a4e8fa commit 15c8fc1

File tree

5 files changed

+108
-93
lines changed

5 files changed

+108
-93
lines changed

src/test/java/tests/waitings/BaseConditionalWaitTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class BaseConditionalWaitTest {
1818
static final long waitForTimeoutCondition = 10;
1919
static final long waitForTimeoutPolling = 150;
20-
static final long accuracy = 3;
20+
static final double accuracy = 0.5;
2121
static final Collection<Class<? extends Throwable>> ignoredExceptions = Collections.singleton(IllegalStateException.class);
2222
ThreadLocal<Timer> timer = ThreadLocal.withInitial(Timer::new);
2323
protected Provider<IApplication> application = AqualityServices.getServiceProvider().getProvider(IApplication.class);

src/test/java/tests/waitings/WaitForObjectTests.java

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

33
import org.openqa.selenium.TimeoutException;
44
import org.openqa.selenium.support.ui.ExpectedCondition;
5+
import org.testng.Assert;
56
import org.testng.annotations.AfterMethod;
67
import org.testng.annotations.BeforeMethod;
78
import org.testng.annotations.DataProvider;
@@ -37,15 +38,16 @@ public Object[][] failWaitForAction() {
3738
}
3839

3940
@Test(dataProvider = "failWaitForAction")
40-
public void testShouldThrowTimeoutExceptionIfConditionIsNotMetAndTimeoutIsOver(Callable failedAction, long timeout) throws Exception {
41+
public void testShouldThrowTimeoutExceptionIfConditionIsNotMetAndTimeoutIsOver(Callable failedAction, long timeout, double pollingInterval) throws Exception {
4142
timer.get().start();
4243
try {
4344
failedAction.call();
45+
Assert.fail("TimeoutException should be thrown but not");
4446
} catch (TimeoutException e) {
4547
double duration = timer.get().stop();
46-
long interval = 2 * timeout + accuracy;
48+
double interval = timeout + pollingInterval / 1000 + accuracy;
4749
assertTrue(duration >= timeout && duration < interval,
48-
String.format("Duration '%s' should be between '%s' and '%s' (timeout and (2*timeout + accuracy)) when condition is not satisfied. ",
50+
String.format("Duration '%s' should be between '%s' and '%s' (timeout and (timeout + pollingInterval + accuracy)) when condition is not satisfied.",
4951
duration, timeout, interval));
5052
}
5153
}
@@ -56,13 +58,14 @@ public Object[][] successWaitForAction() {
5658
}
5759

5860
@Test(dataProvider = "successWaitForAction")
59-
public void testShouldReturnAnObjectIfConditionIsMetAndTimeoutIsNotOver(Callable<String> successAction, long timeout) throws Exception {
61+
public void testShouldReturnAnObjectIfConditionIsMetAndTimeoutIsNotOver(Callable<String> successAction, long timeout, double pollingInterval) throws Exception {
6062
timer.get().start();
6163
String result = successAction.call();
6264
double duration = timer.get().stop();
63-
assertTrue(duration <= timeout,
64-
String.format("Duration '%s' should be less than accuracyTimeout '%s'",
65-
duration, timeout));
65+
double accuracyPollingInterval = pollingInterval / 1000 + accuracy;
66+
assertTrue(duration < accuracyPollingInterval,
67+
String.format("Duration '%s' should be less than accuracy polling interval '%s'",
68+
duration, accuracyPollingInterval));
6669
assertEquals(result, RESULT_STRING, "Method should return correct object");
6770
}
6871

@@ -74,15 +77,17 @@ public Object[][] throwWaitForAction() {
7477
}
7578

7679
@Test(dataProvider = "throwWaitForAction")
77-
public void testShouldThrowException(Callable<String> throwAction, long timeout) throws Exception {
80+
public void testShouldThrowException(Callable<String> throwAction, long timeout, double pollingInterval) throws Exception {
7881
try {
7982
timer.get().start();
8083
throwAction.call();
84+
Assert.fail("IllegalArgumentException should be thrown but not");
8185
} catch (IllegalArgumentException e) {
8286
double duration = timer.get().stop();
83-
assertTrue(duration <= timeout,
84-
String.format("Duration '%s' should be less than accuracyTimeout '%s'",
85-
duration, timeout));
87+
double accuracyPollingInterval = pollingInterval / 1000 + accuracy;
88+
assertTrue(duration < accuracyPollingInterval,
89+
String.format("Duration '%s' should be less than accuracy polling interval '%s'",
90+
duration, accuracyPollingInterval));
8691
assertEquals(e.getMessage(), "I am exception", "It should be custom exception");
8792
}
8893
}
@@ -91,39 +96,39 @@ public void testShouldThrowException(Callable<String> throwAction, long timeout)
9196
public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters() {
9297
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
9398
BooleanSupplier actionWithExceptions = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), ignoredExceptions);
94-
checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition());
99+
checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getPollingInterval());
95100
}
96101

97102
@Test
98103
public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout() {
99104
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
100105
BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), "Condition should be true", ignoredExceptions);
101-
checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition());
106+
checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getPollingInterval());
102107
}
103108

104109
@Test
105110
public void testShouldIgnoreExceptionWaitingWithCustomTimeoutAndExceptions() {
106111
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
107112
BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), waitForTimeoutCondition, waitForTimeoutPolling, ignoredExceptions);
108-
checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutCondition);
113+
checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutPolling);
109114
}
110115

111116
@Test
112117
public void testShouldIgnoreExceptionWaitingWithCustomTimeout() {
113118
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
114119
BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor((driver) -> throwNewException(atomicBoolean).getAsBoolean(), waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", ignoredExceptions);
115-
checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutCondition);
120+
checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutPolling);
116121
}
117122

118-
private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, long timeout) {
119-
long accuracyTimeout = timeout + accuracy;
123+
private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, double pollingInterval) {
120124
timer.get().start();
121125
boolean result = waitAction.getAsBoolean();
122126
double duration = timer.get().stop();
127+
double doubleAccuracyPollingInterval = 2 * pollingInterval / 1000 + accuracy;
123128
assertTrue(result, "waitFor should return true when condition is satisfied.");
124-
assertTrue(duration <= accuracyTimeout,
125-
String.format("Duration '%s' should be less than accuracyTimeout '%s'",
126-
duration, accuracyTimeout));
129+
assertTrue(duration < doubleAccuracyPollingInterval,
130+
String.format("Duration '%s' should be less than double accuracy polling interval '%s'",
131+
duration, doubleAccuracyPollingInterval));
127132
}
128133

129134
private Object[][] getDataProvider(ExpectedCondition<Object> action) {
@@ -136,14 +141,14 @@ private Object[][] getDataProvider(ExpectedCondition<Object> action) {
136141
Callable actionWithCustomTimeoutsAndExceptions = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, Collections.emptyList());
137142
Callable actionWithAllParameters = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", Collections.emptyList());
138143
return new Object[][]{
139-
{onlyAction, timeoutConfiguration.getCondition()},
140-
{actionWithMessage, timeoutConfiguration.getCondition()},
141-
{actionWithExceptions, timeoutConfiguration.getCondition()},
142-
{actionWithMessageAndExceptions, timeoutConfiguration.getCondition()},
143-
{actionWithCustomTimeouts, waitForTimeoutCondition},
144-
{actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition},
145-
{actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition},
146-
{actionWithAllParameters, waitForTimeoutCondition},
144+
{onlyAction, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
145+
{actionWithMessage, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
146+
{actionWithExceptions, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
147+
{actionWithMessageAndExceptions, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
148+
{actionWithCustomTimeouts, waitForTimeoutCondition, waitForTimeoutPolling},
149+
{actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition, waitForTimeoutPolling},
150+
{actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition, waitForTimeoutPolling},
151+
{actionWithAllParameters, waitForTimeoutCondition, waitForTimeoutPolling}
147152
};
148153
}
149154
}

src/test/java/tests/waitings/WaitForTests.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public Object[][] falseWaitForAction() {
1919
}
2020

2121
@Test(dataProvider = "falseWaitForAction")
22-
public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(BooleanSupplier waitAction, long timeout) {
23-
Date startTime = new Date();
22+
public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(BooleanSupplier waitAction, long timeout, double pollingInterval) {
23+
timer.get().start();
2424
boolean result = waitAction.getAsBoolean();
25-
long duration = (new Date().getTime() - startTime.getTime()) / 1000;
26-
long interval = 2 * timeout + accuracy;
25+
double duration = timer.get().stop();
26+
double accuracyPollingInterval = timeout + pollingInterval / 1000 + accuracy;
2727
assertFalse(result, "waitFor should return false when condition is not satisfied.");
28-
assertTrue(duration >= timeout && duration < interval,
29-
String.format("Duration '%s' should be between '%s' and '%s' (timeout and (2*timeout + accuracy)) when condition is not satisfied. ",
30-
duration, timeout, interval));
28+
assertTrue(duration >= timeout && duration < accuracyPollingInterval,
29+
String.format("Duration '%s' should be between '%s' and '%s' (timeout and (timeout + pollingInterval + accuracy)) when condition is not satisfied.",
30+
duration, timeout, accuracyPollingInterval));
3131
}
3232

3333
@DataProvider(name = "trueWaitForAction", parallel = true)
@@ -36,47 +36,51 @@ public Object[][] trueWaitForAction() {
3636
}
3737

3838
@Test(dataProvider = "trueWaitForAction")
39-
public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver(BooleanSupplier waitAction, long timeout) {
40-
checkWaitForMethodForPassedCondition(waitAction, timeout);
39+
public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver(BooleanSupplier waitAction, long timeout, double pollingInterval) {
40+
double checkedTimeout = pollingInterval / 1000 + accuracy;
41+
checkWaitForMethodForPassedCondition(waitAction, checkedTimeout);
4142
}
4243

4344
@Test
4445
public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters() {
4546
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
4647
BooleanSupplier actionWithExceptions = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), ignoredExceptions);
47-
checkWaitForMethodForPassedCondition(actionWithExceptions, timeoutConfiguration.getCondition());
48+
double checkedTimeout = (double) timeoutConfiguration.getPollingInterval() * 2 / 1000 + accuracy;
49+
checkWaitForMethodForPassedCondition(actionWithExceptions, checkedTimeout);
4850
}
4951

5052
@Test
5153
public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout() {
5254
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
5355
BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), "Condition should be true", ignoredExceptions);
54-
checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, timeoutConfiguration.getCondition());
56+
double checkedTimeout = (double) timeoutConfiguration.getPollingInterval() * 2 / 1000 + accuracy;
57+
checkWaitForMethodForPassedCondition(actionWithMessageAndExceptions, checkedTimeout);
5558
}
5659

5760
@Test
5861
public void testShouldIgnoreExceptionWaitingWithCustomTimeoutAndException() {
5962
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
6063
BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), waitForTimeoutCondition, waitForTimeoutPolling, ignoredExceptions);
61-
checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutCondition);
64+
double checkedTimeout = (double) waitForTimeoutPolling * 2 / 1000 + accuracy;
65+
checkWaitForMethodForPassedCondition(actionWithAllParameters, checkedTimeout);
6266
}
6367

6468
@Test
6569
public void testShouldIgnoreExceptionWaitingWithCustomTimeout() {
6670
AtomicBoolean atomicBoolean = new AtomicBoolean(true);
6771
BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor(throwNewException(atomicBoolean), waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", ignoredExceptions);
68-
checkWaitForMethodForPassedCondition(actionWithAllParameters, waitForTimeoutCondition);
72+
double checkedTimeout = (double) waitForTimeoutPolling * 2 / 1000 + accuracy;
73+
checkWaitForMethodForPassedCondition(actionWithAllParameters, checkedTimeout);
6974
}
7075

71-
private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, long timeout) {
72-
long accuracyTimeout = timeout + accuracy;
76+
private void checkWaitForMethodForPassedCondition(BooleanSupplier waitAction, double checkedTimeout) {
7377
timer.get().start();
7478
boolean result = waitAction.getAsBoolean();
7579
double duration = timer.get().stop();
7680
assertTrue(result, "waitFor should return true when condition is satisfied.");
77-
assertTrue(duration <= timeout,
78-
String.format("Duration '%s' should be less than accuracyTimeout '%s'",
79-
duration, accuracyTimeout));
81+
assertTrue(duration < checkedTimeout,
82+
String.format("Duration '%s' should be less than timeout '%s'",
83+
duration, checkedTimeout));
8084
}
8185

8286
@DataProvider(name = "throwExceptionAction", parallel = true)
@@ -88,7 +92,7 @@ public Object[][] throwExceptionAction() {
8892
}
8993

9094
@Test(dataProvider = "throwExceptionAction", expectedExceptions = StaleElementReferenceException.class)
91-
public void testShouldThrowException(BooleanSupplier waitAction, long timeout) {
95+
public void testShouldThrowException(BooleanSupplier waitAction, long timeout, double pollingInterval) {
9296
waitAction.getAsBoolean();
9397
}
9498

@@ -107,14 +111,14 @@ private Object[][] getDataProvider(BooleanSupplier action) {
107111
BooleanSupplier actionWithCustomTimeoutsAndExceptions = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, Collections.emptyList());
108112
BooleanSupplier actionWithAllParameters = () -> conditionalWait.waitFor(action, waitForTimeoutCondition, waitForTimeoutPolling, "Condition should be true", Collections.emptyList());
109113
return new Object[][]{
110-
{onlyAction, timeoutConfiguration.getCondition()},
111-
{actionWithMessage, timeoutConfiguration.getCondition()},
112-
{actionWithExceptions, timeoutConfiguration.getCondition()},
113-
{actionWithMessageAndExceptions, timeoutConfiguration.getCondition()},
114-
{actionWithCustomTimeouts, waitForTimeoutCondition},
115-
{actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition},
116-
{actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition},
117-
{actionWithAllParameters, waitForTimeoutCondition},
114+
{onlyAction, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
115+
{actionWithMessage, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
116+
{actionWithExceptions, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
117+
{actionWithMessageAndExceptions, timeoutConfiguration.getCondition(), timeoutConfiguration.getPollingInterval()},
118+
{actionWithCustomTimeouts, waitForTimeoutCondition, waitForTimeoutPolling},
119+
{actionWithCustomTimeoutsAndMessage, waitForTimeoutCondition, waitForTimeoutPolling},
120+
{actionWithCustomTimeoutsAndExceptions, waitForTimeoutCondition, waitForTimeoutPolling},
121+
{actionWithAllParameters, waitForTimeoutCondition, waitForTimeoutPolling},
118122
};
119123
}
120124
}

0 commit comments

Comments
 (0)