55using System ;
66using System . Collections . Generic ;
77using System . Diagnostics . CodeAnalysis ;
8+ using System . Linq ;
9+ using System . Text . RegularExpressions ;
810
911namespace Behavioral . Automation . Template . Bindings . ElementWrappers
1012{
1113 public class WebElementWrapper : IWebElementWrapper
1214 {
1315 private readonly Func < IWebElement > _elementSelector ;
14- private readonly IDriverService _driverService ;
1516
16- public WebElementWrapper ( [ NotNull ] Func < IWebElement > elementSelector , [ NotNull ] string caption , [ NotNull ] IDriverService driverService )
17+ public WebElementWrapper ( [ NotNull ] Func < IWebElement > elementSelector , [ NotNull ] string caption ,
18+ [ NotNull ] IDriverService driverService )
1719 {
1820 _elementSelector = elementSelector ;
19- _driverService = driverService ;
21+ Driver = driverService ;
2022 Caption = caption ;
2123 }
2224
2325 public string Caption { get ; }
2426
2527 public IWebElement Element => _elementSelector ( ) ;
2628
27- public string Text => Element . Text ;
29+ public string Text
30+ {
31+ get
32+ {
33+ try
34+ {
35+ return Regex . Replace ( Element . Text , @"\t|\n|\r" , " " ) . Replace ( " " , " " ) ;
36+ }
37+ catch ( Exception e ) when ( e is NullReferenceException or StaleElementReferenceException )
38+ {
39+ return string . Empty ;
40+ }
41+ }
42+ }
2843
29- public string GetAttribute ( string attribute ) => Element . GetAttribute ( attribute ) ;
44+ public string GetAttribute ( string attribute )
45+ {
46+ try
47+ {
48+ return Element . GetAttribute ( attribute ) ;
49+ }
50+ catch ( Exception e ) when ( e is NullReferenceException or StaleElementReferenceException )
51+ {
52+ return null ;
53+ }
54+ }
3055
3156 public void Click ( )
3257 {
33- MouseHover ( ) ;
34- Assert . ShouldGet ( ( ) => Enabled ) ;
35- _driverService . MouseClick ( ) ;
58+ Assert . ShouldBecome ( ( ) => Enabled , true , $ "Unable to click on { Caption } . The element was disabled") ;
59+ try
60+ {
61+ Element . Click ( ) ;
62+ }
63+ catch ( ElementClickInterceptedException )
64+ {
65+ MouseHover ( ) ;
66+ Driver . MouseClick ( ) ;
67+ }
3668 }
3769
3870 public void MouseHover ( )
3971 {
40- Assert . ShouldBecome ( ( ) => Enabled , true , $ "{ Caption } is disabled ") ;
41- _driverService . ScrollTo ( Element ) ;
72+ Assert . ShouldBecome ( ( ) => Displayed , true , $ "{ Caption } is not visible ") ;
73+ Driver . ScrollTo ( Element ) ;
4274 }
4375
4476 public void SendKeys ( string text )
@@ -47,29 +79,25 @@ public void SendKeys(string text)
4779 Element . SendKeys ( text ) ;
4880 }
4981
50- public bool Displayed => Element != null && Element . Displayed ;
51-
52- public bool Enabled => Displayed && Element . Enabled && AriaEnabled ;
53-
54- public string Tooltip
82+ public bool Displayed
5583 {
5684 get
5785 {
58- var matTooltip = GetAttribute ( "matTooltip" ) ;
59- if ( matTooltip != null )
86+ try
6087 {
61- return matTooltip ;
88+ return Element is not null && Element . Displayed ;
6289 }
63- var ngReflectTip = GetAttribute ( "ng-reflect-message" ) ; //some elements have their tooltips' texts stored inside 'ng-reflect-message' attribute
64- if ( ngReflectTip != null )
90+ catch ( Exception e ) when ( e is NullReferenceException or StaleElementReferenceException )
6591 {
66- return ngReflectTip ;
92+ return false ;
6793 }
68-
69- return GetAttribute ( "aria-label" ) ; //some elements have their tooltips' texts stored inside 'aria-label' attribute
7094 }
7195 }
7296
97+ public bool Enabled => Displayed && Element . Enabled ;
98+
99+ public string Tooltip => GetAttribute ( "data-test-tooltip-text" ) ;
100+
73101 public bool Stale
74102 {
75103 get
@@ -80,7 +108,7 @@ public bool Stale
80108 var elementEnabled = Element . Enabled ;
81109 return false ;
82110 }
83- catch ( StaleElementReferenceException )
111+ catch ( Exception e ) when ( e is NullReferenceException or StaleElementReferenceException )
84112 {
85113 return true ;
86114 }
@@ -89,34 +117,22 @@ public bool Stale
89117
90118 public IEnumerable < IWebElementWrapper > FindSubElements ( By locator , string caption )
91119 {
92- var elements = Assert . ShouldGet ( ( ) => Element . FindElements ( locator ) ) ;
93- return ElementsToWrappers ( elements , caption ) ;
94- }
95-
96- private IEnumerable < IWebElementWrapper > ElementsToWrappers ( IEnumerable < IWebElement > elements , string caption )
97- {
98- foreach ( var element in elements )
120+ try
99121 {
100- var wrapper = new WebElementWrapper ( ( ) => element , caption , _driverService ) ;
101- yield return wrapper ;
122+ return ElementsToWrappers ( Assert . ShouldGet ( ( ) => Element . FindElements ( locator ) ) , caption ) ;
123+ }
124+ catch ( Exception e ) when ( e is NullReferenceException or InvalidOperationException )
125+ {
126+ NUnit . Framework . Assert . Fail ( $ "Couldn't find elements with { caption } ") ;
127+ return null ;
102128 }
103129 }
104130
105- protected IDriverService Driver => _driverService ;
106-
107- private bool AriaEnabled
131+ private IEnumerable < IWebElementWrapper > ElementsToWrappers ( IEnumerable < IWebElement > elements , string caption )
108132 {
109- get
110- {
111- switch ( Element . GetAttribute ( "aria-disabled" ) )
112- {
113- case null :
114- case "false" :
115- return true ;
116- }
117-
118- return false ;
119- }
133+ return elements . Select ( element => new WebElementWrapper ( ( ) => element , caption , Driver ) ) ;
120134 }
135+
136+ protected IDriverService Driver { get ; }
121137 }
122138}
0 commit comments