44using OpenQA . Selenium . Support . Extensions ;
55using OpenQA . Selenium . Support . UI ;
66using Serilog ;
7- using ExpectedConditions = SeleniumExtras . WaitHelpers . ExpectedConditions ;
87using ConfigType = EverlightRadiology . Framework . Wrapper . TestConstant . ConfigTypes ;
98using ConfigKey = EverlightRadiology . Framework . Wrapper . TestConstant . ConfigTypesKey ;
109using LocatorType = EverlightRadiology . Framework . Wrapper . TestConstant . LocatorType ;
@@ -31,23 +30,23 @@ public WebHelper(IWebDriver? driver)
3130 {
3231 _locator = locatorType ;
3332 _locatorInfo = locatorInfo ;
34- var dWait = new WebDriverWait ( Driver ,
35- TimeSpan . FromSeconds ( int . Parse ( ConfigHelper . ReadConfigValue ( ConfigType . WebDriverConfig ,
36- ConfigKey . ObjectIdentificationTimeOut ) ) ) ) ;
33+ var dWait = new WebDriverWait ( Driver ?? throw new ArgumentNullException ( nameof ( Driver ) ) ,
34+ TimeSpan . FromSeconds ( int . Parse ( ConfigHelper . ReadConfigValue ( ConfigType . WebDriverConfig ,
35+ ConfigKey . ObjectIdentificationTimeOut ) ?? "0" ) ) ) ;
3736 dWait . IgnoreExceptionTypes ( typeof ( StaleElementReferenceException ) ,
3837 typeof ( NoSuchElementException ) ,
39- typeof ( InvalidElementStateException ) ,
4038 typeof ( ElementNotInteractableException ) ) ;
4139 try
4240 {
41+ var locatorText = locatorInfo ?? throw new ArgumentNullException ( nameof ( locatorInfo ) ) ;
4342 IWebElement ? dynamicElement ;
4443 List < IWebElement ? > webElements ;
4544 switch ( locatorType )
4645 {
4746 case LocatorType . Id :
4847 {
49- dynamicElement = dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . Id ( locatorInfo ) ) ) ;
50- webElements = new List < IWebElement ? > ( Driver ? . FindElements ( By . Id ( locatorInfo ) ) ) ;
48+ dynamicElement = dWait . Until ( Driver => ElementToBeClickable ( Driver , By . Id ( locatorText ) ) ) ;
49+ webElements = [ .. Driver ? . FindElements ( By . Id ( locatorText ) ) ] ;
5150 if ( webElements . Count > 1 )
5251 {
5352 foreach ( var webE in webElements . Where ( IsElementDisplayed ) )
@@ -61,18 +60,18 @@ public WebHelper(IWebDriver? driver)
6160 case LocatorType . ClassName :
6261 {
6362 dynamicElement =
64- dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . ClassName ( locatorInfo ) ) ) ;
63+ dWait . Until ( Driver => ElementToBeClickable ( Driver , By . ClassName ( locatorText ) ) ) ;
6564 break ;
6665 }
6766 case LocatorType . Name :
6867 {
69- dynamicElement = dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . Name ( locatorInfo ) ) ) ;
68+ dynamicElement = dWait . Until ( Driver => ElementToBeClickable ( Driver , By . Name ( locatorText ) ) ) ;
7069 break ;
7170 }
7271 case LocatorType . XPath :
7372 {
74- dynamicElement = dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . XPath ( locatorInfo ) ) ) ;
75- webElements = new List < IWebElement ? > ( Driver ? . FindElements ( By . XPath ( locatorInfo ) ) ) ;
73+ dynamicElement = dWait . Until ( Driver => ElementToBeClickable ( Driver , By . XPath ( locatorText ) ) ) ;
74+ webElements = [ .. Driver . FindElements ( By . XPath ( locatorText ) ) ] ;
7675 if ( webElements . Count > 1 )
7776 {
7877 foreach ( var webE in webElements . Where ( IsElementDisplayed ) )
@@ -86,25 +85,25 @@ public WebHelper(IWebDriver? driver)
8685 case LocatorType . CssSelector :
8786 {
8887 dynamicElement =
89- dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . CssSelector ( locatorInfo ) ) ) ;
88+ dWait . Until ( Driver => ElementToBeClickable ( Driver , By . CssSelector ( locatorText ) ) ) ;
9089 break ;
9190 }
9291 case LocatorType . LinkText :
9392 {
9493 dynamicElement =
95- dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . LinkText ( locatorInfo ) ) ) ;
94+ dWait . Until ( Driver => ElementToBeClickable ( Driver , By . LinkText ( locatorText ) ) ) ;
9695 break ;
9796 }
9897 case LocatorType . PartialLinkText :
9998 {
10099 dynamicElement =
101- dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . PartialLinkText ( locatorInfo ) ) ) ;
100+ dWait . Until ( Driver => ElementToBeClickable ( Driver , By . PartialLinkText ( locatorText ) ) ) ;
102101 break ;
103102 }
104103 case LocatorType . TagName :
105104 {
106105 dynamicElement =
107- dWait . Until ( ExpectedConditions . ElementToBeClickable ( By . TagName ( locatorInfo ) ) ) ;
106+ dWait . Until ( Driver => ElementToBeClickable ( Driver , By . TagName ( locatorText ) ) ) ;
108107 break ;
109108 }
110109 default :
@@ -376,11 +375,11 @@ public void KeyboardAction(KeyBoardAction keyPressEventAction, int iteration)
376375 try
377376 {
378377 var wait = new WebDriverWait ( Driver , TimeSpan . FromSeconds ( 10 ) ) ;
379- wait . Until ( ExpectedConditions . AlertIsPresent ( ) ) ;
378+ wait . Until ( AlertIsPresent ) ;
380379 var alert = Driver ? . SwitchTo ( ) . Alert ( ) ;
381380 var strWarning = alert ? . Text ;
382381 alert ? . Accept ( ) ;
383- wait . Until ( ExpectedConditions . AlertState ( false ) ) ;
382+ wait . Until ( AlertIsNotPresent ) ;
384383 Log . Debug ( "Alert displayed as {0}" , strWarning ) ;
385384 return strWarning ;
386385 }
@@ -397,11 +396,11 @@ public void KeyboardAction(KeyBoardAction keyPressEventAction, int iteration)
397396 try
398397 {
399398 var wait = new WebDriverWait ( Driver , TimeSpan . FromSeconds ( 10 ) ) ;
400- wait . Until ( ExpectedConditions . AlertIsPresent ( ) ) ;
399+ wait . Until ( AlertIsPresent ) ;
401400 var alert = Driver ? . SwitchTo ( ) . Alert ( ) ;
402401 var strWarning = alert ? . Text ;
403402 alert ? . Dismiss ( ) ;
404- wait . Until ( ExpectedConditions . AlertState ( false ) ) ;
403+ wait . Until ( AlertIsNotPresent ) ;
405404 Log . Debug ( "Alert displayed as {0}" , strWarning ) ;
406405 return strWarning ;
407406 }
@@ -624,5 +623,36 @@ private void WebElementExceptionHandler(WebDriverAction webDriverAction, string?
624623 {
625624 return InitialiseDynamicWebElement ( _locator , _locatorInfo ) ;
626625 }
626+
627+ private static IWebElement ? ElementToBeClickable ( IWebDriver driver , By locator )
628+ {
629+ var element = driver . FindElement ( locator ) ;
630+ return element . Displayed && element . Enabled ? element : null ;
631+ }
632+
633+ private static IAlert ? AlertIsPresent ( IWebDriver driver )
634+ {
635+ try
636+ {
637+ return driver . SwitchTo ( ) . Alert ( ) ;
638+ }
639+ catch ( NoAlertPresentException )
640+ {
641+ return null ;
642+ }
643+ }
644+
645+ private static bool AlertIsNotPresent ( IWebDriver driver )
646+ {
647+ try
648+ {
649+ driver . SwitchTo ( ) . Alert ( ) ;
650+ return false ;
651+ }
652+ catch ( NoAlertPresentException )
653+ {
654+ return true ;
655+ }
656+ }
627657 }
628658}
0 commit comments