1
0

Binary.pm 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 q/We couldn't find a viable firefox.EXE; you may want to specify it via the binary attribute./;
  22. return which('firefox');
  23. }
  24. sub _firefox_darwin_path {
  25. my $default_firefox = '/Applications/Firefox.app/Contents/MacOS/firefox-bin';
  26. if (-e $default_firefox && -x $default_firefox) {
  27. return $default_firefox
  28. }
  29. else {
  30. return which('firefox-bin');
  31. }
  32. }
  33. sub _firefox_unix_path {
  34. # TODO: maybe which('firefox3'), which('firefox2') ?
  35. return which('firefox') || '/usr/bin/firefox';
  36. }
  37. =head1 SUBROUTINES
  38. =head2 firefox_path
  39. Return the path to the firefox binary on your system.
  40. =cut
  41. sub firefox_path {
  42. my $path;
  43. if ($^O eq 'MSWin32') {
  44. $path =_firefox_windows_path();
  45. }
  46. elsif ($^O eq 'darwin') {
  47. $path = _firefox_darwin_path();
  48. }
  49. else {
  50. $path = _firefox_unix_path;
  51. }
  52. if (not -x $path) {
  53. die $path . ' is not an executable file.';
  54. }
  55. return $path;
  56. }
  57. =head2 setup_firefox_binary_env
  58. Sets various environment variables to make firefox work correctly with webDriver.
  59. =cut
  60. # We want the profile to persist to the end of the session, not just
  61. # the end of this function.
  62. my $profile;
  63. sub setup_firefox_binary_env {
  64. my ($port, $marionette_port, $caller_profile) = @_;
  65. $profile = $caller_profile || Selenium::Firefox::Profile->new;
  66. $profile->add_webdriver($port, $marionette_port);
  67. $profile->add_marionette($marionette_port);
  68. # For non-geckodriver/marionette startup, we instruct Firefox to
  69. # use the profile by specifying the appropriate environment
  70. # variables for it to hook onto.
  71. if (! $marionette_port) {
  72. $ENV{'XRE_PROFILE_PATH'} = $profile->_layout_on_disk;
  73. $ENV{'MOZ_NO_REMOTE'} = '1'; # able to launch multiple instances
  74. $ENV{'MOZ_CRASHREPORTER_DISABLE'} = '1'; # disable breakpad
  75. $ENV{'NO_EM_RESTART'} = '1'; # prevent the binary from detaching from the console.log
  76. }
  77. else {
  78. # In case the user created an old Firefox, which would've set
  79. # those ENV variables, and then wanted to create a new Firefox
  80. # afterwards, the env variables would still be around, and the
  81. # new Firefox would respect the XRE_PROFILE_PATH and try to
  82. # load it in the new geckodriver Firefox, which would cause an
  83. # extension compatibility check
  84. my @env_vars = qw/
  85. XRE_PROFILE_PATH
  86. MOZ_NO_REMOTE
  87. MOZ_CRASHREPORTER_DISABLE
  88. NO_EM_RESTART
  89. /;
  90. foreach (@env_vars) {
  91. delete $ENV{$_};
  92. }
  93. }
  94. return $profile;
  95. }
  96. 1;