sanity.test 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test2::V0;
  5. #XXX Test2 Knows Better TM
  6. no warnings 'experimental';
  7. use feature qw/signatures/;
  8. use Test::Fatal;
  9. use FindBin;
  10. use Cwd qw{abs_path};
  11. use JSON;
  12. use Data::Dumper;
  13. use Selenium::Client;
  14. my $extra = '';
  15. $extra = '/' if grep { $^O eq $_ } qw{msys MSWin32};
  16. my $sut = 'file://' . $extra . abs_path("$FindBin::Bin/test.html");
  17. my $sut2 = 'file://' . $extra . abs_path("$FindBin::Bin/other.html");
  18. my @browsers = qw{firefox chrome};
  19. push(@browsers, 'MicrosoftEdge') if grep { $^O eq $_ } qw{MSWin32 msys};
  20. push(@browsers, 'safari') if $^O eq 'darwin';
  21. foreach my $browser (@browsers) {
  22. my @drivers = qw{Gecko Auto SeleniumHQ::Jar};
  23. @drivers = qw{Chrome Auto SeleniumHQ::Jar} if $browser eq 'chrome';
  24. @drivers = qw{Edge Auto SeleniumHQ::Jar} if $browser eq 'MicrosoftEdge';
  25. @drivers = qw{Safari Auto SeleniumHQ::Jar} if $browser eq 'safari';
  26. foreach my $driver (@drivers) {
  27. subtest "$browser (using $driver): Spec compliance" => sub {
  28. my %options = ( driver => $driver, browser => $browser, debug => $ENV{DEBUG}, headless => !$ENV{NO_HEADLESS} );
  29. # TODO remove when it goes public
  30. $options{driver_version} = '4.0.0-alpha-7' if $driver eq 'SeleniumHQ::Jar';
  31. my $driver = Selenium::Client->new( %options );
  32. my $status = $driver->Status();
  33. ok($status->{ready}, "Driver up and running");
  34. my ($capabilities,$session) = $driver->NewSession();
  35. isa_ok($capabilities,"Selenium::Capabilities");
  36. isa_ok($session, "Selenium::Session");
  37. is( exception { $session->SetTimeouts( script => 1000, implicit => 1000, pageLoad => 1000 ) }, undef, "Can set timeouts");
  38. #XXX GetTimeouts like every other thing *chokes* on data not being *just right* despite spec having undefined behavior here
  39. my $expected = { script => 1000, implicit => 1000, pageLoad => 1000 };
  40. SKIP: {
  41. skip("Selenium 4.0 GetTimeouts is broken, and as we know all broken things HANG 30 SECONDS!!!",1);
  42. my $t = $session->GetTimeouts( script => undef, implicit => undef, pageLoad => undef );
  43. is($t,$expected, "GetTimeouts works");
  44. }
  45. is( exception { $session->NavigateTo( url => $sut ) }, undef, "Can open page");
  46. #Alerts
  47. alertify($session);
  48. is($session->GetCurrentURL(), $sut, "Can get current URL");
  49. is($session->GetTitle(), 'Test Page', "Can get page title");
  50. is(exception { $session->NavigateTo( 'url' => $sut2 ) }, undef, "Can open other page");
  51. is($session->GetPageSource(),"<html><head></head><body>ZIPPY\n</body></html>","Can get page source");
  52. is(exception { $session->Back() }, undef, "Can navigate to the last page visited with back()");
  53. alertify($session) unless $browser eq 'safari';
  54. is(exception { $session->Forward() }, undef, "Can navigate back to previously visited page with forward()");
  55. $session->Back();
  56. #XXX webkit re-issues alerts on back()
  57. alertify($session) if grep { $browser eq $_ } qw{chrome MicrosoftEdge};
  58. is(exception { $session->Refresh() }, undef, "Can refresh the page");
  59. alertify($session);
  60. my $handle = "".$session->GetWindowHandle();
  61. ok($handle,"Can get window handle");
  62. my $link = $session->FindElement( using => 'css selector', value => '#linky' );
  63. $link->ElementClick();
  64. my @newhandles = map { "".$_ } $session->GetWindowHandles();
  65. my ($newhandle) = grep { $_ ne $handle } @newhandles;
  66. die("Could not get existing handle from getwindowhandles") unless $newhandle;
  67. is( exception { $session->SwitchToWindow( handle => $newhandle ) }, undef, "Can switch to new window");
  68. #XXX This fails on firefoxdriver directly for whatever reason
  69. my $src = eval { $session->GetPageSource() } || '';
  70. like($src, qr/ZIPPY/i, "Got right window") if $browser ne 'firefox' || $driver eq 'SeleniumHQ::Jar';
  71. todo "Get page source after back() sometimes fails on firefox direct driver" => sub {
  72. like($src, qr/ZIPPY/i, "Got right window");
  73. } if $browser eq 'firefox' && $driver ne 'SeleniumHQ::Jar';
  74. is( exception { $session->SwitchToWindow( handle => $handle ) }, undef, "Can switch to old window");
  75. like($session->GetPageSource(), qr/Howdy/i, "Switched window correctly");
  76. $session->SwitchToWindow( handle => $newhandle );
  77. is( exception { $session->CloseWindow() }, undef, "CloseWindow closes current window context");
  78. $session->SwitchToWindow( handle => $handle );
  79. #move it around
  80. my %erekt = ( height => 100, width => 500, x => 50, y => 50);
  81. is( exception { $session->SetWindowRect(%erekt) }, undef, "Can set window rect");
  82. my $rekt = $session->GetWindowRect();
  83. SKIP: {
  84. skip("Window rect set is basically never correctly obeyed",1);
  85. is($rekt, \%erekt, "Can get window rect");
  86. }
  87. #Frames
  88. my $frame = $session->FindElement( using => 'css selector', value => '#frame' );
  89. is( exception { $session->SwitchToFrame( id => $frame->{elementid} ) }, undef, "Can switch into frame");
  90. is( exception { $session->SwitchToParentFrame() }, undef, "Can travel up the frame stack");
  91. #Maximize etc
  92. is( exception { $session->MaximizeWindow() }, undef, "Can maximize window");
  93. is( exception { $session->MinimizeWindow() }, undef, "Can minimize window");
  94. is( exception { $session->FullscreenWindow() }, undef, "Can Fullscreen window");
  95. #Element Method Testing
  96. my $element = $session->FindElement( using => 'css selector', value => 'input[name=text]' );
  97. isa_ok($element,'Selenium::Element');
  98. my $prop = $element->GetElementProperty( name => 'title' );
  99. is($prop,'default', "Can get element properties");
  100. my @inputs = $session->FindElements( using => 'css selector', value => 'input' );
  101. is( scalar(@inputs), 5, "Can find multiple elements correctly");
  102. my $finder = $session->FindElement( using => 'css selector', value => 'form' );
  103. my $found = $finder->FindElementFromElement( using => 'css selector', 'value' => 'label' );
  104. todo "Property/Attribute get is broken in Selenium" => sub {
  105. is($found->GetElementAttribute( name => 'for' ), 'text', "Can find child properly");
  106. };
  107. my @radios = $finder->FindElementsFromElement( using => 'css selector', 'value' => 'input[type=radio]' );
  108. is(scalar(@radios), 2, "Can find child elements properly");
  109. my ($unselected, $selected) = @radios;
  110. ok(!$unselected->IsElementSelected(),"IsElementSelected works");
  111. todo "IsElementSelected appears to always return false, lol" => sub {
  112. ok($selected->IsElementSelected(), "IsElementSelected works");
  113. };
  114. my @checked = $session->FindElements( using => 'css selector', value => 'input:checked');
  115. is(scalar(@checked),1,"But we can at least work around that using css :checked pseudoselector");
  116. is(exception { $session->GetActiveElement() }, undef, "Can get active element");
  117. my $invisible = $session->FindElement( using => 'css selector', value => '#no-see-em' );
  118. is($invisible->GetElementCSSValue( propertyname => 'display' ),'none',"Can get CSS values for elements");
  119. is(lc($invisible->GetElementTagName()),'button', "Can get element tag name");
  120. my $hammertime = $session->FindElement( using => 'css selector', value => '#hammertime' );
  121. ok(!$hammertime->IsElementEnabled(),"IsElementEnabled works");
  122. my $clickme = $session->FindElement( using => 'css selector', value => '#clickme' );
  123. is($clickme->GetElementText(),'PARTY HARD', "Can get element text");
  124. my $rkt = $clickme->GetElementRect();
  125. ok(defined $rkt->{x},"GetElementRect appears to function");
  126. my $input = $session->FindElement( using => 'css selector', value => 'input[name=text]' );
  127. $input->ElementClear();
  128. $input->ElementSendKeys( text => "tickle" );
  129. is($input->GetElementProperty( name => 'value' ), 'tickle', "Can clear and send keys to a text input");
  130. is($session->ExecuteScript( script => qq/ return document.querySelector('input').value /, args => [] ),'tickle',"ExecuteScript works");
  131. is($session->ExecuteAsyncScript( script => qq/ return arguments[arguments.length - 1](document.querySelector('input').value) /, args => [] ),'tickle',"ExecuteAsyncScript works");
  132. # Screenshots
  133. ok($session->TakeScreenshot(),"Can take screenshot");
  134. ok($input->TakeElementScreenshot(), "Can take element screenshot");
  135. # Perform / Release Actions
  136. is( exception {
  137. $session->PerformActions( actions => [ { type => 'key', id => 'key', actions => [ { type => 'keyDown', value => 'a' } ] } ] )
  138. }, undef, "Can perform general actions");
  139. is( exception { $session->ReleaseActions() }, undef, "Can release general actions");
  140. is($input->GetElementProperty( name => 'value' ), 'ticklea', "Key sent worked");
  141. # Cookies -- Browsers don't allow cookies for local stuff, so let's do it against CPAN
  142. # XXX lol this site is slow
  143. $session->SetTimeouts( script => 1000, implicit => 1000, pageLoad => 10000 );
  144. $session->NavigateTo( url => 'http://cpan.org' );
  145. $session->AddCookie( cookie => { name => 'tickle', value => 'hug' } );
  146. my @jar = $session->GetAllCookies();
  147. ok(scalar(grep { $_->{name} eq 'tickle' } @jar), "Can set cookies and read them");
  148. ok($session->GetNamedCookie( name => 'tickle' ),"Can GetNamedCookie");
  149. $session->DeleteCookie( name => 'tickle' );
  150. isnt(exception { $session->GetNamedCookie( name => 'tickle') }, undef, "DeleteCookie works");
  151. $session->AddCookie( cookie => { name => 'tickle', value => 'hug' } );
  152. $session->DeleteAllCookies();
  153. isnt( exception { $session->GetNamedCookie( name => 'tickle' ) }, undef, "DeleteAllCookies works");
  154. # is( exception { $session->DeleteSession() }, undef, "Can delete session");
  155. };
  156. }
  157. }
  158. sub alertify ($session) {
  159. is(eval { $session->GetAlertText() } // $@,'BEEE DOOO', "Can get alert text");
  160. is( exception { $session->AcceptAlert() }, undef, "Can dismiss alert");
  161. is(eval { $session->GetAlertText() } // $@,'Are you a fugitive from Justice?', "Can get alert text on subsequent alert");
  162. is( exception { $session->SendAlertText( text => "HORGLE") }, undef,"send_keys_to_prompt works");
  163. is( exception { $session->DismissAlert() }, undef, "Can accept alert");
  164. }
  165. done_testing();