… | |
… | |
45 | recommendation, which can be found here |
45 | recommendation, which can be found here |
46 | <https://www.w3.org/TR/webdriver1/>: |
46 | <https://www.w3.org/TR/webdriver1/>: |
47 | |
47 | |
48 | https://www.w3.org/TR/webdriver1/ |
48 | https://www.w3.org/TR/webdriver1/ |
49 | |
49 | |
50 | Mozilla's "geckodriver" has had webdriver for a long time, while |
50 | Mozilla's "geckodriver" has had webdriver support for a long time, while |
51 | "chromedriver" only has basic and mostly undocumented webdriver support |
51 | "chromedriver" only has basic and mostly undocumented webdriver support |
52 | as of release 77. |
52 | as of release 77. |
53 | |
53 | |
|
|
54 | In Debian GNU/Linux, you can install the chromedriver for chromium via |
|
|
55 | the "chromium-driver" package. Unfortunately, there is no (working) |
|
|
56 | package for geckodriver, but you can download it from github |
|
|
57 | <https://github.com/mozilla/geckodriver/releases>. |
|
|
58 | |
54 | CONVENTIONS |
59 | CONVENTIONS |
55 | Unless otherwise stated, all delays and time differences in this module |
60 | Unless otherwise stated, all delays and time differences in this module |
56 | are represented as an integer number of milliseconds. |
61 | are represented as an integer number of milliseconds, which is perhaps |
|
|
62 | surprising to users of my other modules but is what the WebDriver spec |
|
|
63 | uses. |
57 | |
64 | |
58 | WEBDRIVER OBJECTS |
65 | WEBDRIVER OBJECTS |
59 | new AnyEvent::WebDriver key => value... |
66 | new AnyEvent::WebDriver key => value... |
60 | Create a new WebDriver object. Example for a remote WebDriver |
67 | Create a new WebDriver object. Example for a remote WebDriver |
61 | connection (the only type supported at the moment): |
68 | connection (the only type supported at the moment): |
… | |
… | |
70 | |
77 | |
71 | proxy => $proxyspec |
78 | proxy => $proxyspec |
72 | The proxy to use (same as the "proxy" argument used by |
79 | The proxy to use (same as the "proxy" argument used by |
73 | AnyEvent::HTTP). The default is "undef", which disables proxies. |
80 | AnyEvent::HTTP). The default is "undef", which disables proxies. |
74 | To use the system-provided proxy (e.g. "http_proxy" environment |
81 | To use the system-provided proxy (e.g. "http_proxy" environment |
75 | variable), specify a value of "default". |
82 | variable), specify the string "default". |
76 | |
83 | |
77 | autodelete => $boolean |
84 | autodelete => $boolean |
78 | If true (the default), then automatically execute |
85 | If true (the default), then automatically execute |
79 | "delete_session" when the WebDriver object is destroyed with an |
86 | "delete_session" when the WebDriver object is destroyed with an |
80 | active session. If set to a false value, then the session will |
87 | active session. If set to a false value, then the session will |
… | |
… | |
498 | $text = $wd->send_alert_text |
505 | $text = $wd->send_alert_text |
499 | Fills in the user prompt with the given text. |
506 | Fills in the user prompt with the given text. |
500 | |
507 | |
501 | SCREEN CAPTURE |
508 | SCREEN CAPTURE |
502 | $wd->take_screenshot |
509 | $wd->take_screenshot |
503 | Create a screenshot, returning it as a PNG image in a "data:" URL. |
510 | Create a screenshot, returning it as a PNG image. To decode and |
|
|
511 | save, you could do something like: |
|
|
512 | |
|
|
513 | use MIME::Base64 (); |
|
|
514 | |
|
|
515 | my $screenshot = $wd->take_screenshot; |
|
|
516 | |
|
|
517 | open my $fh, ">", "screenshot.png" or die "screenshot.png: $!\n"; |
|
|
518 | |
|
|
519 | syswrite $fh, MIME::Base64::decode_base64 $screenshot; |
504 | |
520 | |
505 | $wd->take_element_screenshot ($element) |
521 | $wd->take_element_screenshot ($element) |
506 | Similar to "take_screenshot", but only takes a screenshot of the |
522 | Similar to "take_screenshot", but only takes a screenshot of the |
507 | bounding box of a single element. |
523 | bounding box of a single element. |
|
|
524 | |
|
|
525 | Compatibility note: As of chrome version 80, I found that the |
|
|
526 | screenshot scaling is often wrong (the screenshot is much smaller |
|
|
527 | than the element normally displays) unless chrome runs in headless |
|
|
528 | mode. The spec does allow for any form of scaling, so this is not |
|
|
529 | strictly a bug in chrome, but of course it diminishes trhe |
|
|
530 | screenshot functionality. |
|
|
531 | |
|
|
532 | PRINT |
|
|
533 | $wd->print_page (key => value...) |
|
|
534 | Create a printed version of the document, returning it as a PDF |
|
|
535 | document encoded as base64. See "take_screenshot" for an example on |
|
|
536 | how to decode and save such a string. |
|
|
537 | |
|
|
538 | This command takes a lot of optional parameters, see the print |
|
|
539 | section <https://www.w3.org/TR/webdriver2/#print> of the WebDriver |
|
|
540 | specification for details. |
|
|
541 | |
|
|
542 | This command is taken from a draft document, so it might change in |
|
|
543 | the future. |
508 | |
544 | |
509 | ACTION LISTS |
545 | ACTION LISTS |
510 | Action lists can be quite complicated. Or at least it took a while for |
546 | Action lists can be quite complicated. Or at least it took a while for |
511 | me to twist my head around them. Basically, an action list consists of a |
547 | me to twist my head around them. Basically, an action list consists of a |
512 | number of sources representing devices (such as a finger, a mouse, a pen |
548 | number of sources representing devices (such as a finger, a mouse, a pen |
513 | or a keyboard) and a list of actions for each source. |
549 | or a keyboard) and a list of actions for each source, in a timeline. |
514 | |
550 | |
515 | An action can be a key press, a pointer move or a pause (time delay). |
551 | An action can be a key press, a pointer move or a pause (time delay). |
516 | |
552 | |
517 | While you can provide these action lists manually, it is (hopefully) |
553 | While you can provide these action lists manually, it is (hopefully) |
518 | less cumbersome to use the API described in this section to create them. |
554 | less cumbersome to use the API described in this section to create them. |
… | |
… | |
523 | |
559 | |
524 | Most methods here are designed to chain, i.e. they return the web |
560 | Most methods here are designed to chain, i.e. they return the web |
525 | actions object, to simplify multiple calls. |
561 | actions object, to simplify multiple calls. |
526 | |
562 | |
527 | Also, while actions from different sources can happen "at the same time" |
563 | Also, while actions from different sources can happen "at the same time" |
528 | in the WebDriver protocol, this class ensures that actions will execute |
564 | in the WebDriver protocol, this class by default ensures that actions |
529 | in the order specified. |
565 | will execute in the order specified. |
530 | |
566 | |
531 | For example, to simulate a mouse click to an input element, followed by |
567 | For example, to simulate a mouse click to an input element, followed by |
532 | entering some text and pressing enter, you can use this: |
568 | entering some text and pressing enter, you can use this: |
533 | |
569 | |
534 | $wd->actions |
570 | $wd->actions |
535 | ->click (0, 100) |
571 | ->click (0, 100) |
536 | ->type ("some text") |
572 | ->type ("some text") |
537 | ->key ("{Enter}") |
573 | ->key ("{Enter}") |
538 | ->perform; |
574 | ->perform; |
539 | |
575 | |
540 | By default, keyboard and mouse input sources are provided. You can |
576 | By default, "keyboard" and "mouse" input sources are provided and used. |
541 | create your own sources and use them when adding events. The above |
577 | You can create your own sources and use them when adding events. The |
542 | example could be more verbosely written like this: |
578 | above example could be more verbosely written like this: |
543 | |
579 | |
544 | $wd->actions |
580 | $wd->actions |
545 | ->source ("mouse", "pointer", pointerType => "mouse") |
581 | ->source ("mouse", "pointer", pointerType => "mouse") |
546 | ->source ("kbd", "key") |
582 | ->source ("kbd", "key") |
547 | ->click (0, 100, "mouse") |
583 | ->click (0, 100, "mouse") |
… | |
… | |
550 | ->perform; |
586 | ->perform; |
551 | |
587 | |
552 | When you specify the event source explicitly it will switch the current |
588 | When you specify the event source explicitly it will switch the current |
553 | "focus" for this class of device (all keyboards are in one class, all |
589 | "focus" for this class of device (all keyboards are in one class, all |
554 | pointer-like devices such as mice/fingers/pens are in one class), so you |
590 | pointer-like devices such as mice/fingers/pens are in one class), so you |
555 | don't have to specify the source for subsequent actions. |
591 | don't have to specify the source for subsequent actions that are on the |
|
|
592 | same class. |
556 | |
593 | |
557 | When you use the sources "keyboard", "mouse", "touch1".."touch3", "pen" |
594 | When you use the sources "keyboard", "mouse", "touch1".."touch3", "pen" |
558 | without defining them, then a suitable default source will be created |
595 | without defining them, then a suitable default source will be created |
559 | for them. |
596 | for them. |
560 | |
597 | |
… | |
… | |
599 | $al = $al->doubleclick ($button, $source) |
636 | $al = $al->doubleclick ($button, $source) |
600 | Convenience function that creates two button press and release |
637 | Convenience function that creates two button press and release |
601 | action pairs in a row, with no unnecessary delay between them. |
638 | action pairs in a row, with no unnecessary delay between them. |
602 | $button defaults to 0. |
639 | $button defaults to 0. |
603 | |
640 | |
604 | $al = $al->move ($button, $origin, $x, $y, $duration, $source) |
641 | $al = $al->move ($origin, $x, $y, $duration, $source) |
605 | Moves a pointer to the given position, relative to origin (either |
642 | Moves a pointer to the given position, relative to origin (either |
606 | "viewport", "pointer" or an element object. |
643 | "viewport", "pointer" or an element object. The coordinates will be |
|
|
644 | truncated to integer values. |
607 | |
645 | |
608 | $al = $al->cancel ($source) |
646 | $al = $al->cancel ($source) |
609 | Executes a pointer cancel action. |
647 | Executes a pointer cancel action. |
610 | |
648 | |
611 | $al = $al->keyDown ($key, $source) |
649 | $al = $al->key_down ($key, $source) |
612 | $al = $al->keyUp ($key, $source) |
650 | $al = $al->key_up ($key, $source) |
613 | Press or release the given key. |
651 | Press or release the given key. |
614 | |
652 | |
615 | $al = $al->key ($key, $source) |
653 | $al = $al->key ($key, $source) |
616 | Peess and release the given key, without unnecessary delay. |
654 | Peess and release the given key in one go, without unnecessary |
|
|
655 | delay. |
617 | |
656 | |
618 | A special syntax, "{keyname}" can be used for special keys - all the |
657 | A special syntax, "{keyname}" can be used for special keys - all the |
619 | special key names from section 17.4.2 |
658 | special key names from the second table in section 17.4.2 |
620 | <https://www.w3.org/TR/webdriver1/#keyboard-actions> of the |
659 | <https://www.w3.org/TR/webdriver1/#keyboard-actions> of the |
621 | WebDriver recommendation can be used. |
660 | WebDriver recommendation can be used - prefix with "Shift-Space". to |
|
|
661 | get the shifted version, as in "Shift-" |
622 | |
662 | |
623 | Example: press and release "a". |
663 | Example: press and release "a". |
624 | |
664 | |
625 | $al->key ("a"); |
665 | $al->key ("a"); |
626 | |
666 | |