3333import static org .openqa .selenium .remote .DriverCommand .GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW ;
3434import static org .openqa .selenium .remote .DriverCommand .GET_PAGE_SOURCE ;
3535import static org .openqa .selenium .remote .DriverCommand .GET_WINDOW_HANDLES ;
36+ import static org .openqa .selenium .remote .DriverCommand .IS_ELEMENT_DISPLAYED ;
3637import static org .openqa .selenium .remote .DriverCommand .MAXIMIZE_CURRENT_WINDOW ;
3738import static org .openqa .selenium .remote .DriverCommand .SET_ALERT_VALUE ;
3839import static org .openqa .selenium .remote .DriverCommand .SET_CURRENT_WINDOW_POSITION ;
@@ -63,6 +64,7 @@ public class W3CHttpCommandCodec extends AbstractHttpCommandCodec {
6364 public W3CHttpCommandCodec () {
6465 alias (GET_ELEMENT_ATTRIBUTE , EXECUTE_SCRIPT );
6566 alias (GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW , EXECUTE_SCRIPT );
67+ alias (IS_ELEMENT_DISPLAYED , EXECUTE_SCRIPT );
6668 alias (SUBMIT_ELEMENT , EXECUTE_SCRIPT );
6769
6870 defineCommand (EXECUTE_SCRIPT , post ("/session/:sessionId/execute/sync" ));
@@ -134,18 +136,10 @@ public W3CHttpCommandCodec() {
134136
135137 case GET_ELEMENT_ATTRIBUTE :
136138 // 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" ));
149143
150144 case GET_ELEMENT_LOCATION_ONCE_SCROLLED_INTO_VIEW :
151145 return toScript (
@@ -161,6 +155,9 @@ public W3CHttpCommandCodec() {
161155 case GET_CURRENT_WINDOW_POSITION :
162156 return toScript ("return {x: window.screenX, y: window.screenY}" );
163157
158+ case IS_ELEMENT_DISPLAYED :
159+ return executeAtom ("isDisplayed.js" , asElement (parameters .get ("id" )));
160+
164161 case SET_CURRENT_WINDOW_POSITION :
165162 return toScript (
166163 "window.screenX = arguments[0]; window.screenY = arguments[1]" ,
@@ -184,6 +181,21 @@ public W3CHttpCommandCodec() {
184181 }
185182 }
186183
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+
187199 private Map <String , ?> toScript (String script , Object ... args ) {
188200 // Escape the quote marks
189201 script = script .replaceAll ("\" " , "\\ \" " );
0 commit comments