BinaryModeBuilder.pm 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package Selenium::BinaryModeBuilder;
  2. # ABSTRACT: Teach a WebDriver how to start its own binary aka no JRE!
  3. use File::Which qw/which/;
  4. use IO::Socket::INET;
  5. use Selenium::Waiter qw/wait_until/;
  6. use Selenium::Firefox::Binary qw/firefox_path setup_firefox_binary_env/;
  7. use Selenium::Firefox::Profile;
  8. use Try::Tiny;
  9. use Moo::Role;
  10. has 'binary_mode' => (
  11. is => 'ro',
  12. init_arg => undef,
  13. builder => 1
  14. );
  15. sub _build_binary_mode {
  16. my ($self) = @_;
  17. if (! $self->has_remote_server_addr && ! $self->has_port) {
  18. try {
  19. my $port = start_binary_on_port($self->binary_name, $self->binary_port);
  20. $self->port($port);
  21. return 1;
  22. }
  23. catch {
  24. warn $_;
  25. return 0;
  26. }
  27. }
  28. else {
  29. return 0;
  30. }
  31. }
  32. sub probe_port {
  33. my ($port) = @_;
  34. return IO::Socket::INET->new(
  35. PeerAddr => '127.0.0.1',
  36. PeerPort => $port,
  37. Timeout => 3
  38. );
  39. }
  40. sub start_binary_on_port {
  41. my ($process, $port) = @_;
  42. my $executable = _find_executable($process);
  43. $port = _find_open_port_above($port);
  44. if ($process eq 'firefox') {
  45. setup_firefox_binary_env($port);
  46. }
  47. my $command = _construct_command($executable, $port);
  48. system($command);
  49. my $success = wait_until { probe_port($port) } timeout => 10;
  50. if ($success) {
  51. return $port;
  52. }
  53. else {
  54. die 'Unable to connect to the ' . $executable . ' binary on port ' . $port;
  55. }
  56. }
  57. sub _find_executable {
  58. my ($binary) = @_;
  59. if ($binary eq 'firefox') {
  60. return firefox_path();
  61. }
  62. else {
  63. my $executable = which($binary);
  64. if (not defined $executable) {
  65. die qq(Unable to find the $binary binary in your \$PATH. We'll try falling back to standard Remote Driver);
  66. }
  67. else {
  68. return $executable;
  69. }
  70. }
  71. }
  72. sub _construct_command {
  73. my ($executable, $port) = @_;
  74. my %args;
  75. if ($executable =~ /chromedriver$/) {
  76. %args = (
  77. port => $port,
  78. 'base-url' => 'wd/hub'
  79. );
  80. }
  81. elsif ($executable =~ /phantomjs$/) {
  82. %args = (
  83. webdriver => '127.0.0.1:' . $port
  84. );
  85. }
  86. elsif ($executable =~ /firefox/i) {
  87. $executable .= ' -no-remote ';
  88. }
  89. my @args = map { '--' . $_ . '=' . $args{$_} } keys %args;
  90. return join(' ', ($executable, @args, '> /dev/null 2>&1 &') );
  91. }
  92. sub _find_open_port_above {
  93. my ($port) = @_;
  94. my $free_port = wait_until {
  95. if ( probe_port($port) ) {
  96. $port++;
  97. return 0;
  98. }
  99. else {
  100. return $port;
  101. }
  102. };
  103. return $free_port;
  104. }
  105. 1;