1717
1818package org .openqa .selenium .firefox ;
1919
20+ import static org .openqa .selenium .firefox .FirefoxDriver .SystemProperty .BROWSER_LOGFILE ;
2021import static org .openqa .selenium .firefox .FirefoxDriver .SystemProperty .DRIVER_USE_MARIONETTE ;
2122import static org .openqa .selenium .firefox .FirefoxOptions .FIREFOX_OPTIONS ;
2223import static org .openqa .selenium .remote .CapabilityType .PROXY ;
2526import com .google .common .collect .Sets ;
2627
2728import org .openqa .selenium .Capabilities ;
28- import org .openqa .selenium .NoSuchSessionException ;
2929import org .openqa .selenium .Proxy ;
3030import org .openqa .selenium .WebDriverException ;
31- import org .openqa .selenium .firefox .internal .NewProfileExtensionConnection ;
32- import org .openqa .selenium .internal .Killable ;
33- import org .openqa .selenium .internal .Lock ;
34- import org .openqa .selenium .internal .SocketLock ;
35- import org .openqa .selenium .logging .LocalLogs ;
36- import org .openqa .selenium .logging .NeedsLocalLogs ;
3731import org .openqa .selenium .remote .BeanToJsonConverter ;
38- import org .openqa .selenium .remote .Command ;
3932import org .openqa .selenium .remote .CommandExecutor ;
4033import org .openqa .selenium .remote .DesiredCapabilities ;
41- import org .openqa .selenium .remote .DriverCommand ;
4234import org .openqa .selenium .remote .FileDetector ;
4335import org .openqa .selenium .remote .RemoteWebDriver ;
44- import org .openqa .selenium .remote .Response ;
4536import org .openqa .selenium .remote .service .DriverCommandExecutor ;
37+ import org .openqa .selenium .remote .service .DriverService ;
4638
39+ import java .io .File ;
4740import java .io .IOException ;
48- import java .net .URI ;
4941import java .util .Map ;
50- import java .util .Optional ;
5142import java .util .Set ;
5243import java .util .logging .Logger ;
5344
6354 *WebDriver driver = new FirefoxDriver(options);
6455 * </pre>
6556 */
66- public class FirefoxDriver extends RemoteWebDriver implements Killable {
57+ public class FirefoxDriver extends RemoteWebDriver {
6758
6859 public static final class SystemProperty {
6960
@@ -233,17 +224,22 @@ private FirefoxDriver(
233224 }
234225
235226 private static CommandExecutor toExecutor (FirefoxOptions options ) {
236- if (options .isLegacy ()) {
237- return new FirefoxDriver .LazyCommandExecutor (options .getBinary (), options .getProfile ());
227+ DriverService .Builder builder ;
238228
229+ if (options .isLegacy ()) {
230+ builder = XpiDriverService .builder ()
231+ .withBinary (options .getBinaryOrNull ().orElseGet (FirefoxBinary ::new ))
232+ .withProfile (options .getProfile ());
239233 } else {
240- GeckoDriverService .Builder builder = new GeckoDriverService .Builder ().usingPort (0 );
241- Optional <FirefoxBinary > binary = options .getBinaryOrNull ();
242- if (binary .isPresent ()) {
243- builder .usingFirefoxBinary (binary .get ());
244- }
245- return new DriverCommandExecutor (builder .build ());
234+ builder = new GeckoDriverService .Builder ()
235+ .usingFirefoxBinary (options .getBinaryOrNull ().orElseGet (FirefoxBinary ::new ));
246236 }
237+
238+ if (System .getProperty (BROWSER_LOGFILE ) != null ) {
239+ builder .withLogFile (new File (System .getProperty (System .getProperty (BROWSER_LOGFILE ))));
240+ }
241+
242+ return new DriverCommandExecutor (builder .build ());
247243 }
248244
249245 private static FirefoxOptions getFirefoxOptions (Capabilities capabilities ) {
@@ -295,16 +291,6 @@ public void setFileDetector(FileDetector detector) {
295291 "via RemoteWebDriver" );
296292 }
297293
298- /**
299- * Attempt to forcibly kill this Killable at the OS level. Useful where the extension has
300- * stopped responding, and you don't want to leak resources. Should not ordinarily be called.
301- */
302- public void kill () {
303- if (this .getCommandExecutor () instanceof LazyCommandExecutor ) {
304- ((LazyCommandExecutor ) this .getCommandExecutor ()).binary .quit ();
305- }
306- }
307-
308294 private static boolean isLegacy (Capabilities desiredCapabilities ) {
309295 Boolean forceMarionette = forceMarionetteFromSystemProperty ();
310296 if (forceMarionette != null ) {
@@ -322,46 +308,6 @@ private static Boolean forceMarionetteFromSystemProperty() {
322308 return Boolean .valueOf (useMarionette );
323309 }
324310
325- @ Override
326- protected void startClient (Capabilities desiredCapabilities , Capabilities requiredCapabilities ) {
327- if (isLegacy (desiredCapabilities )) {
328- LazyCommandExecutor exe = (LazyCommandExecutor ) getCommandExecutor ();
329-
330- // TODO(simon): Make this not sinfully ugly
331- ExtensionConnection connection = connectTo (exe .binary , exe .profile , "localhost" );
332- exe .setConnection (connection );
333-
334- try {
335- connection .start ();
336- } catch (IOException e ) {
337- throw new WebDriverException ("An error occurred while connecting to Firefox" , e );
338- }
339-
340- }
341- }
342-
343- protected ExtensionConnection connectTo (FirefoxBinary binary , FirefoxProfile profile ,
344- String host ) {
345- Lock lock = obtainLock (profile );
346- try {
347- FirefoxBinary bin = binary == null ? new FirefoxBinary () : binary ;
348- return new NewProfileExtensionConnection (lock , bin , profile , host );
349- } catch (Exception e ) {
350- throw new WebDriverException (e );
351- }
352- }
353-
354- protected Lock obtainLock (FirefoxProfile profile ) {
355- return new SocketLock ();
356- }
357-
358- @ Override
359- protected void stopClient () {
360- if (this .getCommandExecutor () instanceof LazyCommandExecutor ) {
361- ((LazyCommandExecutor ) this .getCommandExecutor ()).quit ();
362- }
363- }
364-
365311 /**
366312 * Drops capabilities that we shouldn't send over the wire.
367313 *
@@ -391,53 +337,4 @@ private static Capabilities dropCapabilities(Capabilities capabilities) {
391337
392338 return caps ;
393339 }
394-
395- public static class LazyCommandExecutor implements CommandExecutor , NeedsLocalLogs {
396- private ExtensionConnection connection ;
397- private final FirefoxBinary binary ;
398- private final FirefoxProfile profile ;
399- private LocalLogs logs = LocalLogs .getNullLogger ();
400-
401- LazyCommandExecutor (FirefoxBinary binary , FirefoxProfile profile ) {
402- this .binary = binary ;
403- this .profile = profile ;
404- }
405-
406- public void setConnection (ExtensionConnection connection ) {
407- this .connection = connection ;
408- connection .setLocalLogs (logs );
409- }
410-
411- public void quit () {
412- if (connection != null ) {
413- connection .quit ();
414- connection = null ;
415- }
416- if (profile != null ) {
417- profile .cleanTemporaryModel ();
418- }
419- }
420-
421- public Response execute (Command command ) throws IOException {
422- if (connection == null ) {
423- if (command .getName ().equals (DriverCommand .QUIT )) {
424- return new Response ();
425- }
426- throw new NoSuchSessionException (
427- "The FirefoxDriver cannot be used after quit() was called." );
428- }
429- return connection .execute (command );
430- }
431-
432- public void setLocalLogs (LocalLogs logs ) {
433- this .logs = logs ;
434- if (connection != null ) {
435- connection .setLocalLogs (logs );
436- }
437- }
438-
439- public URI getAddressOfRemoteServer () {
440- return connection .getAddressOfRemoteServer ();
441- }
442- }
443340}
0 commit comments