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