CanStartBinary.t 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Which qw/which/;
  5. use Selenium::Chrome;
  6. use Selenium::Firefox;
  7. use Selenium::Firefox::Binary;
  8. use Selenium::PhantomJS;
  9. use Sub::Install;
  10. use Test::Fatal;
  11. use Test::More;
  12. unless ( $ENV{RELEASE_TESTING} ) {
  13. plan skip_all => "Author tests not required for installation.";
  14. }
  15. PHANTOMJS: {
  16. SKIP: {
  17. my $has_phantomjs = which('phantomjs');
  18. skip 'Phantomjs binary not found in path', 3
  19. unless $has_phantomjs;
  20. skip 'PhantomJS binary not found in path', 3
  21. unless is_proper_phantomjs_available();
  22. my $phantom = Selenium::PhantomJS->new;
  23. is( $phantom->browser_name, 'phantomjs', 'binary phantomjs is okay');
  24. isnt( $phantom->port, 4444, 'phantomjs can start up its own binary');
  25. ok( Selenium::CanStartBinary::probe_port( $phantom->port ), 'the phantomjs binary is listening on its port');
  26. }
  27. }
  28. MANUAL: {
  29. ok( exception { PhantomJS->new( binary => '/bad/executable') },
  30. 'we throw if the user specified binary is not executable');
  31. SKIP: {
  32. my $phantom_binary = which('phantomjs');
  33. skip 'PhantomJS needed for manual binary path tests', 2
  34. unless $phantom_binary;
  35. my $manual_phantom = Selenium::PhantomJS->new(
  36. binary => $phantom_binary
  37. );
  38. isnt( $manual_phantom->port, 4444, 'manual phantom can start up user specified binary');
  39. ok( Selenium::CanStartBinary::probe_port( $manual_phantom->port ), 'the manual chrome binary is listening on its port');
  40. }
  41. }
  42. CHROME: {
  43. SKIP: {
  44. my $has_chromedriver = which('chromedriver');
  45. skip 'Chrome binary not found in path', 3
  46. unless $has_chromedriver;
  47. my $chrome = Selenium::Chrome->new;
  48. ok( $chrome->browser_name eq 'chrome', 'convenience chrome is okay' );
  49. isnt( $chrome->port, 4444, 'chrome can start up its own binary');
  50. ok( Selenium::CanStartBinary::probe_port( $chrome->port ), 'the chrome binary is listening on its port');
  51. }
  52. }
  53. FIREFOX: {
  54. SKIP: {
  55. skip 'Firefox will not start up on UNIX without a display', 3
  56. if ($^O ne 'MSWin32' && ! $ENV{DISPLAY});
  57. my $binary = Selenium::Firefox::Binary::firefox_path();
  58. skip 'Firefox binary not found in path', 3
  59. unless $binary;
  60. ok(-x $binary, 'we can find some sort of firefox');
  61. my $firefox = Selenium::Firefox->new;
  62. isnt( $firefox->port, 4444, 'firefox can start up its own binary');
  63. ok( Selenium::CanStartBinary::probe_port( $firefox->port ), 'the firefox binary is listening on its port');
  64. }
  65. }
  66. TIMEOUT: {
  67. my $binary = Selenium::Firefox::Binary::firefox_path();
  68. skip 'Firefox binary not found in path', 3
  69. unless $binary;
  70. # Force the port check to exhaust the wait_until timeout so that
  71. # we can exercise the startup_timeout constructor option
  72. # functionality.
  73. Sub::Install::reinstall_sub({
  74. code => sub { 0 },
  75. into => 'Selenium::CanStartBinary',
  76. as => 'probe_port'
  77. });
  78. my $start = time;
  79. eval { Selenium::Firefox->new( startup_timeout => 1 ) };
  80. # The test leaves a bit of a cushion to handle any unexpected
  81. # latency issues when starting up the browser - the important part
  82. # is that our timeout duration is _not_ the default 10 seconds.
  83. ok( time - $start < 5, 'We can specify how long to wait for a binary to be available' );
  84. }
  85. sub is_proper_phantomjs_available {
  86. my $ver = `phantomjs --version` // '';
  87. chomp $ver;
  88. $ver =~ s/^(\d\.\d).*/$1/;
  89. return $ver >= 1.9;
  90. }
  91. done_testing;