55import aquality .selenium .configuration .ITimeoutConfiguration ;
66import org .openqa .selenium .StaleElementReferenceException ;
77import org .testng .Assert ;
8+ import org .testng .annotations .AfterMethod ;
89import org .testng .annotations .Test ;
910import utils .DurationSample ;
1011import utils .Timer ;
11-
1212import java .util .Collections ;
1313import java .util .concurrent .TimeoutException ;
14-
1514import static org .testng .Assert .assertFalse ;
1615import static org .testng .Assert .assertTrue ;
1716
@@ -88,8 +87,6 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndDefaul
8887 } catch (org .openqa .selenium .TimeoutException e ) {
8988 DurationSample durationSample = new DurationSample (timer .duration (), getTimeoutConfig ().getCondition (), defaultDeviation );
9089 assertTrue (durationSample .isDurationBetweenLimits (), durationSample .toString ());
91- } finally {
92- BrowserManager .getBrowser ().quit ();
9390 }
9491 }
9592
@@ -102,71 +99,122 @@ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeou
10299 timer .start ();
103100 return false ;
104101 }, waitForTimeoutCondition , waitForTimeoutPolling ,
105- "Conditional should be true" , Collections . singleton ( StaleElementReferenceException . class ) );
102+ "Conditional should be true" );
106103
107104 } catch (org .openqa .selenium .TimeoutException e ) {
108105 DurationSample durationSample = new DurationSample (timer .duration (), waitForTimeoutCondition , defaultDeviation );
109106 assertTrue (durationSample .isDurationBetweenLimits (), durationSample .toString ());
110- } finally {
111- BrowserManager .getBrowser ().quit ();
112107 }
113108 }
114109
115110 @ Test
116- public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver () {
111+ public void testTimeoutExceptionShouldBeThrownIfDriverConditionIsNotMetAndTimeoutIsOverWithIgnoredExceptions () {
117112 Timer timer = new Timer ();
118113 try {
119114 ConditionalWait .waitFor ((driver ) ->
120115 {
121116 timer .start ();
122- return true ;
123- },
124- "Conditional should be true" );
125- DurationSample durationSample = new DurationSample ( timer . duration (), getTimeoutConfig (). getCondition ());
126- assertTrue ( durationSample . getDuration () < getTimeoutConfig (). getCondition ());
127- } finally {
128- BrowserManager . getBrowser (). quit ( );
117+ return false ;
118+ }, waitForTimeoutCondition , waitForTimeoutPolling ,
119+ "Conditional should be true" , Collections . emptyList () );
120+
121+ } catch ( org . openqa . selenium . TimeoutException e ) {
122+ DurationSample durationSample = new DurationSample ( timer . duration (), waitForTimeoutCondition , defaultDeviation );
123+ assertTrue ( durationSample . isDurationBetweenLimits (), durationSample . toString () );
129124 }
130125 }
131126
132127 @ Test
133- public void testExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver (){
128+ public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndDefaultTimeoutIsNotOver () {
129+ Timer timer = new Timer ();
130+
131+ ConditionalWait .waitFor ((driver ) ->
132+ {
133+ timer .start ();
134+ return true ;
135+ },
136+ "Conditional should be true" );
137+ DurationSample durationSample = new DurationSample (timer .duration (), getTimeoutConfig ().getCondition ());
138+ assertTrue (durationSample .getDuration () < getTimeoutConfig ().getCondition ());
139+ }
140+
141+ @ Test
142+ public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOverWithIgnoredExceptions () {
143+ Timer timer = new Timer ();
144+ boolean conditionResult = ConditionalWait .waitFor ((driver ) ->
145+ {
146+ timer .start ();
147+ return true ;
148+ }, waitForTimeoutCondition , waitForTimeoutPolling ,
149+ "Conditional should be true" );
150+ DurationSample durationSample = new DurationSample (timer .duration (), waitForTimeoutCondition );
151+ assertTrue (durationSample .getDuration () < waitForTimeoutCondition );
152+ assertTrue (conditionResult , "Condition result should be true" );
153+ }
154+
155+ @ Test
156+ public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver () {
157+ Timer timer = new Timer ();
158+ boolean conditionResult = ConditionalWait .waitFor ((driver ) ->
159+ {
160+ timer .start ();
161+ return true ;
162+ }, waitForTimeoutCondition , waitForTimeoutPolling ,
163+ "Conditional should be true" , Collections .singleton (IllegalArgumentException .class ));
164+ DurationSample durationSample = new DurationSample (timer .duration (), waitForTimeoutCondition );
165+ assertTrue (durationSample .getDuration () < waitForTimeoutCondition );
166+ assertTrue (conditionResult , "Condition result should be true" );
167+ }
168+
169+ @ Test
170+ public void testExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver () {
134171 Timer timer = new Timer ();
135- try {
172+ try {
136173 ConditionalWait .waitFor ((driver ) ->
137174 {
138175 timer .start ();
139176 throw new IllegalArgumentException ("I am exception" );
140177 }, waitForTimeoutCondition , waitForTimeoutPolling ,
141178 "Conditional should be true" , Collections .singleton (IllegalArgumentException .class ));
142- } catch (org .openqa .selenium .TimeoutException e ){
179+ } catch (org .openqa .selenium .TimeoutException e ) {
143180 DurationSample durationSample = new DurationSample (timer .duration (), waitForTimeoutCondition , defaultDeviation );
144181 assertTrue (durationSample .isDurationBetweenLimits (), durationSample .toString ());
145- } finally {
146- BrowserManager .getBrowser ().quit ();
147182 }
148183 }
149184
150185 @ Test
151- public void testTimeoutExceptionShouldNotBeThrownIfDriverConditionIsMetAndTimeoutIsNotOver () {
186+ public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndTimeoutIsNotOver () {
152187 Timer timer = new Timer ();
153188 try {
154- boolean conditionResult = ConditionalWait .waitFor ((driver ) ->
189+ ConditionalWait .waitFor ((driver ) ->
155190 {
156191 timer .start ();
157- return true ;
192+ throw new StaleElementReferenceException ( "I am StaleElementReferenceException" ) ;
158193 }, waitForTimeoutCondition , waitForTimeoutPolling ,
159- "Conditional should be true" , Collections .singleton (IllegalArgumentException .class ));
160- DurationSample durationSample = new DurationSample (timer .duration (), waitForTimeoutCondition );
161- assertTrue (durationSample .getDuration () < waitForTimeoutCondition );
162- assertTrue (conditionResult , "Condition result should be true" );
163- } finally {
164- BrowserManager .getBrowser ().quit ();
194+ "Conditional should be true" );
195+ } catch (org .openqa .selenium .TimeoutException e ) {
196+ DurationSample durationSample = new DurationSample (timer .duration (), waitForTimeoutCondition , defaultDeviation );
197+ assertTrue (durationSample .isDurationBetweenLimits (), durationSample .toString ());
198+ }
199+ }
200+
201+ @ Test
202+ public void testStaleElementReferenceExceptionShouldBeCaughtConditionIsMetAndDefaultTimeoutIsNotOver () {
203+ Timer timer = new Timer ();
204+ try {
205+ ConditionalWait .waitFor ((driver ) ->
206+ {
207+ timer .start ();
208+ throw new StaleElementReferenceException ("I am StaleElementReferenceException" );
209+ }, "Conditional should be true" );
210+ } catch (org .openqa .selenium .TimeoutException e ) {
211+ DurationSample durationSample = new DurationSample (timer .duration (), getTimeoutConfig ().getCondition (), defaultDeviation );
212+ assertTrue (durationSample .isDurationBetweenLimits (), durationSample .toString ());
165213 }
166214 }
167215
168216 @ Test
169- public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver () {
217+ public void testTrueShouldBeReturnedIfConditionIsMetAndTimeoutIsNotOver () {
170218 Timer timer = new Timer ();
171219 boolean conditionResult = ConditionalWait .waitFor (() ->
172220 {
@@ -179,7 +227,7 @@ public void testTrueShouldNotBeReturnedIfConditionIsMetAndTimeoutIsNotOver(){
179227 }
180228
181229 @ Test
182- public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver (){
230+ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver () {
183231 Timer timer = new Timer ();
184232 boolean conditionResult = ConditionalWait .waitFor (() ->
185233 {
@@ -192,7 +240,7 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndTimeoutIsOver(){
192240 }
193241
194242 @ Test
195- public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver (){
243+ public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver () {
196244 Timer timer = new Timer ();
197245 boolean conditionResult = ConditionalWait .waitFor (() ->
198246 {
@@ -205,7 +253,7 @@ public void testTrueShouldBeReturnedIfConditionIsMetAndDefaultTimeoutIsNotOver()
205253 }
206254
207255 @ Test
208- public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver (){
256+ public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver () {
209257 Timer timer = new Timer ();
210258 boolean conditionResult = ConditionalWait .waitFor (() ->
211259 {
@@ -217,7 +265,12 @@ public void testFalseShouldBeReturnedIfConditionIsNotMetAndDefaultTimeoutIsOver(
217265 assertFalse (conditionResult , "Condition result should be false" );
218266 }
219267
220- private ITimeoutConfiguration getTimeoutConfig (){
268+ @ AfterMethod
269+ public void after () {
270+ BrowserManager .getBrowser ().quit ();
271+ }
272+
273+ private ITimeoutConfiguration getTimeoutConfig () {
221274 return Configuration .getInstance ().getTimeoutConfiguration ();
222275 }
223276}
0 commit comments