@@ -13,7 +13,10 @@ public class CachedElementTests : TestWithBrowser
1313 {
1414 private static readonly By RemoveButtonLoc = By . XPath ( "//button[.='Remove']" ) ;
1515 private static readonly By ContentLoc = By . Id ( "checkbox" ) ;
16+ private static readonly By StartLoc = By . XPath ( "//*[@id='start']//button" ) ;
17+ private static readonly By LoadingLoc = By . Id ( "loading" ) ;
1618 private static readonly Uri DynamicContentUrl = new Uri ( $ "{ TestSite } /dynamic_controls") ;
19+ private static readonly Uri DynamicLoadingUrl = new Uri ( $ "{ TestSite } /dynamic_loading/1") ;
1720 private const string ElementCacheVariableName = "elementCache.isEnabled" ;
1821
1922 private static readonly Func < IElementStateProvider , bool > [ ] StateFunctionsFalseWhenElementStale
@@ -40,12 +43,60 @@ private static readonly Func<IElementStateProvider, bool>[] StateFunctionsTrueWh
4043 public new void SetUp ( )
4144 {
4245 Environment . SetEnvironmentVariable ( ElementCacheVariableName , true . ToString ( ) ) ;
46+ }
47+
48+ private void StartLoading ( )
49+ {
50+ AqualityServices . Application . Driver . Navigate ( ) . GoToUrl ( DynamicLoadingUrl ) ;
51+ new Label ( StartLoc , "start" , ElementState . Displayed ) . Click ( ) ;
52+ }
53+
54+ private void OpenDynamicContent ( )
55+ {
4356 AqualityServices . Application . Driver . Navigate ( ) . GoToUrl ( DynamicContentUrl ) ;
4457 }
4558
59+ private void WaitForLoading ( Label loader )
60+ {
61+ Assume . That ( loader . State . WaitForDisplayed ( ) , "Loader should be displayed in the beginning" ) ;
62+ Assume . That ( loader . State . WaitForNotDisplayed ( ) , "Loader should not be displayed in the end" ) ;
63+ }
64+
65+ [ Test ]
66+ public void Should_ReturnFalse_AtWaitForDisplayed_WhenElementIsNotDisplayed ( )
67+ {
68+ var loader = new Label ( LoadingLoc , "loader" , ElementState . Displayed ) ;
69+ StartLoading ( ) ;
70+ WaitForLoading ( loader ) ;
71+ Assert . IsFalse ( loader . State . WaitForDisplayed ( TimeSpan . Zero ) , nameof ( Should_ReturnFalse_AtWaitForDisplayed_WhenElementIsNotDisplayed ) ) ;
72+ }
73+
74+ [ Test ]
75+ public void Should_ReturnTrue_AtWaitForExist_WhenElementIsNotDisplayed ( )
76+ {
77+ var loader = new Label ( LoadingLoc , "loader" , ElementState . Displayed ) ;
78+ StartLoading ( ) ;
79+ WaitForLoading ( loader ) ;
80+ Assert . IsTrue ( loader . State . WaitForExist ( TimeSpan . Zero ) , nameof ( Should_ReturnTrue_AtWaitForExist_WhenElementIsNotDisplayed ) ) ;
81+ }
82+
83+ [ Test ]
84+ public void Should_BeStale_WhenBecameInvisible ( )
85+ {
86+ StartLoading ( ) ;
87+ var loader = new Label ( LoadingLoc , "loader" , ElementState . Displayed ) ;
88+ Assume . That ( loader . State . WaitForDisplayed ( ) , "Loader should be displayed in the beginning" ) ;
89+ Assert . IsTrue ( AqualityServices . ServiceProvider . GetRequiredService < ConditionalWait > ( ) . WaitFor (
90+ ( ) => loader . Cache . IsStale ) , "Loader should become invisible and be treated as stale" ) ;
91+ Assert . IsFalse ( loader . State . IsDisplayed , "Invisible loader should be not displayed" ) ;
92+ Assert . IsFalse ( loader . State . IsExist , "Loader that was displayed previously and become invisible should be treated as disappeared" ) ;
93+ Assert . IsTrue ( loader . State . WaitForExist ( TimeSpan . Zero ) , "When waiting for existance, we should get an actual element's state" ) ;
94+ }
95+
4696 [ Test ]
4797 public void Should_RefreshElement_WhenItIsStale ( )
4898 {
99+ OpenDynamicContent ( ) ;
49100 var example = new Label ( ContentLoc , "Example" , ElementState . Displayed ) ;
50101 example . GetElement ( ) ;
51102 var exToString = example . GetElement ( ) . ToString ( ) ;
@@ -68,13 +119,12 @@ public void Should_ReturnCorrectState_True_WhenWindowIsRefreshed([ValueSource(na
68119
69120 private void AssertStateConditionAfterRefresh ( Func < IElementStateProvider , bool > stateCondition , bool expectedValue )
70121 {
122+ OpenDynamicContent ( ) ;
71123 var testElement = new Label ( ContentLoc , "Example" , ElementState . ExistsInAnyState ) ;
72124 testElement . State . WaitForClickable ( ) ;
73125 new Label ( RemoveButtonLoc , "Remove" , ElementState . Displayed ) . Click ( ) ;
74- AqualityServices . ServiceProvider . GetRequiredService < ConditionalWait > ( ) . WaitFor ( driver =>
75- {
76- return testElement . Cache . IsStale ;
77- } , message : "Element should be stale when it disappeared." ) ;
126+ AqualityServices . ServiceProvider . GetRequiredService < ConditionalWait > ( ) . WaitForTrue (
127+ ( ) => testElement . Cache . IsStale , message : "Element should be stale when it disappeared." ) ;
78128 AqualityServices . Application . Driver . Navigate ( ) . Refresh ( ) ;
79129 Assert . IsTrue ( testElement . Cache . IsStale , "Element should remain stale after the page refresh." ) ;
80130 Assert . AreEqual ( expectedValue , stateCondition ( testElement . State ) ,
0 commit comments