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.50 by root, Thu Mar 26 21:28:18 2020 UTC vs.
Revision 1.51 by root, Fri Mar 27 17:05:50 2020 UTC

77my $json = eval { require JSON::XS; JSON::XS:: } || do { require JSON::PP; JSON::PP:: }; 77my $json = eval { require JSON::XS; JSON::XS:: } || do { require JSON::PP; JSON::PP:: };
78$json = $json->new->utf8; 78$json = $json->new->utf8;
79 79
80$json->boolean_values (0, 1) 80$json->boolean_values (0, 1)
81 if $json->can ("boolean_values"); 81 if $json->can ("boolean_values");
82
83sub _decode_base64 {
84 require MIME::Base64;
85
86 MIME::Base64::decode_base64 (shift)
87}
82 88
83sub req_ { 89sub req_ {
84 my ($self, $method, $ep, $body, $cb) = @_; 90 my ($self, $method, $ep, $body, $cb) = @_;
85 91
86 AnyEvent::HTTP::http_request $method => "$self->{_ep}$ep", 92 AnyEvent::HTTP::http_request $method => "$self->{_ep}$ep",
1076 1082
1077=cut 1083=cut
1078 1084
1079=item $wd->take_screenshot 1085=item $wd->take_screenshot
1080 1086
1081Create a screenshot, returning it as a PNG image encoded as base64. To 1087Create a screenshot, returning it as a PNG image. To decode and save, you
1082decode and save, you coulöd do something like: 1088coulöd do something like:
1083 1089
1084 use MIME::Base64 (); 1090 use MIME::Base64 ();
1085 1091
1086 my $screenshot = $wd->take_screenshot; 1092 my $screenshot = $wd->take_screenshot;
1087 1093
1093 1099
1094Similar to C<take_screenshot>, but only takes a screenshot of the bounding 1100Similar to C<take_screenshot>, but only takes a screenshot of the bounding
1095box of a single element. 1101box of a single element.
1096 1102
1097Note: The W3C WebDriver specification also mentions a "scroll" parameter, 1103Note: The W3C WebDriver specification also mentions a "scroll" parameter,
1098but there is no way to specify it according to the spec, so it is 1104but it is impossible to pass parameters for this command, so it is
1099currently left out. 1105czrrently left out.
1100 1106
1101=cut 1107=cut
1102 1108
1103sub take_screenshot_ { 1109sub take_screenshot_ {
1110 my $cb = pop; push @_, sub { $cb->($_[0], _decode_base64 $_[1]) };
1104 $_[0]->get_ (screenshot => $_[1]); 1111 $_[0]->get_ (screenshot => $_[1]);
1105} 1112}
1106 1113
1107sub take_element_screenshot_ { 1114sub take_element_screenshot_ {
1115 my $cb = pop; push @_, sub { $cb->($_[0], _decode_base64 $_[1]) };
1108 $_[0]->get_ ("element/$_[1]{$WEB_ELEMENT_IDENTIFIER}/screenshot" => $_[2]); 1116 $_[0]->get_ ("element/$_[1]{$WEB_ELEMENT_IDENTIFIER}/screenshot" => $_[2]);
1109} 1117}
1110 1118
1111=back 1119=back
1120
1121=head3 PRINT
1122
1123=over
1124
1125=cut
1126
1127=item $wd->print_page (key => value...)
1128
1129Create a printed version of the document, returning it as a PDF document
1130encoded as base64. See C<take_screenshot> for an example on how to decode
1131and save such a string.
1132
1133This command takes a lot of optional parameters, see L<the print
1134section|https://www.w3.org/TR/webdriver2/#print> of the WebDriver
1135specification for details.
1136
1137This command is taken from a draft document, so it might change in the
1138future.
1139
1140=cut
1141
1142sub print_page {
1143 my $cb = pop; push @_, sub { $cb->($_[0], _decode_base64 $_[1]) };
1144 $_[0]->post_ (print => { @_ });
1145}
1112 1146
1113=head2 ACTION LISTS 1147=head2 ACTION LISTS
1114 1148
1115Action lists can be quite complicated. Or at least it took a while for 1149Action lists can be quite complicated. Or at least it took a while for
1116me to twist my head around them. Basically, an action list consists of a 1150me to twist my head around them. Basically, an action list consists of a
1117number of sources representing devices (such as a finger, a mouse, a pen 1151number of sources representing devices (such as a finger, a mouse, a pen
1118or a keyboard) and a list of actions for each source. 1152or a keyboard) and a list of actions for each source, in a timeline.
1119 1153
1120An action can be a key press, a pointer move or a pause (time delay). 1154An action can be a key press, a pointer move or a pause (time delay).
1121 1155
1122While you can provide these action lists manually, it is (hopefully) less 1156While you can provide these action lists manually, it is (hopefully) less
1123cumbersome to use the API described in this section to create them. 1157cumbersome to use the API described in this section to create them.
1128 1162
1129Most methods here are designed to chain, i.e. they return the web actions 1163Most methods here are designed to chain, i.e. they return the web actions
1130object, to simplify multiple calls. 1164object, to simplify multiple calls.
1131 1165
1132Also, while actions from different sources can happen "at the same time" 1166Also, while actions from different sources can happen "at the same time"
1133in the WebDriver protocol, this class ensures that actions will execute in 1167in the WebDriver protocol, this class by default ensures that actions will
1134the order specified. 1168execute in the order specified.
1135 1169
1136For example, to simulate a mouse click to an input element, followed by 1170For example, to simulate a mouse click to an input element, followed by
1137entering some text and pressing enter, you can use this: 1171entering some text and pressing enter, you can use this:
1138 1172
1139 $wd->actions 1173 $wd->actions
1140 ->click (0, 100) 1174 ->click (0, 100)
1141 ->type ("some text") 1175 ->type ("some text")
1142 ->key ("{Enter}") 1176 ->key ("{Enter}")
1143 ->perform; 1177 ->perform;
1144 1178
1145By default, keyboard and mouse input sources are provided. You can create 1179By default, C<keyboard> and C<mouse> input sources are provided and
1146your own sources and use them when adding events. The above example could 1180used. You can create your own sources and use them when adding events. The
1147be more verbosely written like this: 1181above example could be more verbosely written like this:
1148 1182
1149 $wd->actions 1183 $wd->actions
1150 ->source ("mouse", "pointer", pointerType => "mouse") 1184 ->source ("mouse", "pointer", pointerType => "mouse")
1151 ->source ("kbd", "key") 1185 ->source ("kbd", "key")
1152 ->click (0, 100, "mouse") 1186 ->click (0, 100, "mouse")
1155 ->perform; 1189 ->perform;
1156 1190
1157When you specify the event source explicitly it will switch the current 1191When you specify the event source explicitly it will switch the current
1158"focus" for this class of device (all keyboards are in one class, all 1192"focus" for this class of device (all keyboards are in one class, all
1159pointer-like devices such as mice/fingers/pens are in one class), so you 1193pointer-like devices such as mice/fingers/pens are in one class), so you
1160don't have to specify the source for subsequent actions. 1194don't have to specify the source for subsequent actions that are on the
1195same class.
1161 1196
1162When you use the sources C<keyboard>, C<mouse>, C<touch1>..C<touch3>, 1197When you use the sources C<keyboard>, C<mouse>, C<touch1>..C<touch3>,
1163C<pen> without defining them, then a suitable default source will be 1198C<pen> without defining them, then a suitable default source will be
1164created for them. 1199created for them.
1165 1200

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines