example.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. # 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. #XXX this *can* race
  41. sleep 1;
  42. $page->evaluate("console.log('hug')");
  43. my $console_log = $handle->await( $promise );
  44. print "Logged to console: '".$console_log->text()."'\n";
  45. # Use a selector to find which input is visible and type into it
  46. # Ideally you'd use a better selector to solve this problem, but this is just showing off
  47. my $inputs = $page->selectMulti('input');
  48. foreach my $input (@$inputs) {
  49. try {
  50. # Pretty much a brute-force approach here, again use a better pseudo-selector instead like :visible
  51. $input->fill('tickle', { timeout => 250 } );
  52. } catch {
  53. print "Element not visible, skipping...\n";
  54. }
  55. }
  56. # Said better selector
  57. my $actual_input = $page->select('input[name=q]');
  58. $actual_input->fill('whee');
  59. # Ensure we can grab the parent (convenience)
  60. print "Got Parent: ISA ".ref($actual_input->{parent})."\n";
  61. # Take screen of said element
  62. $actual_input->screenshot({ path => 'test.jpg' });
  63. # Fiddle with HIDs
  64. my $mouse = $page->mouse;
  65. $mouse->move( 0, 0 );
  66. my $keyboard = $page->keyboard();
  67. $keyboard->type('F12');
  68. # Start to do some more advanced actions with the page
  69. use FindBin;
  70. use Cwd qw{abs_path};
  71. my $pg = abs_path("$FindBin::Bin/at/test.html");
  72. # Handle dialogs on page start, and dialog after dialog
  73. # NOTE -- the 'load' event won't fire until the dialog is dismissed in some browsers
  74. $promise = $page->waitForEvent('dialog');
  75. $page->goto("file://$pg", { waitUntil => 'networkidle' });
  76. my $dlg = $handle->await($promise);
  77. $promise = $page->waitForEvent('dialog');
  78. $dlg->dismiss();
  79. $dlg = $handle->await($promise);
  80. $dlg->accept();
  81. # Download stuff -- note this requries acceptDownloads = true in the page open
  82. # NOTE -- the 'download' event fires unreliably, as not all browsers properly obey the 'download' property in hrefs.
  83. # Chrome, for example would choke here on an intermediate dialog.
  84. $promise = $page->waitForEvent('download');
  85. $page->select('#d-lo')->click();
  86. my $download = $handle->await( $promise );
  87. $download->saveAs('test2.jpg');
  88. # Fiddle with file inputs
  89. my $choochoo = $page->waitForEvent('filechooser');
  90. $page->select('#drphil')->click();
  91. my $chooseu = $handle->await( $choochoo );
  92. $chooseu->setFiles('test.jpg');
  93. # Make sure we can do child selectors
  94. my $parent = $page->select('body');
  95. my $child = $parent->select('#drphil');
  96. print ref($child)."\n";