Binary.pm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package Selenium::Firefox::Binary;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Subroutines for locating and properly initializing the Firefox Binary
  5. use File::Which qw/which/;
  6. use Selenium::Firefox::Profile;
  7. require Exporter;
  8. our @ISA = qw/Exporter/;
  9. our @EXPORT_OK = qw/firefox_path setup_firefox_binary_env/;
  10. sub _firefox_windows_path {
  11. # TODO: make this slightly less dumb
  12. my @program_files = (
  13. $ENV{PROGRAMFILES} // 'C:\Program Files',
  14. $ENV{'PROGRAMFILES(X86)'} // 'C:\Program Files (x86)',
  15. );
  16. foreach (@program_files) {
  17. my $binary_path = $_ . '\Mozilla Firefox\firefox.exe';
  18. return $binary_path if -x $binary_path;
  19. }
  20. # Fall back to a completely naive strategy
  21. warn
  22. q/We couldn't find a viable firefox.EXE; you may want to specify it via the binary attribute./;
  23. return which('firefox');
  24. }
  25. sub _firefox_darwin_path {
  26. my $default_firefox =
  27. '/Applications/Firefox.app/Contents/MacOS/firefox-bin';
  28. if ( -e $default_firefox && -x $default_firefox ) {
  29. return $default_firefox;
  30. }
  31. else {
  32. return which('firefox-bin');
  33. }
  34. }
  35. sub _firefox_unix_path {
  36. # TODO: maybe which('firefox3'), which('firefox2') ?
  37. return which('firefox') || '/usr/bin/firefox';
  38. }
  39. =head1 SUBROUTINES
  40. =head2 firefox_path
  41. Return the path to the firefox binary on your system.
  42. =cut
  43. sub firefox_path {
  44. my $path;
  45. if ( $^O eq 'MSWin32' ) {
  46. $path = _firefox_windows_path();
  47. }
  48. elsif ( $^O eq 'darwin' ) {
  49. $path = _firefox_darwin_path();
  50. }
  51. else {
  52. $path = _firefox_unix_path;
  53. }
  54. if ( not -x $path ) {
  55. die $path . ' is not an executable file.';
  56. }
  57. return $path;
  58. }
  59. =head2 setup_firefox_binary_env
  60. Sets various environment variables to make firefox work correctly with webDriver.
  61. =cut
  62. # We want the profile to persist to the end of the session, not just
  63. # the end of this function.
  64. my $profile;
  65. sub setup_firefox_binary_env {
  66. my ( $port, $marionette_port, $caller_profile ) = @_;
  67. $profile = $caller_profile || Selenium::Firefox::Profile->new;
  68. $profile->add_webdriver( $port, $marionette_port );
  69. $profile->add_marionette($marionette_port);
  70. # For non-geckodriver/marionette startup, we instruct Firefox to
  71. # use the profile by specifying the appropriate environment
  72. # variables for it to hook onto.
  73. if ( !$marionette_port ) {
  74. $ENV{'XRE_PROFILE_PATH'} = $profile->_layout_on_disk;
  75. $ENV{'MOZ_NO_REMOTE'} = '1'; # able to launch multiple instances
  76. $ENV{'MOZ_CRASHREPORTER_DISABLE'} = '1'; # disable breakpad
  77. $ENV{'NO_EM_RESTART'} =
  78. '1'; # prevent the binary from detaching from the console.log
  79. }
  80. else {
  81. # In case the user created an old Firefox, which would've set
  82. # those ENV variables, and then wanted to create a new Firefox
  83. # afterwards, the env variables would still be around, and the
  84. # new Firefox would respect the XRE_PROFILE_PATH and try to
  85. # load it in the new geckodriver Firefox, which would cause an
  86. # extension compatibility check
  87. my @env_vars = qw/
  88. XRE_PROFILE_PATH
  89. MOZ_NO_REMOTE
  90. MOZ_CRASHREPORTER_DISABLE
  91. NO_EM_RESTART
  92. /;
  93. foreach (@env_vars) {
  94. delete $ENV{$_};
  95. }
  96. }
  97. return $profile;
  98. }
  99. 1;