sanity.test 13 KB

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