CanStartBinary.t 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. use strict;
  2. use warnings;
  3. use File::Which qw/which/;
  4. use Selenium::Chrome;
  5. use Selenium::Firefox;
  6. use Selenium::Firefox::Binary;
  7. use Selenium::PhantomJS;
  8. use Sub::Install;
  9. use Test::Fatal;
  10. use Test::More;
  11. unless ( $ENV{RELEASE_TESTING} ) {
  12. plan skip_all => "Author tests not required for installation.";
  13. }
  14. PHANTOMJS: {
  15. SKIP: {
  16. my $has_phantomjs = which('phantomjs');
  17. skip 'Phantomjs binary not found in path', 3
  18. unless $has_phantomjs;
  19. skip 'PhantomJS binary not found in path', 3
  20. unless is_proper_phantomjs_available();
  21. my $phantom = Selenium::PhantomJS->new;
  22. is( $phantom->browser_name, 'phantomjs', 'binary phantomjs is okay');
  23. isnt( $phantom->port, 4444, 'phantomjs can start up its own binary');
  24. ok( Selenium::CanStartBinary::probe_port( $phantom->port ), 'the phantomjs binary is listening on its port');
  25. }
  26. }
  27. MANUAL: {
  28. ok( exception { PhantomJS->new( binary => '/bad/executable') },
  29. 'we throw if the user specified binary is not executable');
  30. SKIP: {
  31. my $phantom_binary = which('phantomjs');
  32. skip 'PhantomJS needed for manual binary path tests', 2
  33. unless $phantom_binary;
  34. my $manual_phantom = Selenium::PhantomJS->new(
  35. binary => $phantom_binary
  36. );
  37. isnt( $manual_phantom->port, 4444, 'manual phantom can start up user specified binary');
  38. ok( Selenium::CanStartBinary::probe_port( $manual_phantom->port ), 'the manual chrome binary is listening on its port');
  39. }
  40. }
  41. CHROME: {
  42. SKIP: {
  43. my $has_chromedriver = which('chromedriver');
  44. skip 'Chrome binary not found in path', 3
  45. unless $has_chromedriver;
  46. my $chrome = Selenium::Chrome->new(
  47. custom_args => ' --fake-arg'
  48. );
  49. like( $chrome->_construct_command, qr/--fake-arg/, 'can pass custom args');
  50. ok( $chrome->browser_name eq 'chrome', 'convenience chrome is okay' );
  51. isnt( $chrome->port, 4444, 'chrome can start up its own binary' );
  52. like( $chrome->_binary_args, qr/--url-base=wd\/hub/, 'chrome has correct webdriver context' );
  53. ok( Selenium::CanStartBinary::probe_port( $chrome->port ), 'the chrome binary is listening on its port');
  54. }
  55. }
  56. FIREFOX: {
  57. SKIP: {
  58. skip 'Firefox will not start up on UNIX without a display', 3
  59. if ($^O ne 'MSWin32' && ! $ENV{DISPLAY});
  60. my $binary = Selenium::Firefox::Binary::firefox_path();
  61. skip 'Firefox binary not found in path', 3
  62. unless $binary;
  63. ok(-x $binary, 'we can find some sort of firefox');
  64. my $firefox = Selenium::Firefox->new;
  65. isnt( $firefox->port, 4444, 'firefox can start up its own binary');
  66. ok( Selenium::CanStartBinary::probe_port( $firefox->port ), 'the firefox binary is listening on its port');
  67. $firefox->shutdown_binary;
  68. PROFILE: {
  69. my $encoded = 0;
  70. {
  71. package FFProfile;
  72. use Moo;
  73. extends 'Selenium::Firefox::Profile';
  74. sub _encode { $encoded++ };
  75. 1;
  76. }
  77. my $p = FFProfile->new;
  78. my $firefox_with_profile = Selenium::Firefox->new(firefox_profile => $p);
  79. $firefox_with_profile->shutdown_binary;
  80. is($encoded, 0, 'Binary firefox does not encode profile unnecessarily');
  81. }
  82. my $firefox_marionette = Selenium::Firefox->new(
  83. marionette_enabled => 1
  84. );
  85. isnt( $firefox->port, 4444, 'firefox can start up its own binary');
  86. ok( Selenium::CanStartBinary::probe_port( $firefox_marionette->marionette_port ), 'the firefox binary with marionette enabled is listening on its marionette port');
  87. }
  88. }
  89. TIMEOUT: {
  90. my $binary = Selenium::Firefox::Binary::firefox_path();
  91. skip 'Firefox binary not found in path', 3
  92. unless $binary;
  93. # Force the port check to exhaust the wait_until timeout so that
  94. # we can exercise the startup_timeout constructor option
  95. # functionality.
  96. Sub::Install::reinstall_sub({
  97. code => sub { return 0 },
  98. into => 'Selenium::CanStartBinary',
  99. as => 'probe_port'
  100. });
  101. my $start = time;
  102. eval { Selenium::Firefox->new( startup_timeout => 1 ) };
  103. # The test leaves a bit of a cushion to handle any unexpected
  104. # latency issues when starting up the browser - the important part
  105. # is that our timeout duration is _not_ the default 10 seconds.
  106. ok( time - $start < 10, 'We can specify how long to wait for a binary to be available' );
  107. }
  108. sub is_proper_phantomjs_available {
  109. my $ver = `phantomjs --version` // '';
  110. chomp $ver;
  111. $ver =~ s/^(\d\.\d).*/$1/;
  112. return $ver >= 1.9;
  113. }
  114. done_testing;