18
18
import org .openqa .selenium .ElementNotInteractableException ;
19
19
import org .openqa .selenium .WebDriver ;
20
20
import org .openqa .selenium .remote .RemoteWebElement ;
21
+ import org .slf4j .Logger ;
21
22
import org .slf4j .LoggerFactory ;
22
23
23
24
import software .xdev .selenium .elements .CanFindElements ;
34
35
@ SuppressWarnings ("java:S2160" )
35
36
public class ImprovedRemoteWebElement extends RemoteWebElement implements CanFindElements
36
37
{
37
- protected String waitForServerLoadToFinishFunction ;
38
+ protected Logger logger ;
39
+ protected final String waitForServerLoadToFinishFunction ;
40
+ protected boolean autoScrollIntoView = true ;
38
41
39
42
public ImprovedRemoteWebElement (final String waitForServerLoadToFinishFunction )
40
43
{
41
44
this .waitForServerLoadToFinishFunction = waitForServerLoadToFinishFunction ;
42
45
}
43
46
47
+ public ImprovedRemoteWebElement withAutoScrollIntoView (final boolean autoScrollIntoView )
48
+ {
49
+ this .autoScrollIntoView = autoScrollIntoView ;
50
+ return this ;
51
+ }
52
+
53
+ protected Logger logger ()
54
+ {
55
+ if (this .logger == null )
56
+ {
57
+ this .logger = LoggerFactory .getLogger (this .getClass ());
58
+ }
59
+ return this .logger ;
60
+ }
61
+
44
62
@ Override
45
63
public WebDriver getWebDriver ()
46
64
{
@@ -57,8 +75,7 @@ public void click()
57
75
}
58
76
catch (final ElementNotInteractableException ex )
59
77
{
60
- LoggerFactory .getLogger (this .getClass ())
61
- .warn (
78
+ this .logger ().warn (
62
79
"Element can't be clicked via UI - executing JS click. "
63
80
+ "Please manually check if the element is accessible. "
64
81
+ "If the element is accessible consider calling performJsClick directly." , ex );
@@ -94,9 +111,16 @@ public void prepareForOperation()
94
111
95
112
public void scrollIntoViewIfRequired ()
96
113
{
97
- if (!this .isDisplayed ())
114
+ try
115
+ {
116
+ if (this .autoScrollIntoView && !this .isDisplayed ())
117
+ {
118
+ this .executeScript ("arguments[0].scrollIntoView(true);" , this );
119
+ }
120
+ }
121
+ catch (final ElementNotInteractableException ex )
98
122
{
99
- this .executeScript ( "arguments[0].scrollIntoView(true);" , this );
123
+ this .logger (). warn ( "Element can't be scrolled into view" , ex );
100
124
}
101
125
}
102
126
@@ -115,8 +139,7 @@ public void waitForServerLoadToFinish()
115
139
final Boolean retVal = (Boolean )this .executeScript (this .waitForServerLoadToFinishFunction );
116
140
if (retVal == null )
117
141
{
118
- LoggerFactory .getLogger (this .getClass ())
119
- .warn ("waitForLoadToFinishFunction returned null! It should either return true or false" );
142
+ this .logger ().warn ("waitForLoadToFinishFunction returned null! It should either return true or false" );
120
143
}
121
144
finished = Boolean .TRUE .equals (retVal );
122
145
}
0 commit comments