FindBinary.pm 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 unless -e $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 ' . $executable . ' is not executable. Choose the correct file or chmod +x it as needed.';
  48. }
  49. }
  50. }
  51. sub _naive_find_binary {
  52. my ($executable) = @_;
  53. my $naive_binary = which($executable);
  54. if (defined $naive_binary) {
  55. return $naive_binary;
  56. }
  57. else {
  58. warn qq(Unable to find the $executable binary in your \$PATH.);
  59. return;
  60. }
  61. }