@@ -13,7 +13,10 @@ public class CachedElementTests : TestWithBrowser
13
13
{
14
14
private static readonly By RemoveButtonLoc = By . XPath ( "//button[.='Remove']" ) ;
15
15
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" ) ;
16
18
private static readonly Uri DynamicContentUrl = new Uri ( $ "{ TestSite } /dynamic_controls") ;
19
+ private static readonly Uri DynamicLoadingUrl = new Uri ( $ "{ TestSite } /dynamic_loading/1") ;
17
20
private const string ElementCacheVariableName = "elementCache.isEnabled" ;
18
21
19
22
private static readonly Func < IElementStateProvider , bool > [ ] StateFunctionsFalseWhenElementStale
@@ -40,12 +43,60 @@ private static readonly Func<IElementStateProvider, bool>[] StateFunctionsTrueWh
40
43
public new void SetUp ( )
41
44
{
42
45
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
+ {
43
56
AqualityServices . Application . Driver . Navigate ( ) . GoToUrl ( DynamicContentUrl ) ;
44
57
}
45
58
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
+
46
96
[ Test ]
47
97
public void Should_RefreshElement_WhenItIsStale ( )
48
98
{
99
+ OpenDynamicContent ( ) ;
49
100
var example = new Label ( ContentLoc , "Example" , ElementState . Displayed ) ;
50
101
example . GetElement ( ) ;
51
102
var exToString = example . GetElement ( ) . ToString ( ) ;
@@ -68,13 +119,12 @@ public void Should_ReturnCorrectState_True_WhenWindowIsRefreshed([ValueSource(na
68
119
69
120
private void AssertStateConditionAfterRefresh ( Func < IElementStateProvider , bool > stateCondition , bool expectedValue )
70
121
{
122
+ OpenDynamicContent ( ) ;
71
123
var testElement = new Label ( ContentLoc , "Example" , ElementState . ExistsInAnyState ) ;
72
124
testElement . State . WaitForClickable ( ) ;
73
125
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." ) ;
78
128
AqualityServices . Application . Driver . Navigate ( ) . Refresh ( ) ;
79
129
Assert . IsTrue ( testElement . Cache . IsStale , "Element should remain stale after the page refresh." ) ;
80
130
Assert . AreEqual ( expectedValue , stateCondition ( testElement . State ) ,
0 commit comments