example.pl 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. use strict;
  2. use warnings;
  3. use Data::Dumper;
  4. use Playwright;
  5. use Try::Tiny;
  6. my $handle = Playwright->new( debug => 1 );
  7. # Open a new chrome instance
  8. my $browser = $handle->launch( headless => 1, type => 'firefox' );
  9. my $process = $handle->server( browser => $browser, command => 'process' );
  10. print "Browser PID: ".$process->{pid}."\n";
  11. # Open a tab therein
  12. my $page = $browser->newPage({ videosPath => 'video', acceptDownloads => 1 });
  13. # Browser contexts don't exist until you open at least one page.
  14. # You'll need this to grab and set cookies.
  15. my ($context) = @{$browser->contexts()};
  16. # Load a URL in the tab
  17. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  18. print Dumper($res->status(), $browser->version());
  19. # Put your hand in the jar
  20. my $cookies = $context->cookies();
  21. print Dumper($cookies);
  22. # Grab the main frame, in case this is a frameset
  23. my $frameset = $page->mainFrame();
  24. print Dumper($frameset->childFrames());
  25. # Run some JS
  26. my $fun = "
  27. var input = arguments[0];
  28. return {
  29. width: document.documentElement.clientWidth,
  30. height: document.documentElement.clientHeight,
  31. deviceScaleFactor: window.devicePixelRatio,
  32. arg: input
  33. };";
  34. my $result = $page->evaluate($fun, 'zippy');
  35. print Dumper($result);
  36. # Read the console
  37. $page->on('console',"return [...arguments]");
  38. my $promise = $page->waitForEvent('console');
  39. #XXX this *can* race
  40. sleep 1;
  41. $page->evaluate("console.log('hug')");
  42. my $console_log = $handle->await( $promise );
  43. print "Logged to console: '".$console_log->text()."'\n";
  44. # Use a selector to find which input is visible and type into it
  45. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  46. my $inputs = $page->selectMulti('input');
  47. foreach my $input (@$inputs) {
  48. try {
  49. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  50. $input->fill('tickle', { timeout => 250 } );
  51. } catch {
  52. print "Element not visible, skipping...\n";
  53. }
  54. }
  55. # Said better selector
  56. my $actual_input = $page->select('input[name=q]');
  57. $actual_input->fill('whee');
  58. # Ensure we can grab the parent (convenience)
  59. print "Got Parent: ISA ".ref($actual_input->{parent})."\n";
  60. # Take screen of said element
  61. $actual_input->screenshot({ path => 'test.jpg' });
  62. # Fiddle with HIDs
  63. my $mouse = $page->mouse;
  64. $mouse->move( 0, 0 );
  65. my $keyboard = $page->keyboard();
  66. $keyboard->type('F12');
  67. # Start to do some more advanced actions with the page
  68. use FindBin;
  69. use Cwd qw{abs_path};
  70. my $pg = abs_path("$FindBin::Bin/at/test.html");
  71. # Handle dialogs on page start, and dialog after dialog
  72. # NOTE -- the 'load' event won't fire until the dialog is dismissed in some browsers
  73. $promise = $page->waitForEvent('dialog');
  74. $page->goto("file://$pg", { waitUntil => 'networkidle' });
  75. my $dlg = $handle->await($promise);
  76. $promise = $page->waitForEvent('dialog');
  77. $dlg->dismiss();
  78. $dlg = $handle->await($promise);
  79. $dlg->accept();
  80. # Download stuff -- note this requries acceptDownloads = true in the page open
  81. # NOTE -- the 'download' event fires unreliably, as not all browsers properly obey the 'download' property in hrefs.
  82. # Chrome, for example would choke here on an intermediate dialog.
  83. $promise = $page->waitForEvent('download');
  84. $page->select('#d-lo')->click();
  85. my $download = $handle->await( $promise );
  86. print "Download suggested filename\n";
  87. print $download->suggestedFilename()."\n";
  88. $download->saveAs('test2.jpg');
  89. # Fiddle with file inputs
  90. my $choochoo = $page->waitForEvent('filechooser');
  91. $page->select('#drphil')->click();
  92. my $chooseu = $handle->await( $choochoo );
  93. $chooseu->setFiles('test.jpg');
  94. # Make sure we can do child selectors
  95. my $parent = $page->select('body');
  96. my $child = $parent->select('#drphil');
  97. print ref($child)."\n";
  98. # Save a video now that we are done
  99. my $bideo = $page->video;
  100. # IT IS IMPORTANT TO CLOSE THE PAGE FIRST OR THIS WILL HANG!
  101. $page->close();
  102. my $vidpath = $bideo->saveAs('video/example.webm');