1818package org .openqa .selenium .remote .service ;
1919
2020import java .io .File ;
21+ import java .util .ArrayList ;
22+ import java .util .Arrays ;
23+ import java .util .List ;
24+ import java .util .Map ;
25+ import java .util .logging .Logger ;
2126import org .openqa .selenium .Capabilities ;
27+ import org .openqa .selenium .MutableCapabilities ;
28+ import org .openqa .selenium .Proxy ;
2229import org .openqa .selenium .WebDriverException ;
2330import org .openqa .selenium .internal .Require ;
2431import org .openqa .selenium .manager .SeleniumManager ;
2734
2835public class DriverFinder {
2936
37+ private static final Logger LOG = Logger .getLogger (DriverFinder .class .getName ());
38+
3039 public static Result getPath (DriverService service , Capabilities options ) {
3140 return getPath (service , options , false );
3241 }
@@ -37,7 +46,9 @@ public static Result getPath(DriverService service, Capabilities options, boolea
3746
3847 if (result .getDriverPath () == null ) {
3948 try {
40- result = SeleniumManager .getInstance ().getDriverPath (options , offline );
49+ List <String > arguments = toArguments (options , offline );
50+ result = SeleniumManager .getInstance ().getResult (arguments );
51+ ((MutableCapabilities ) options ).setCapability ("browserVersion" , (String ) null );
4152 } catch (RuntimeException e ) {
4253 throw new WebDriverException (
4354 String .format ("Unable to obtain: %s, error %s" , options , e .getMessage ()), e );
@@ -62,4 +73,63 @@ public static Result getPath(DriverService service, Capabilities options, boolea
6273
6374 throw new NoSuchDriverException (message );
6475 }
76+
77+ private static List <String > toArguments (Capabilities options , boolean offline ) {
78+ List <String > arguments = new ArrayList <>();
79+ arguments .add ("--browser" );
80+ arguments .add (options .getBrowserName ());
81+
82+ if (!options .getBrowserVersion ().isEmpty ()) {
83+ arguments .add ("--browser-version" );
84+ arguments .add (options .getBrowserVersion ());
85+ }
86+
87+ String browserBinary = getBrowserBinary (options );
88+ if (browserBinary != null && !browserBinary .isEmpty ()) {
89+ arguments .add ("--browser-path" );
90+ arguments .add (browserBinary );
91+ }
92+
93+ if (offline ) {
94+ arguments .add ("--offline" );
95+ }
96+
97+ Proxy proxy = Proxy .extractFrom (options );
98+ if (proxy != null ) {
99+ arguments .add ("--proxy" );
100+ if (proxy .getSslProxy () != null ) {
101+ arguments .add (proxy .getSslProxy ());
102+ } else if (proxy .getHttpProxy () != null ) {
103+ arguments .add (proxy .getHttpProxy ());
104+ }
105+ }
106+ return arguments ;
107+ }
108+
109+ /**
110+ * Returns the browser binary path when present in the vendor options
111+ *
112+ * @param options browser options used to start the session
113+ * @return the browser binary path when present, only Chrome/Firefox/Edge
114+ */
115+ private static String getBrowserBinary (Capabilities options ) {
116+ List <String > vendorOptionsCapabilities =
117+ Arrays .asList ("moz:firefoxOptions" , "goog:chromeOptions" , "ms:edgeOptions" );
118+ for (String vendorOptionsCapability : vendorOptionsCapabilities ) {
119+ if (options .asMap ().containsKey (vendorOptionsCapability )) {
120+ try {
121+ @ SuppressWarnings ("unchecked" )
122+ Map <String , Object > vendorOptions =
123+ (Map <String , Object >) options .getCapability (vendorOptionsCapability );
124+ return (String ) vendorOptions .get ("binary" );
125+ } catch (Exception e ) {
126+ LOG .warning (
127+ String .format (
128+ "Exception while retrieving the browser binary path. %s: %s" ,
129+ options , e .getMessage ()));
130+ }
131+ }
132+ }
133+ return null ;
134+ }
65135}
0 commit comments