example.pl 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. use strict;
  2. use warnings;
  3. use Data::Dumper;
  4. use Playwright;
  5. use Try::Tiny;
  6. use Net::EmptyPort;
  7. use Carp::Always;
  8. NORMAL: {
  9. my $handle = Playwright->new( debug => 1 );
  10. # Open a new chrome instance
  11. my $browser = $handle->launch( headless => 1, type => 'firefox' );
  12. my $process = $handle->server( browser => $browser, command => 'process' );
  13. print "Browser PID: ".$process->{pid}."\n";
  14. # Open a tab therein
  15. my $page = $browser->newPage({ videosPath => 'video', acceptDownloads => 1 });
  16. # Test the spec method
  17. print Dumper($page->spec(),$page);
  18. # Browser contexts don't exist until you open at least one page.
  19. # You'll need this to grab and set cookies.
  20. my ($context) = @{$browser->contexts()};
  21. # Load a URL in the tab
  22. my $res = $page->goto('http://troglodyne.net', { waitUntil => 'networkidle' });
  23. print Dumper($res->status(), $browser->version());
  24. # Put your hand in the jar
  25. my $cookies = $context->cookies();
  26. print Dumper($cookies);
  27. # Grab the main frame, in case this is a frameset
  28. my $frameset = $page->mainFrame();
  29. print Dumper($frameset->childFrames());
  30. # Run some JS
  31. my $fun = "
  32. var input = arguments[0];
  33. return {
  34. width: document.documentElement.clientWidth,
  35. height: document.documentElement.clientHeight,
  36. deviceScaleFactor: window.devicePixelRatio,
  37. arg: input
  38. };";
  39. my $result = $page->evaluate($fun, 'zippy');
  40. print Dumper($result);
  41. # Read the console
  42. $page->on('console',"return [...arguments]");
  43. my $promise = $page->waitForEvent('console');
  44. #XXX this *can* race
  45. sleep 1;
  46. $page->evaluate("console.log('hug')");
  47. my $console_log = $handle->await( $promise );
  48. print "Logged to console: '".$console_log->text()."'\n";
  49. # Use a selector to find which input is visible and type into it
  50. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  51. my $inputs = $page->selectMulti('input');
  52. foreach my $input (@$inputs) {
  53. try {
  54. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  55. $input->fill('tickle', { timeout => 250 } );
  56. } catch {
  57. print "Element not visible, skipping...\n";
  58. }
  59. }
  60. # Said better selector
  61. my $actual_input = $page->select('input[name=like]');
  62. $actual_input->fill('whee');
  63. # Ensure we can grab the parent (convenience)
  64. print "Got Parent: ISA ".ref($actual_input->{parent})."\n";
  65. # Take screen of said element
  66. $actual_input->screenshot({ path => 'test.jpg' });
  67. # Fiddle with HIDs
  68. my $mouse = $page->mouse;
  69. $mouse->move( 0, 0 );
  70. my $keyboard = $page->keyboard();
  71. $keyboard->type('F12');
  72. # Start to do some more advanced actions with the page
  73. use FindBin;
  74. use Cwd qw{abs_path};
  75. my $pg = abs_path("$FindBin::Bin/at/test.html");
  76. # Handle dialogs on page start, and dialog after dialog
  77. # NOTE -- the 'load' event won't fire until the dialog is dismissed in some browsers
  78. $promise = $page->waitForEvent('dialog');
  79. $page->goto("file://$pg", { waitUntil => 'networkidle' });
  80. my $dlg = $handle->await($promise);
  81. $promise = $page->waitForEvent('dialog');
  82. $dlg->dismiss();
  83. $dlg = $handle->await($promise);
  84. $dlg->accept();
  85. # Download stuff -- note this requries acceptDownloads = true in the page open
  86. # NOTE -- the 'download' event fires unreliably, as not all browsers properly obey the 'download' property in hrefs.
  87. # Chrome, for example would choke here on an intermediate dialog.
  88. $promise = $page->waitForEvent('download');
  89. $page->select('#d-lo')->click();
  90. my $download = $handle->await( $promise );
  91. print "Download suggested filename\n";
  92. print $download->suggestedFilename()."\n";
  93. $download->saveAs('test2.jpg');
  94. # Fiddle with file inputs
  95. my $choochoo = $page->waitForEvent('filechooser');
  96. $page->select('#drphil')->click();
  97. my $chooseu = $handle->await( $choochoo );
  98. $chooseu->setFiles('test.jpg');
  99. # Make sure we can do child selectors
  100. my $parent = $page->select('body');
  101. my $child = $parent->select('#drphil');
  102. print ref($child)."\n";
  103. # Test out pusht/popt/try_until
  104. # Timeouts are in milliseconds
  105. Playwright::pusht($page,5000);
  106. my $checkpoint = time();
  107. my $element = Playwright::try_until($page, 'select', 'bogus-bogus-nothere');
  108. my $elapsed = time() - $checkpoint;
  109. Playwright::popt($page);
  110. print "Waited $elapsed seconds for timeout to drop\n";
  111. $checkpoint = time();
  112. $element = Playwright::try_until($page, 'select', 'bogus-bogus-nothere');
  113. $elapsed = time() - $checkpoint;
  114. print "Waited $elapsed seconds for timeout to drop\n";
  115. # Try out the API testing extensions
  116. print "HEAD http://troglodyne.net : \n";
  117. my $fr = $page->request;
  118. my $resp = $fr->fetch("http://troglodyne.net", { method => "HEAD" });
  119. print Dumper($resp->headers());
  120. print "200 OK\n" if $resp->status() == 200;
  121. # Test that we can do stuff with with the new locator API.
  122. my $loc = $page->locator('body');
  123. my $innerTubes = $loc->allInnerTexts();
  124. print Dumper($innerTubes);
  125. my $image = $page->getByAltText('picture');
  126. print Dumper($image);
  127. # Save a video now that we are done
  128. my $bideo = $page->video;
  129. # IT IS IMPORTANT TO CLOSE THE PAGE FIRST OR THIS WILL HANG!
  130. $page->close();
  131. my $vidpath = $bideo->saveAs('video/example.webm');
  132. }
  133. # Example of using persistent mode / remote hosts
  134. OPEN: {
  135. my $handle = Playwright->new( debug => 1 );
  136. my $handle2 = Playwright->new( debug => 1, host => 'localhost', port => $handle->{port} );
  137. my $browser = $handle2->launch( headless => 1, type => 'firefox' );
  138. my $process = $handle2->server( browser => $browser, command => 'process' );
  139. print "Browser PID: ".$process->{pid}."\n";
  140. }
  141. # Example of connecting to remote CDP sessions
  142. CDP: {
  143. local $SIG{HUP} = 'IGNORE';
  144. sub kill_krom_and_die {
  145. my ($in, $msg) = @_;
  146. kill_krom($in);
  147. die $msg;
  148. }
  149. sub kill_krom {
  150. my ($in) = @_;
  151. kill HUP => -getpgrp();
  152. close $in;
  153. }
  154. my $port = Net::EmptyPort::empty_port();
  155. my $pid = fork // die("Could not fork");
  156. if (!$pid) {
  157. open(my $stdin, '|-', qq{chromium-browser --remote-debugging-port=$port --headless}) or die "Could not open chromium-browser to test!";
  158. print "Waiting for cdp server on port $port to come up...\n";
  159. Net::EmptyPort::wait_port( $port, 10 )
  160. or kill_krom_and_die($stdin, "Server never came up after 10s!");
  161. print "done\n";
  162. #XXX not clear that this doesn't want an http uri instead? idk
  163. my $handle = Playwright->new( debug => 1, cdp_uri => "ws://127.0.0.1:$port" );
  164. # Open a new chrome instance
  165. my $browser = $handle->launch( headless => 1, type => 'chrome' );
  166. # Open a tab therein
  167. my $page = $browser->newPage({ videosPath => 'video', acceptDownloads => 1 });
  168. # Load a URL in the tab
  169. my $res = $page->goto('http://troglodyne.net', { waitUntil => 'networkidle' });
  170. print Dumper($res->status(), $browser->version());
  171. $handle->quit();
  172. #XXX OF COURSE chrome responds correctly to ESPIPE and SIGCHLD, why wouldn't it
  173. kill_krom($stdin);
  174. exit 0;
  175. } else {
  176. # If it can't get done in 20s, it ain't getting done
  177. foreach (0..20) {
  178. last unless waitpid( $pid, 1) == 0;
  179. sleep 1;
  180. }
  181. }
  182. print "All Done!\n\n";
  183. }
  184. # Clean up, since we left survivors
  185. require './bin/reap_playwright_servers';
  186. Playwright::ServerReaper::main();
  187. 0;