Debian Codegraph @

README.mkdn

raw ◀ ▶
1 Debian patch touch this file
1
2 [![Travis Build Status](https://travis-ci.org/Corion/WWW-Mechanize-Shell.svg?branch=master)](https://travis-ci.org/Corion/WWW-Mechanize-Shell)
3 [![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/Corion/WWW-Mechanize-Shell?branch=master&svg=true)](https://ci.appveyor.com/project/Corion/WWW-Mechanize-Shell)
4
5 # NAME
6
7 WWW::Mechanize::Shell - An interactive shell for WWW::Mechanize
8
9 # SYNOPSIS
10
11 From the command line as
12
13 perl -MWWW::Mechanize::Shell -eshell
14
15 or alternatively as a custom shell program via :
16
17 #!/usr/bin/perl -w
18 use strict;
19 use WWW::Mechanize::Shell;
20
21 my $shell = WWW::Mechanize::Shell->new("shell");
22
23 if (@ARGV) {
24 $shell->source_file( @ARGV );
25 } else {
26 $shell->cmdloop;
27 };
28
29 # DESCRIPTION
30
31 This module implements a www-like shell above WWW::Mechanize
32 and also has the capability to output crude Perl code that recreates
33 the recorded session. Its main use is as an interactive starting point
34 for automating a session through WWW::Mechanize.
35
36 The cookie support is there, but no cookies are read from your existing
37 browser sessions. See [HTTP::Cookies](https://metacpan.org/pod/HTTP%3A%3ACookies) on how to implement reading/writing
38 your current browsers cookies.
39
40 ## `WWW::Mechanize::Shell->new %ARGS`
41
42 This is the constructor for a new shell instance. Some of the options
43 can be passed to the constructor as parameters.
44
45 By default, a file `.mechanizerc` (respectively `mechanizerc` under Windows)
46 in the users home directory is executed before the interactive shell loop is
47 entered. This can be used to set some defaults. If you want to supply a different
48 filename for the rcfile, the `rcfile` parameter can be passed to the constructor :
49
50 rcfile => '.myapprc',
51
52 - **agent**
53
54 my $shell = WWW::Mechanize::Shell->new(
55 agent => WWW::Mechanize::Chrome->new(),
56 );
57
58 Pass in a premade custom user agent. This object must be compatible to
59 [WWW::Mechanize](https://metacpan.org/pod/WWW%3A%3AMechanize). Use this feature from the command line as
60
61 perl -Ilib -MWWW::Mechanize::Chrome \
62 -MWWW::Mechanize::Shell \
63 -e"shell(agent => WWW::Mechanize::Chrome->new())"
64
65 ## `$shell->release_agent`
66
67 Since the shell stores a reference back to itself within the
68 WWW::Mechanize instance, it is necessary to break this
69 circular reference. This method does this.
70
71 ## `$shell->source_file FILENAME`
72
73 The `source_file` method executes the lines of FILENAME
74 as if they were typed in.
75
76 $shell->source_file( $filename );
77
78 ## `$shell->display_user_warning`
79
80 All user warnings are routed through this routine
81 so they can be rerouted / disabled easily.
82
83 ## `$shell->print_paged LIST`
84
85 Prints the text in LIST using `$ENV{PAGER}`. If `$ENV{PAGER}`
86 is empty, prints directly to `STDOUT`. Most of this routine
87 comes from the `perldoc` utility.
88
89 ## `$shell->link_text LINK`
90
91 Returns a meaningful text from a WWW::Mechanize::Link object. This is (in order of
92 precedence) :
93
94 $link->text
95 $link->name
96 $link->url
97
98 ## `$shell->history`
99
100 Returns the (relevant) shell history, that is, all commands
101 that were not solely for the information of the user. The
102 lines are returned as a list.
103
104 print join "\n", $shell->history;
105
106 ## `$shell->script`
107
108 Returns the shell history as a Perl program. The
109 lines are returned as a list. The lines do not have
110 a one-by-one correspondence to the lines in the history.
111
112 print join "\n", $shell->script;
113
114 ## `$shell->status`
115
116 `status` is called for status updates.
117
118 ## `$shell->display FILENAME LINES`
119
120 `display` is called to output listings, currently from the
121 `history` and `script` commands. If the second parameter
122 is defined, it is the name of the file to be written,
123 otherwise the lines are displayed to the user.
124
125 # COMMANDS
126
127 The shell implements various commands :
128
129 ## exit
130
131 Leaves the shell.
132
133 ## restart
134
135 Restart the shell.
136
•137 This is mostly useful when you are modifying the shell itself. It doesn't
138 work if you use the shell in oneliner mode with `-e`.
139
140 ## get
141
142 Download a specific URL.
143
144 This is used as the entry point in all sessions
145
146 Syntax:
147
148 get URL
149
150 ## save
151
152 Download a link into a file.
153
154 If more than one link matches the RE, all matching links are
155 saved. The filename is taken from the last part of the
156 URL. Alternatively, the number of a link may also be given.
157
158 Syntax:
159
160 save RE
161
162 ## content
163
164 Display the content for the current page.
165
166 Syntax: content \[FILENAME\]
167
168 If the FILENAME argument is provided, save the content to the file.
169
170 A trailing "\\n" is added to the end of the content when using the
171 shell, so this might not be ideally suited to save binary files without
172 manual editing of the produced script.
173
174 ## title
175
176 Display the current page title as found
177 in the `<TITLE>` tag.
178
179 ## headers
180
181 Prints all `<H1>` through `<H5>` strings found in the content,
182 indented accordingly. With an argument, prints only those
183 levels; e.g., `headers 145` prints H1,H4,H5 strings only.
184
185 ## ua
186
187 Get/set the current user agent
188
189 Syntax:
190
191 # fake Internet Explorer
192 ua "Mozilla/4.0 (compatible; MSIE 4.01; Windows 98)"
193
194 # fake QuickTime v5
195 ua "QuickTime (qtver=5.0.2;os=Windows NT 5.0Service Pack 2)"
196
197 # fake Mozilla/Gecko based
198 ua "Mozilla/5.001 (windows; U; NT4.0; en-us) Gecko/25250101"
199
200 # set empty user agent :
201 ua ""
202
203 ## links
204
205 Display all links on a page
206
207 The links numbers displayed can used by `open` to directly
208 select a link to follow.
209
210 ## images
211
212 Display images on a page
213
214 ## parse
215
216 Dump the output of HTML::TokeParser of the current content
217
218 ## forms
219
220 Display all forms on the current page.
221
222 ## form
223
224 Select the form named NAME
225
226 If NAME matches `/^\d+$/`, it is assumed to be the (1-based) index
227 of the form to select. There is no way of selecting a numerically
228 named form by its name.
229
230 ## dump
231
232 Dump the values of the current form
233
234 ## value
235
236 Set a form value
237
238 Syntax:
239
240 value NAME [VALUE]
241
242 ## tick
243
244 Set checkbox marks
245
246 Syntax:
247
248 tick NAME VALUE(s)
249
250 If no value is given, all boxes are checked.
251
252 ## untick
253
254 Remove checkbox marks
255
256 Syntax:
257
258 untick NAME VALUE(s)
259
260 If no value is given, all marks are removed.
261
262 ## submit
263
264 submits the form without clicking on any button
265
266 ## click
267
268 Clicks on the button named NAME.
269
270 No regular expression expansion is done on NAME.
271
272 Syntax:
273
274 click NAME
275
276 If you have a button that has no name (displayed as NONAME),
277 use
278
279 click ""
280
281 to click on it.
282
283 ## open
284
285 &lt;open> accepts one argument, which can be a regular expression or the number
286 of a link on the page, starting at zero. These numbers are displayed by the
287 `links` function. It goes directly to the page if a number is used
288 or if the RE has one match. Otherwise, a list of links matching
289 the regular expression is displayed.
290
291 The regular expression should start and end with "/".
292
293 Syntax:
294
295 open [ RE | # ]
296
297 ## back
298
299 Go back one page in the browser page history.
300
301 ## reload
302
303 Repeat the last request, thus reloading the current page.
304
305 Note that also POST requests are blindly repeated, as this command
306 is mostly intended to be used when testing server side code.
307
308 ## browse
309
310 Open the web browser with the current page
311
312 Displays the current page in the browser.
313
314 ## set
315
316 Set a shell option
317
318 Syntax:
319
320 set OPTION [value]
321
322 The command lists all valid options. Here is a short overview over
323 the different options available :
324
325 autosync - automatically synchronize the browser window
326 autorestart - restart the shell when any required module changes
327 This does not work with C<-e> oneliners.
328 watchfiles - watch all required modules for changes
329 cookiefile - the file where to store all cookies
330 dumprequests - dump all requests to STDOUT
331 dumpresponses - dump the headers of the responses to STDOUT
332 verbose - print commands to STDERR as they are run,
333 when sourcing from a file
334
335 ## history
336
337 Display your current session history as the relevant commands.
338
339 Syntax:
340
341 history [FILENAME]
342
343 Commands that have no influence on the browser state are not added
344 to the history. If a parameter is given to the `history` command,
345 the history is saved to that file instead of displayed onscreen.
346
347 ## script
348
349 Display your current session history as a Perl script using WWW::Mechanize.
350
351 Syntax:
352
353 script [FILENAME]
354
355 If a parameter is given to the `script` command, the script is saved to
356 that file instead of displayed on the console.
357
358 This command was formerly known as `history`.
359
360 ## comment
361
362 Adds a comment to the script and the history. The comment
363 is prepended with a \\n to increase readability.
364
365 ## fillout
366
367 Fill out the current form
368
369 Interactively asks the values hat have no preset
370 value via the autofill command.
371
372 ## auth
373
374 Set basic authentication credentials.
375
376 Syntax:
377
378 auth [authority realm] user password
379
380 If you get back a 401, you can simply supply the matching
381 user and password, as the authority and the realm are taken
382 from the last response:
383
384 >get http://www.example.com
385 Retrieving http://www.example.com(401)
386 http://www.example.com>auth corion secret
387 http://www.example.com>get http://www.example.com
388 Retrieving http://www.example.com(200)
389 http://www.example.com>
390
391 If you know the authority and the realm in advance, you can
392 presupply the credentials, for example at the start of the script :
393
394 >auth www.example.com:80 secure_realm corion secret
395 >get http://www.example.com
396 Retrieving http://www.example.com(200)
397 http://www.example.com>
398
399 ## table
400
401 Display a table described by the columns COLUMNS.
402
403 Syntax:
404
405 table COLUMNS
406
407 Example:
408
409 table Product Price Description
410
411 If there is a table on the current page that has in its first row the three
412 columns `Product`, `Price` and `Description` (not necessarily in that order),
413 the script will display these columns of the whole table.
414
415 The `HTML::TableExtract` module is needed for this feature.
416
417 ## tables
418
419 Display a list of tables.
420
421 Syntax:
422
423 tables
424
425 This command will display the top row for every
426 table on the current page. This is convenient if you want
427 to find out what the exact spellings for each column are.
428
429 The command does not always work nice, for example if a
430 site uses tables for layout, it will be harder to guess
431 what tables are irrelevant and what tables are relevant.
432
433 [HTML::TableExtract](https://metacpan.org/pod/HTML%3A%3ATableExtract) is needed for this feature.
434
435 ## cookies
436
437 Set the cookie file name
438
439 Syntax:
440
441 cookies FILENAME
442
443 ## autofill
444
445 Define an automatic value
446
447 Sets a form value to be filled automatically. The NAME parameter is
448 the WWW::Mechanize::FormFiller::Value subclass you want to use. For
449 session fields, `Keep` is a good candidate, for interactive stuff,
450 `Ask` is a value implemented by the shell.
451
452 A field name starting and ending with a slash (`/`) is taken to be
453 a regular expression and will be applied to all fields with their
454 name matching the expression. A field with a matching name still
455 takes precedence over the regular expression.
456
457 Syntax:
458
459 autofill NAME [PARAMETERS]
460
461 Examples:
462
463 autofill login Fixed corion
464 autofill password Ask
465 autofill selection Random red green orange
466 autofill session Keep
467 autofill "/date$/" Random::Date string "%m/%d/%Y"
468
469 ## eval
470
471 Evaluate Perl code and print the result
472
473 Syntax:
474
475 eval CODE
476
477 For the generated scripts, anything matching the regular expression
478 `/\$self->agent\b/` is automatically
479 replaced by `$agent` in your eval code, to do the Right Thing.
480
481 Examples:
482
483 # Say hello
484 eval "Hello World"
485
486 # And take a look at the current content type
487 eval $self->agent->ct
488
489 ## source
490
491 Execute a batch of commands from a file
492
493 Syntax:
494
495 source FILENAME
496
497 ## versions
498
499 Print the version numbers of important modules
500
501 Syntax:
502
503 versions
504
505 ## timeout
506
507 Set new timeout value for the agent. Effects all subsequent
508 requests. VALUE is in seconds.
509
510 Syntax:
511
512 timeout VALUE
513
514 ## ct
515
516 prints the content type of the most current response.
517
518 Syntax:
519
520 ct
521
522 ## referrer
523
524 set the value of the Referer: header
525
526 Syntax:
527
528 referer URL
529 referrer URL
530
531 ## referer
532
533 Alias for referrer
534
535 ## response
536
537 display the last server response
538
539 ## `$shell->munge_code( CODE )`
540
541 Munges a coderef to become code fit for
542 output independent of WWW::Mechanize::Shell.
543
544 ## `shell`
545
546 This subroutine is exported by default as a convenience method
547 so that the following oneliner invocation works:
548
549 perl -MWWW::Mechanize::Shell -eshell
550
551 You can pass constructor arguments to this
552 routine as well. Any scripts given in `@ARGV`
553 will be run. If `@ARGV` is empty,
554 an interactive loop will be started.
555
556 # SAMPLE SESSIONS
557
558 ## Entering values
559
560 # Search for a term on Google
561 get http://www.google.com
562 value q "Corions Homepage"
563 click btnG
564 script
565 # (yes, this is a bad example of automating, as Google
566 # already has a Perl API. But other sites don't)
567
568 ## Retrieving a table
569
570 get http://www.perlmonks.org
571 open "/Saints in/"
572 table User Experience Level
573 script
574 # now you have a program that gives you a csv file of
575 # that table.
576
577 ## Uploading a file
578
579 get http://aliens:xxxxx/
580 value f path/to/file
581 click "upload"
582
583 ## Batch download
584
585 # download prerelease versions of my modules
586 get http://www.corion.net/perl-dev
587 save /.tar.gz$/
588
589 # REGULAR EXPRESSION SYNTAX
590
591 Some commands take regular expressions as parameters. A regular
592 expression **must** be a single parameter matching `^/.*/([isxm]+)?$`, so
593 you have to use quotes around it if the expression contains spaces :
594
595 /link_foo/ # will match as (?-xims:link_foo)
596 "/link foo/" # will match as (?-xims:link foo)
597
598 Slashes do not need to be escaped, as the shell knows that a RE starts and
599 ends with a slash :
600
601 /link/foo/ # will match as (?-xims:link/foo)
602 "/link/ /foo/" # will match as (?-xims:link/\s/foo)
603
604 The `/i` modifier works as expected.
605 If you desire more power over the regular expressions, consider dropping
606 to Perl or recommend me a good parser module for regular expressions.
607
608 # DISPLAYING HTML
609
610 WWW::Mechanize::Shell now uses the module HTML::Display
611 to display the HTML of the current page in your browser.
612 Have a look at the documentation of HTML::Display how to
613 make it use your browser of choice in the case it does not
614 already guess it correctly.
615
616 # FILLING FORMS VIA CUSTOM CODE
617
618 If you want to stay within the confines of the shell, but still
619 want to fill out forms using custom Perl code, here is a recipe
620 how to achieve this :
621
•622 Code passed to the `eval` command gets evaluated in the WWW::Mechanize::Shell
623 namespace. You can inject new subroutines there and these get picked
624 up by the Callback class of WWW::Mechanize::FormFiller :
625
626 # Fill in the "date" field with the current date/time as string
627 eval sub &::custom_today { scalar localtime };
628 autofill date Callback WWW::Mechanize::Shell::custom_today
629 fillout
630
631 This method can also be used to retrieve data from shell scripts :
632
633 # Fill in the "date" field with the current date/time as string
634 # works only if there is a program "date"
635 eval sub &::custom_today { chomp `date` };
636 autofill date Callback WWW::Mechanize::Shell::custom_today
637 fillout
638
639 As the namespace is different between the shell and the generated
640 script, make sure you always fully qualify your subroutine names,
641 either in your own namespace or in the main namespace.
642
643 # GENERATED SCRIPTS
644
645 The `script` command outputs a skeleton script that reproduces
646 your actions as done in the current session. It pulls in
647 `WWW::Mechanize::FormFiller`, which is possibly not needed. You
648 should add some error and connection checking afterwards.
649
650 # ADDING FIELDS TO HTML
651
652 If you are automating a JavaScript dependent site, you will encounter
653 JavaScript like this :
654
655 <script>
656 document.write( "<input type=submit name=submit>" );
657 </script>
658
659 HTML::Form will not know about this and will not have provided a
660 submit button for you (understandably). If you want to create such
661 a submit button from within your automation script, use the following
662 code :
663
664 $agent->current_form->push_input( submit => { name => "submit", value =>"submit" } );
665
666 This also works for other dynamically generated input fields.
667
668 To fake an input field from within a shell session, use the `eval` command :
669
670 eval $self->agent->current_form->push_input(submit=>{name=>"submit",value=>"submit"});
671
672 And yes, the generated script should do the Right Thing for this eval as well.
673
674 # LOCAL FILES
675
676 If you want to use the shell on a local file without setting up a `http` server
677 to serve the file, you can use the `file:` URI scheme to load it into the "browser":
678
679 get file:local.html
680 forms
681
682 # PROXY SUPPORT
683
684 Currently, the proxy support is realized via a call to
685 the `env_proxy` method of the WWW::Mechanize object, which
686 loads the proxies from the environment. There is no provision made
687 to prevent using proxies (yet). The generated scripts also
688 load their proxies from the environment.
689
690 # ONLINE HELP
691
692 The online help feature is currently a bit broken in `Term::Shell`,
693 but a fix is in the works. Until then, you can re-enable the
694 dynamic online help by patching `Term::Shell` :
695
696 Remove the three lines
697
698 my $smry = exists $o->{handlers}{$h}{smry}
699 ? $o->summary($h)
700 : "undocumented";
701
702 in `sub run_help` and replace them by
703
704 my $smry = $o->summary($h);
705
706 The shell works without this patch and the online help is still
707 available through `perldoc WWW::Mechanize::Shell`
708
709 # BUGS
710
711 Bug reports are very welcome - please use the RT interface at
712 https://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Mechanize-Shell or send a
713 descriptive mail to [email protected] . Please
714 try to include as much (relevant) information as possible - a test script
715 that replicates the undesired behaviour is welcome every time!
716
717 - The two parameter version of the `auth` command guesses the realm from
718 the last received response. Currently a RE is used to extract the realm,
719 but this fails with some servers resp. in some cases. Use the four
720 parameter version of `auth`, or if not possible, code the extraction
721 in Perl, either in the final script or through `eval` commands.
722 - The shell currently detects when you want to follow a JavaScript link and tells you
723 that this is not supported. It would be nicer if there was some callback mechanism
724 to (automatically?) extract URLs from JavaScript-infected links.
725
726 # TODO
727
728 - Add XPath expressions (by moving `WWW::Mechanize` from HTML::Parser to XML::XMLlib
729 or maybe easier, by tacking Class::XPath onto an HTML tree)
730 - Add `head` as a command ?
731 - Optionally silence the HTML::Parser / HTML::Forms warnings about invalid HTML.
732
733 # EXPORT
734
735 The routine `shell` is exported into the importing namespace. This
736 is mainly for convenience so you can use the following commandline
737 invocation of the shell like with CPAN :
738
739 perl -MWWW::Mechanize::Shell -e"shell"
740
741 # REPOSITORY
742
743 The public repository of this module is
744 [https://github.com/Corion/WWW-Mechanize-Shell](https://github.com/Corion/WWW-Mechanize-Shell).
745
746 # SUPPORT
747
748 The public support forum of this module is
749 [http://perlmonks.org/](http://perlmonks.org/).
750
751 # COPYRIGHT AND LICENSE
752
753 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
754
755 Copyright (C) 2002-2026 Max Maischein
756
757 # AUTHOR
758
759 Max Maischein, <[email protected]>
760
761 Please contact me if you find bugs or otherwise improve the module. More tests are also very welcome !
762
763 # SEE ALSO
764
765 [WWW::Mechanize](https://metacpan.org/pod/WWW%3A%3AMechanize),[WWW::Mechanize::FormFiller](https://metacpan.org/pod/WWW%3A%3AMechanize%3A%3AFormFiller),[WWW::Mechanize::Firefox](https://metacpan.org/pod/WWW%3A%3AMechanize%3A%3AFirefox)