sanity.test 11 KB

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