example.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 => 0, type => 'firefox' );
  9. # Open a tab therein
  10. my $page = $browser->newPage({ videosPath => 'video', acceptDownloads => 1 });
  11. my $bideo = $page->video;
  12. my $vidpath = $bideo->path;
  13. print "Video Path: $vidpath\n";
  14. # Browser contexts don't exist until you open at least one page.
  15. # You'll need this to grab and set cookies.
  16. my ($context) = @{$browser->contexts()};
  17. # Load a URL in the tab
  18. my $res = $page->goto('http://google.com', { waitUntil => 'networkidle' });
  19. print Dumper($res->status(), $browser->version());
  20. # Put your hand in the jar
  21. my $cookies = $context->cookies();
  22. print Dumper($cookies);
  23. # Grab the main frame, in case this is a frameset
  24. my $frameset = $page->mainFrame();
  25. print Dumper($frameset->childFrames());
  26. # Run some JS
  27. my $fun = "
  28. var input = arguments[0];
  29. return {
  30. width: document.documentElement.clientWidth,
  31. height: document.documentElement.clientHeight,
  32. deviceScaleFactor: window.devicePixelRatio,
  33. arg: input
  34. };";
  35. my $result = $page->evaluate($fun, 'zippy');
  36. print Dumper($result);
  37. # Read the console
  38. $page->on('console',"return [...arguments]");
  39. my $promise = $page->waitForEvent('console');
  40. $page->evaluate("console.log('hug')");
  41. my $console_log = $handle->await( $promise );
  42. print "Logged to console: '".$console_log->text()."'\n";
  43. # Use a selector to find which input is visible and type into it
  44. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  45. my $inputs = $page->selectMulti('input');
  46. foreach my $input (@$inputs) {
  47. try {
  48. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  49. $input->fill('tickle', { timeout => 250 } );
  50. } catch {
  51. print "Element not visible, skipping...\n";
  52. }
  53. }
  54. # Said better selector
  55. my $actual_input = $page->select('input[name=q]');
  56. $actual_input->fill('whee');
  57. # Take screen of said element
  58. $actual_input->screenshot({ path => 'test.jpg' });
  59. # Fiddle with HIDs
  60. my $mouse = $page->mouse;
  61. $mouse->move( 0, 0 );
  62. my $keyboard = $page->keyboard();
  63. $keyboard->type('F12');
  64. # Start to do some more advanced actions with the page
  65. use FindBin;
  66. use Cwd qw{abs_path};
  67. my $pg = abs_path("$FindBin::Bin/at/test.html");
  68. # Handle dialogs on page start, and dialog after dialog
  69. # NOTE -- the 'load' event won't fire until the dialog is dismissed in some browsers
  70. $promise = $page->waitForEvent('dialog');
  71. $page->goto("file://$pg", { waitUntil => 'networkidle' });
  72. my $dlg = $handle->await($promise);
  73. $promise = $page->waitForEvent('dialog');
  74. $dlg->dismiss();
  75. $dlg = $handle->await($promise);
  76. $dlg->accept();
  77. # Download stuff -- note this requries acceptDownloads = true in the page open
  78. # NOTE -- the 'download' event fires unreliably, as not all browsers properly obey the 'download' property in hrefs.
  79. # Chrome, for example would choke here on an intermediate dialog.
  80. $promise = $page->waitForEvent('download');
  81. $page->select('#d-lo')->click();
  82. my $download = $handle->await( $promise );
  83. $download->saveAs('test2.jpg');
  84. # Fiddle with file inputs
  85. my $choochoo = $page->waitForEvent('filechooser');
  86. $page->select('#drphil')->click();
  87. my $chooseu = $handle->await( $choochoo );
  88. $chooseu->setFiles('test.jpg');