ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-WebDriver/WebDriver.pm
(Generate patch)

Comparing AnyEvent-WebDriver/WebDriver.pm (file contents):
Revision 1.13 by root, Wed Aug 29 02:47:02 2018 UTC vs.
Revision 1.14 by root, Wed Aug 29 04:54:21 2018 UTC

23 $wd->element_click ($wd->find_element ("css selector" => 'input[type="submit"]')); 23 $wd->element_click ($wd->find_element ("css selector" => 'input[type="submit"]'));
24 24
25 sleep 10; 25 sleep 10;
26 26
27=head1 DESCRIPTION 27=head1 DESCRIPTION
28
29WARNING: THE API IS NOT GUARANTEED TO BE STABLE UNTIL VERSION 1.0.
28 30
29This module aims to implement the W3C WebDriver specification which is the 31This module aims to implement the W3C WebDriver specification which is the
30standardised equivalent to the Selenium WebDriver API., which in turn aims 32standardised equivalent to the Selenium WebDriver API., which in turn aims
31at remotely controlling web browsers such as Firefox or Chromium. 33at remotely controlling web browsers such as Firefox or Chromium.
32 34
50package AnyEvent::WebDriver; 52package AnyEvent::WebDriver;
51 53
52use common::sense; 54use common::sense;
53 55
54use Carp (); 56use Carp ();
55use JSON::XS ();
56use AnyEvent (); 57use AnyEvent ();
57use AnyEvent::HTTP (); 58use AnyEvent::HTTP ();
58 59
59our $VERSION = 0.2; 60our $VERSION = 0.2;
60 61
61our $WEB_ELEMENT_IDENTIFIER = "element-6066-11e4-a52e-4f735466cecf"; 62our $WEB_ELEMENT_IDENTIFIER = "element-6066-11e4-a52e-4f735466cecf";
62 63
63my $json = JSON::XS->new 64my $json = eval { require JSON::XS; JSON::XS:: } || do { require JSON::PP; JSON::PP:: };
64 ->utf8; 65$json = $json->new->utf8;
65 66
66$json->boolean_values (0, 1) 67$json->boolean_values (0, 1)
67 if $json->can ("boolean_values"); 68 if $json->can ("boolean_values");
68 69
69sub req_ { 70sub req_ {
112 (my $name = $AUTOLOAD) =~ s/^.*://; 113 (my $name = $AUTOLOAD) =~ s/^.*://;
113 114
114 my $name_ = "$name\_"; 115 my $name_ = "$name\_";
115 116
116 defined &$name_ 117 defined &$name_
117 or Carp::croak "AUTOLOAD: no such method"; 118 or Carp::croak "$AUTOLOAD: no such method";
118 119
119 my $func_ = \&$name_; 120 my $func_ = \&$name_;
120 121
121 *$name = sub { 122 *$name = sub {
122 $func_->(@_, my $cv = AE::cv); 123 $func_->(@_, my $cv = AE::cv);
212 213
213sub actions { 214sub actions {
214 AnyEvent::WebDriver::Actions->new (wd => $_[0]) 215 AnyEvent::WebDriver::Actions->new (wd => $_[0])
215} 216}
216 217
218=item $sessionstring = $wd->save_session
219
220Save the current session in a string so it can be restored load with
221C<load_session>. Note that only the session data itself is stored
222(currently the session id and capabilities), not the endpoint information
223itself.
224
225The main use of this function is in conjunction with disabled
226C<autodelete>, to save a session to e.g., and restore it later. It could
227presumably used for other applications, suhc as using the same sssion from
228multiple processes and so on.
229
230=item $wd->load_session ($sessionstring)
231
232=item $wd->set_session ($sessionid, $capabilities)
233
234Starts using the given session, as identified by
235C<$sessionid>. C<$capabilities> should be the original session
236capabilities, although the current version of this module does not make
237any use of it.
238
239The C<$sessionid> is stored in C<< $wd->{sid} >> (and could be fetched
240form there for later use), while the capabilities are stored in C<<
241$wd->{capabilities} >>.
242
243=cut
244
245sub save_session {
246 my ($self) = @_;
247
248 $json->encode ([1, $self->{sid}, $self->{capabilities}]);
249}
250
251sub load_session {
252 my ($self, $session) = @_;
253
254 $session = $json->decode ($session);
255
256 $session->[0] == 1
257 or Carp::croak "AnyEvent::WebDriver::load_session: session corrupted or from different version";
258
259 $self->set_session ($session->[1], $session->[2]);
260}
261
262sub set_session {
263 my ($self, $sid, $caps) = @_;
264
265 $self->{sid} = $sid;
266 $self->{capabilities} = $caps;
267
268 $self->{_ep} = "$self->{endpoint}/session/$self->{sid}/";
269}
270
217=back 271=back
218 272
219=head2 SIMPLIFIED API 273=head2 SIMPLIFIED API
220 274
221This section documents the simplified API, which is really just a very 275This section documents the simplified API, which is really just a very
248successfully, and only one session can be created per WebDriver object. 302successfully, and only one session can be created per WebDriver object.
249 303
250On success, C<< $wd->{sid} >> is set to the session ID, and C<< 304On success, C<< $wd->{sid} >> is set to the session ID, and C<<
251$wd->{capabilities} >> is set to the returned capabilities. 305$wd->{capabilities} >> is set to the returned capabilities.
252 306
307Simple example of creatring a WebDriver object and a new session:
308
253 my $wd = new AnyEvent::Selenium endpoint => "http://localhost:4545"; 309 my $wd = new AnyEvent::Selenium endpoint => "http://localhost:4545";
310 $wd->new_session ({});
311
312Real-world example with capability negotiation:
254 313
255 $wd->new_session ({ 314 $wd->new_session ({
256 capabilities => { 315 capabilities => {
316 alwaysMatch => {
257 pageLoadStrategy => "normal", 317 pageLoadStrategy => "eager",
318 unhandledPromptBehavior => "dismiss",
319 },
320 firstMatch => [
321 {
322 browserName => "firefox",
323 "moz:firefoxOptions" => {
324 binary => "firefox/firefox",
325 args => ["-devtools"],
326 prefs => {
327 "dom.webnotifications.enabled" => \0,
328 },
329 },
330 },
331 {
332 # generic fallback
333 },
334 ],
335
258 }. 336 },
259 }); 337 });
338
339Firefox-specific capability documentation can be found L<on
340MDN|https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities>,
341Chrome-specific capability documentation might be found
342L<here|http://chromedriver.chromium.org/capabilities>, but the latest
343release at the time of this writing has effectively no WebDriver support
344at all, and canary releases are not freely downloadable.
345
346If you have URLs for Safari/IE/Edge etc. capabilities, feel free to tell
347me about them.
260 348
261=cut 349=cut
262 350
263sub new_session_ { 351sub new_session_ {
264 my ($self, $kv, $cb) = @_; 352 my ($self, $kv, $cb) = @_;
265 353
266 local $self->{_ep} = "$self->{endpoint}/"; 354 local $self->{_ep} = "$self->{endpoint}/";
267 $self->post_ (session => $kv, sub { 355 $self->post_ (session => $kv, sub {
268 my ($status, $res) = @_; 356 my ($status, $res) = @_;
269 357
358 exists $res->{capabilities}
359 or $status = "500"; # blasted chromedriver
360
361 $self->set_session ($res->{sessionId}, $res->{capabilities});
270 if ($status eq "200") { 362 if $status eq "200";
271 $self->{sid} = $res->{sessionId};
272 $self->{capabilities} = $res->{capabilities};
273
274 $self->{_ep} = "$self->{endpoint}/session/$self->{sid}/";
275 }
276 363
277 $cb->($status, $res); 364 $cb->($status, $res);
278 }); 365 });
279} 366}
280 367

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines