33
33
import static org .openqa .selenium .remote .DriverCommand .GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW ;
34
34
import static org .openqa .selenium .remote .DriverCommand .GET_PAGE_SOURCE ;
35
35
import static org .openqa .selenium .remote .DriverCommand .GET_WINDOW_HANDLES ;
36
+ import static org .openqa .selenium .remote .DriverCommand .IS_ELEMENT_DISPLAYED ;
36
37
import static org .openqa .selenium .remote .DriverCommand .MAXIMIZE_CURRENT_WINDOW ;
37
38
import static org .openqa .selenium .remote .DriverCommand .SET_ALERT_VALUE ;
38
39
import static org .openqa .selenium .remote .DriverCommand .SET_CURRENT_WINDOW_POSITION ;
@@ -63,6 +64,7 @@ public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
63
64
public W3CHttpCommandCodec () {
64
65
alias (GET_ELEMENT_ATTRIBUTE , EXECUTE_SCRIPT );
65
66
alias (GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW , EXECUTE_SCRIPT );
67
+ alias (IS_ELEMENT_DISPLAYED , EXECUTE_SCRIPT );
66
68
alias (SUBMIT_ELEMENT , EXECUTE_SCRIPT );
67
69
68
70
defineCommand (EXECUTE_SCRIPT , post ("/session/:sessionId/execute/sync" ));
@@ -134,18 +136,10 @@ public W3CHttpCommandCodec() {
134
136
135
137
case GET_ELEMENT_ATTRIBUTE :
136
138
// Read the atom, wrap it, execute it.
137
- try {
138
- String scriptName = "/org/openqa/selenium/remote/getAttribute.js" ;
139
- URL url = getClass ().getResource (scriptName );
140
-
141
- String rawFunction = Resources .toString (url , Charsets .UTF_8 );
142
- String script = String .format (
143
- "return (%s).apply(null, arguments);" ,
144
- rawFunction );
145
- return toScript (script , asElement (parameters .get ("id" )), parameters .get ("name" ));
146
- } catch (IOException | NullPointerException e ) {
147
- throw new WebDriverException (e );
148
- }
139
+ return executeAtom (
140
+ "getAttribute.js" ,
141
+ asElement (parameters .get ("id" )),
142
+ parameters .get ("name" ));
149
143
150
144
case GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW :
151
145
return toScript (
@@ -161,6 +155,9 @@ public W3CHttpCommandCodec() {
161
155
case GET_CURRENT_WINDOW_POSITION :
162
156
return toScript ("return {x: window.screenX, y: window.screenY}" );
163
157
158
+ case IS_ELEMENT_DISPLAYED :
159
+ return executeAtom ("isDisplayed.js" , asElement (parameters .get ("id" )));
160
+
164
161
case SET_CURRENT_WINDOW_POSITION :
165
162
return toScript (
166
163
"window.screenX = arguments[0]; window.screenY = arguments[1]" ,
@@ -184,6 +181,21 @@ public W3CHttpCommandCodec() {
184
181
}
185
182
}
186
183
184
+ private Map <String , ?> executeAtom (String atomFileName , Object ... args ) {
185
+ try {
186
+ String scriptName = "/org/openqa/selenium/remote/" + atomFileName ;
187
+ URL url = getClass ().getResource (scriptName );
188
+
189
+ String rawFunction = Resources .toString (url , Charsets .UTF_8 );
190
+ String script = String .format (
191
+ "return (%s).apply(null, arguments);" ,
192
+ rawFunction );
193
+ return toScript (script , args );
194
+ } catch (IOException | NullPointerException e ) {
195
+ throw new WebDriverException (e );
196
+ }
197
+ }
198
+
187
199
private Map <String , ?> toScript (String script , Object ... args ) {
188
200
// Escape the quote marks
189
201
script = script .replaceAll ("\" " , "\\ \" " );
0 commit comments