Binary.pm 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package Selenium::Firefox::Binary;
  2. # ABSTRACT: Subroutines for locating and properly initializing the Firefox Binary
  3. use File::Which qw/which/;
  4. use Selenium::Firefox::Profile;
  5. require Exporter;
  6. our @ISA = qw/Exporter/;
  7. our @EXPORT_OK = qw/firefox_path setup_firefox_binary_env/;
  8. sub _firefox_windows_path {
  9. # TODO: make this slightly less dumb
  10. my @program_files = (
  11. $ENV{PROGRAMFILES} // 'C:\Program Files',
  12. $ENV{'PROGRAMFILES(X86)'} // 'C:\Program Files (x86)',
  13. );
  14. foreach (@program_files) {
  15. my $binary_path = $_ . '\Mozilla Firefox\firefox.exe';
  16. return $binary_path if -x $binary_path;
  17. }
  18. # Fall back to a completely naive strategy
  19. warn q/We couldn't find a viable firefox.EXE; you may want to specify it via the binary attribute./;
  20. return which('firefox');
  21. }
  22. sub _firefox_darwin_path {
  23. my $default_firefox = '/Applications/Firefox.app/Contents/MacOS/firefox-bin';
  24. if (-e $default_firefox && -x $default_firefox) {
  25. return $default_firefox
  26. }
  27. else {
  28. return which('firefox-bin');
  29. }
  30. }
  31. sub _firefox_unix_path {
  32. # TODO: maybe which('firefox3'), which('firefox2') ?
  33. return which('firefox') || '/usr/bin/firefox';
  34. }
  35. sub firefox_path {
  36. my $path;
  37. if ($^O eq 'MSWin32') {
  38. $path =_firefox_windows_path();
  39. }
  40. elsif ($^O eq 'darwin') {
  41. $path = _firefox_darwin_path();
  42. }
  43. else {
  44. $path = _firefox_unix_path;
  45. }
  46. if (not -x $path) {
  47. die $path . ' is not an executable file.';
  48. }
  49. return $path;
  50. }
  51. # We want the profile to persist to the end of the session, not just
  52. # the end of this function.
  53. my $profile;
  54. sub setup_firefox_binary_env {
  55. my ($port, $marionette_port, $caller_profile) = @_;
  56. $profile = $caller_profile || Selenium::Firefox::Profile->new;
  57. $profile->add_webdriver($port);
  58. $profile->add_marionette($marionette_port);
  59. # For non-geckodriver/marionette startup, we instruct Firefox to
  60. # use the profile by specifying the appropriate environment
  61. # variables for it to hook onto.
  62. if (! $marionette_port) {
  63. $ENV{'XRE_PROFILE_PATH'} = $profile->_layout_on_disk;
  64. $ENV{'MOZ_NO_REMOTE'} = '1'; # able to launch multiple instances
  65. $ENV{'MOZ_CRASHREPORTER_DISABLE'} = '1'; # disable breakpad
  66. $ENV{'NO_EM_RESTART'} = '1'; # prevent the binary from detaching from the console.log
  67. }
  68. return $profile;
  69. }
  70. 1;