2
2
3
3
import org .openqa .selenium .TimeoutException ;
4
4
import org .openqa .selenium .support .ui .ExpectedCondition ;
5
+ import org .testng .Assert ;
5
6
import org .testng .annotations .AfterMethod ;
6
7
import org .testng .annotations .BeforeMethod ;
7
8
import org .testng .annotations .DataProvider ;
@@ -37,15 +38,16 @@ public Object[][] failWaitForAction() {
37
38
}
38
39
39
40
@ 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 {
41
42
timer .get ().start ();
42
43
try {
43
44
failedAction .call ();
45
+ Assert .fail ("TimeoutException should be thrown but not" );
44
46
} catch (TimeoutException e ) {
45
47
double duration = timer .get ().stop ();
46
- long interval = 2 * timeout + accuracy ;
48
+ double interval = timeout + pollingInterval / 1000 + accuracy ;
47
49
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." ,
49
51
duration , timeout , interval ));
50
52
}
51
53
}
@@ -56,13 +58,14 @@ public Object[][] successWaitForAction() {
56
58
}
57
59
58
60
@ 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 {
60
62
timer .get ().start ();
61
63
String result = successAction .call ();
62
64
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 ));
66
69
assertEquals (result , RESULT_STRING , "Method should return correct object" );
67
70
}
68
71
@@ -74,15 +77,17 @@ public Object[][] throwWaitForAction() {
74
77
}
75
78
76
79
@ 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 {
78
81
try {
79
82
timer .get ().start ();
80
83
throwAction .call ();
84
+ Assert .fail ("IllegalArgumentException should be thrown but not" );
81
85
} catch (IllegalArgumentException e ) {
82
86
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 ));
86
91
assertEquals (e .getMessage (), "I am exception" , "It should be custom exception" );
87
92
}
88
93
}
@@ -91,39 +96,39 @@ public void testShouldThrowException(Callable<String> throwAction, long timeout)
91
96
public void testShouldIgnoreExceptionForWaitingWithoutCustomParameters () {
92
97
AtomicBoolean atomicBoolean = new AtomicBoolean (true );
93
98
BooleanSupplier actionWithExceptions = () -> conditionalWait .waitFor ((driver ) -> throwNewException (atomicBoolean ).getAsBoolean (), ignoredExceptions );
94
- checkWaitForMethodForPassedCondition (actionWithExceptions , timeoutConfiguration .getCondition ());
99
+ checkWaitForMethodForPassedCondition (actionWithExceptions , timeoutConfiguration .getPollingInterval ());
95
100
}
96
101
97
102
@ Test
98
103
public void testShouldIgnoreExceptionForWaitingWithDefaultTimeout () {
99
104
AtomicBoolean atomicBoolean = new AtomicBoolean (true );
100
105
BooleanSupplier actionWithMessageAndExceptions = () -> conditionalWait .waitFor ((driver ) -> throwNewException (atomicBoolean ).getAsBoolean (), "Condition should be true" , ignoredExceptions );
101
- checkWaitForMethodForPassedCondition (actionWithMessageAndExceptions , timeoutConfiguration .getCondition ());
106
+ checkWaitForMethodForPassedCondition (actionWithMessageAndExceptions , timeoutConfiguration .getPollingInterval ());
102
107
}
103
108
104
109
@ Test
105
110
public void testShouldIgnoreExceptionWaitingWithCustomTimeoutAndExceptions () {
106
111
AtomicBoolean atomicBoolean = new AtomicBoolean (true );
107
112
BooleanSupplier actionWithAllParameters = () -> conditionalWait .waitFor ((driver ) -> throwNewException (atomicBoolean ).getAsBoolean (), waitForTimeoutCondition , waitForTimeoutPolling , ignoredExceptions );
108
- checkWaitForMethodForPassedCondition (actionWithAllParameters , waitForTimeoutCondition );
113
+ checkWaitForMethodForPassedCondition (actionWithAllParameters , waitForTimeoutPolling );
109
114
}
110
115
111
116
@ Test
112
117
public void testShouldIgnoreExceptionWaitingWithCustomTimeout () {
113
118
AtomicBoolean atomicBoolean = new AtomicBoolean (true );
114
119
BooleanSupplier actionWithAllParameters = () -> conditionalWait .waitFor ((driver ) -> throwNewException (atomicBoolean ).getAsBoolean (), waitForTimeoutCondition , waitForTimeoutPolling , "Condition should be true" , ignoredExceptions );
115
- checkWaitForMethodForPassedCondition (actionWithAllParameters , waitForTimeoutCondition );
120
+ checkWaitForMethodForPassedCondition (actionWithAllParameters , waitForTimeoutPolling );
116
121
}
117
122
118
- private void checkWaitForMethodForPassedCondition (BooleanSupplier waitAction , long timeout ) {
119
- long accuracyTimeout = timeout + accuracy ;
123
+ private void checkWaitForMethodForPassedCondition (BooleanSupplier waitAction , double pollingInterval ) {
120
124
timer .get ().start ();
121
125
boolean result = waitAction .getAsBoolean ();
122
126
double duration = timer .get ().stop ();
127
+ double doubleAccuracyPollingInterval = 2 * pollingInterval / 1000 + accuracy ;
123
128
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 ));
127
132
}
128
133
129
134
private Object [][] getDataProvider (ExpectedCondition <Object > action ) {
@@ -136,14 +141,14 @@ private Object[][] getDataProvider(ExpectedCondition<Object> action) {
136
141
Callable actionWithCustomTimeoutsAndExceptions = () -> conditionalWait .waitFor (action , waitForTimeoutCondition , waitForTimeoutPolling , Collections .emptyList ());
137
142
Callable actionWithAllParameters = () -> conditionalWait .waitFor (action , waitForTimeoutCondition , waitForTimeoutPolling , "Condition should be true" , Collections .emptyList ());
138
143
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 }
147
152
};
148
153
}
149
154
}
0 commit comments