1717
1818package org .openqa .selenium .remote ;
1919
20+ import static java .util .Collections .singleton ;
21+ import static java .util .concurrent .TimeUnit .SECONDS ;
22+ import static org .openqa .selenium .remote .CapabilityType .LOGGING_PREFS ;
23+ import static org .openqa .selenium .remote .CapabilityType .PLATFORM ;
24+ import static org .openqa .selenium .remote .CapabilityType .PLATFORM_NAME ;
25+ import static org .openqa .selenium .remote .CapabilityType .SUPPORTS_JAVASCRIPT ;
26+
2027import com .google .common .collect .ImmutableMap ;
2128import com .google .common .collect .ImmutableSet ;
29+
2230import org .openqa .selenium .Alert ;
2331import org .openqa .selenium .Beta ;
2432import org .openqa .selenium .By ;
8896import java .util .stream .Collectors ;
8997import java .util .stream .Stream ;
9098
91- import static java .util .Collections .singleton ;
92- import static java .util .concurrent .TimeUnit .SECONDS ;
93- import static org .openqa .selenium .remote .CapabilityType .LOGGING_PREFS ;
94- import static org .openqa .selenium .remote .CapabilityType .PLATFORM ;
95- import static org .openqa .selenium .remote .CapabilityType .PLATFORM_NAME ;
96- import static org .openqa .selenium .remote .CapabilityType .SUPPORTS_JAVASCRIPT ;
97-
9899@ Augmentable
99100public class RemoteWebDriver implements WebDriver ,
100101 JavascriptExecutor ,
@@ -107,9 +108,8 @@ public class RemoteWebDriver implements WebDriver,
107108
108109 // TODO: This static logger should be unified with the per-instance localLogs
109110 private static final Logger logger = Logger .getLogger (RemoteWebDriver .class .getName ());
110- private Level level = Level .FINE ;
111111 private final ElementLocation elementLocation = new ElementLocation ();
112-
112+ private Level level = Level . FINE ;
113113 private ErrorHandler errorHandler = new ErrorHandler ();
114114 private CommandExecutor executor ;
115115 private Capabilities capabilities ;
@@ -129,14 +129,6 @@ protected RemoteWebDriver() {
129129 this .capabilities = init (new ImmutableCapabilities ());
130130 }
131131
132- private static URL getDefaultServerURL () {
133- try {
134- return new URL (System .getProperty ("webdriver.remote.server" , "http://localhost:4444/" ));
135- } catch (MalformedURLException e ) {
136- throw new WebDriverException (e );
137- }
138- }
139-
140132 public RemoteWebDriver (Capabilities capabilities ) {
141133 this (getDefaultServerURL (),
142134 Require .nonNull ("Capabilities" , capabilities ));
@@ -147,15 +139,6 @@ public RemoteWebDriver(URL remoteAddress, Capabilities capabilities) {
147139 Require .nonNull ("Capabilities" , capabilities ));
148140 }
149141
150- private static CommandExecutor createTracedExecutorWithTracedHttpClient (URL remoteAddress ) {
151- Tracer tracer = OpenTelemetryTracer .getInstance ();
152- CommandExecutor executor = new HttpCommandExecutor (
153- Collections .emptyMap (),
154- ClientConfig .defaultConfig ().baseUrl (remoteAddress ),
155- new TracedHttpClient .Factory (tracer , HttpClient .Factory .createDefault ()));
156- return new TracedCommandExecutor (executor , tracer );
157- }
158-
159142 public RemoteWebDriver (CommandExecutor executor , Capabilities capabilities ) {
160143 this .executor = Require .nonNull ("Command executor" , executor );
161144 capabilities = init (capabilities );
@@ -177,6 +160,23 @@ public RemoteWebDriver(CommandExecutor executor, Capabilities capabilities) {
177160 }
178161 }
179162
163+ private static URL getDefaultServerURL () {
164+ try {
165+ return new URL (System .getProperty ("webdriver.remote.server" , "http://localhost:4444/" ));
166+ } catch (MalformedURLException e ) {
167+ throw new WebDriverException (e );
168+ }
169+ }
170+
171+ private static CommandExecutor createTracedExecutorWithTracedHttpClient (URL remoteAddress ) {
172+ Tracer tracer = OpenTelemetryTracer .getInstance ();
173+ CommandExecutor executor = new HttpCommandExecutor (
174+ Collections .emptyMap (),
175+ ClientConfig .defaultConfig ().baseUrl (remoteAddress ),
176+ new TracedHttpClient .Factory (tracer , HttpClient .Factory .createDefault ()));
177+ return new TracedCommandExecutor (executor , tracer );
178+ }
179+
180180 @ Beta
181181 public static RemoteWebDriverBuilder builder () {
182182 return new RemoteWebDriverBuilder ();
@@ -218,22 +218,6 @@ private Capabilities init(Capabilities capabilities) {
218218 return capabilities ;
219219 }
220220
221- /**
222- * Set the file detector to be used when sending keyboard input. By default, this is set to a file
223- * detector that does nothing.
224- *
225- * @param detector The detector to use. Must not be null.
226- * @see FileDetector
227- * @see LocalFileDetector
228- * @see UselessFileDetector
229- */
230- public void setFileDetector (FileDetector detector ) {
231- if (detector == null ) {
232- throw new WebDriverException ("You may not set a file detector that is null" );
233- }
234- fileDetector = detector ;
235- }
236-
237221 public SessionId getSessionId () {
238222 return sessionId ;
239223 }
@@ -422,13 +406,13 @@ protected void setFoundBy(SearchContext context, WebElement element, String by,
422406 }
423407 }
424408
425- // Misc
426-
427409 @ Override
428410 public String getPageSource () {
429411 return (String ) execute (DriverCommand .GET_PAGE_SOURCE ).getValue ();
430412 }
431413
414+ // Misc
415+
432416 @ Override
433417 public void close () {
434418 if (this instanceof HasDevTools ) {
@@ -528,14 +512,14 @@ public Options manage() {
528512 return new RemoteWebDriverOptions ();
529513 }
530514
531- protected void setElementConverter (JsonToWebElementConverter converter ) {
532- this .converter = Require .nonNull ("Element converter" , converter );
533- }
534-
535515 protected JsonToWebElementConverter getElementConverter () {
536516 return converter ;
537517 }
538518
519+ protected void setElementConverter (JsonToWebElementConverter converter ) {
520+ this .converter = Require .nonNull ("Element converter" , converter );
521+ }
522+
539523 /**
540524 * Sets the RemoteWebDriver's client log level.
541525 *
@@ -674,7 +658,14 @@ protected void log(SessionId sessionId, String commandName, Object toLog, When w
674658 text = text .substring (0 , 100 ) + "..." ;
675659 }
676660 }
677- switch (when ) {
661+ // No need to log a screenshot response.
662+ if ((commandName .equals (DriverCommand .SCREENSHOT )
663+ || commandName .equals (DriverCommand .ELEMENT_SCREENSHOT )) && toLog instanceof Response ) {
664+ Response responseToLog = (Response ) toLog ;
665+ responseToLog .setValue ("*Screenshot response suppressed*" );
666+ text = String .valueOf (responseToLog );
667+ }
668+ switch (when ) {
678669 case BEFORE :
679670 logger .log (level , "Executing: " + commandName + " " + text );
680671 break ;
@@ -694,6 +685,52 @@ public FileDetector getFileDetector() {
694685 return fileDetector ;
695686 }
696687
688+ /**
689+ * Set the file detector to be used when sending keyboard input. By default, this is set to a file
690+ * detector that does nothing.
691+ *
692+ * @param detector The detector to use. Must not be null.
693+ * @see FileDetector
694+ * @see LocalFileDetector
695+ * @see UselessFileDetector
696+ */
697+ public void setFileDetector (FileDetector detector ) {
698+ if (detector == null ) {
699+ throw new WebDriverException ("You may not set a file detector that is null" );
700+ }
701+ fileDetector = detector ;
702+ }
703+
704+ @ Override
705+ public String toString () {
706+ Capabilities caps = getCapabilities ();
707+ if (caps == null ) {
708+ return super .toString ();
709+ }
710+
711+ // w3c name first
712+ Object platform = caps .getCapability (PLATFORM_NAME );
713+ if (!(platform instanceof String )) {
714+ platform = caps .getCapability (PLATFORM );
715+ }
716+ if (platform == null ) {
717+ platform = "unknown" ;
718+ }
719+
720+ return String .format (
721+ "%s: %s on %s (%s)" ,
722+ getClass ().getSimpleName (),
723+ caps .getBrowserName (),
724+ platform ,
725+ getSessionId ());
726+ }
727+
728+ public enum When {
729+ BEFORE ,
730+ AFTER ,
731+ EXCEPTION
732+ }
733+
697734 protected class RemoteWebDriverOptions implements Options {
698735
699736 @ Override
@@ -887,15 +924,7 @@ public Duration getPageLoadTimeout() {
887924 @ Beta
888925 protected class RemoteWindow implements Window {
889926
890- @ Override
891- public void setSize (Dimension targetSize ) {
892- execute (DriverCommand .SET_CURRENT_WINDOW_SIZE (targetSize ));
893- }
894-
895- @ Override
896- public void setPosition (Point targetPosition ) {
897- execute (DriverCommand .SET_CURRENT_WINDOW_POSITION (targetPosition ));
898- }
927+ Map <String , Object > rawPoint ;
899928
900929 @ Override
901930 @ SuppressWarnings ({"unchecked" })
@@ -910,7 +939,11 @@ public Dimension getSize() {
910939 return new Dimension (width , height );
911940 }
912941
913- Map <String , Object > rawPoint ;
942+ @ Override
943+ public void setSize (Dimension targetSize ) {
944+ execute (DriverCommand .SET_CURRENT_WINDOW_SIZE (targetSize ));
945+ }
946+
914947 @ Override
915948 @ SuppressWarnings ("unchecked" )
916949 public Point getPosition () {
@@ -923,6 +956,11 @@ public Point getPosition() {
923956 return new Point (x , y );
924957 }
925958
959+ @ Override
960+ public void setPosition (Point targetPosition ) {
961+ execute (DriverCommand .SET_CURRENT_WINDOW_POSITION (targetPosition ));
962+ }
963+
926964 @ Override
927965 public void maximize () {
928966 execute (DriverCommand .MAXIMIZE_CURRENT_WINDOW );
@@ -1140,34 +1178,4 @@ public void setUserVerified(boolean verified) {
11401178 ImmutableMap .of ("authenticatorId" , id , "isUserVerified" , verified ));
11411179 }
11421180 }
1143-
1144- public enum When {
1145- BEFORE ,
1146- AFTER ,
1147- EXCEPTION
1148- }
1149-
1150- @ Override
1151- public String toString () {
1152- Capabilities caps = getCapabilities ();
1153- if (caps == null ) {
1154- return super .toString ();
1155- }
1156-
1157- // w3c name first
1158- Object platform = caps .getCapability (PLATFORM_NAME );
1159- if (!(platform instanceof String )) {
1160- platform = caps .getCapability (PLATFORM );
1161- }
1162- if (platform == null ) {
1163- platform = "unknown" ;
1164- }
1165-
1166- return String .format (
1167- "%s: %s on %s (%s)" ,
1168- getClass ().getSimpleName (),
1169- caps .getBrowserName (),
1170- platform ,
1171- getSessionId ());
1172- }
11731181}
0 commit comments