FindBinary.pm 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package Selenium::CanStartBinary::FindBinary;
  2. use strict;
  3. use warnings;
  4. # ABSTRACT: Coercions for finding webdriver binaries on your system
  5. use Cwd qw/abs_path/;
  6. use File::Which qw/which/;
  7. use IO::Socket::INET;
  8. use Selenium::Firefox::Binary qw/firefox_path/;
  9. require Exporter;
  10. our @ISA = qw/Exporter/;
  11. our @EXPORT_OK = qw/coerce_simple_binary coerce_firefox_binary/;
  12. use constant IS_WIN => $^O eq 'MSWin32';
  13. =for Pod::Coverage *EVERYTHING*
  14. =cut
  15. sub coerce_simple_binary {
  16. my ($executable) = @_;
  17. my $manual_binary = _validate_manual_binary($executable);
  18. if ($manual_binary) {
  19. return $manual_binary;
  20. }
  21. else {
  22. return _naive_find_binary($executable);
  23. }
  24. }
  25. sub coerce_firefox_binary {
  26. my ($executable) = @_;
  27. my $manual_binary = _validate_manual_binary($executable);
  28. if ($manual_binary) {
  29. return $manual_binary;
  30. }
  31. else {
  32. return firefox_path();
  33. }
  34. }
  35. sub _validate_manual_binary {
  36. my ($executable) = @_;
  37. my $abs_executable = eval {
  38. my $path = abs_path($executable);
  39. die if $path && !-f $path;
  40. $path;
  41. };
  42. if ($abs_executable) {
  43. if ( -x $abs_executable || IS_WIN ) {
  44. return $abs_executable;
  45. }
  46. else {
  47. die 'The binary at '
  48. . $executable
  49. . ' is not executable. Choose the correct file or chmod +x it as needed.';
  50. }
  51. }
  52. }
  53. sub _naive_find_binary {
  54. my ($executable) = @_;
  55. my $naive_binary = which($executable);
  56. if ( defined $naive_binary ) {
  57. return $naive_binary;
  58. }
  59. else {
  60. warn qq(Unable to find the $executable binary in your \$PATH.);
  61. return;
  62. }
  63. }