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.35 by root, Mon Sep 3 20:57:43 2018 UTC vs.
Revision 1.36 by root, Tue Sep 4 01:20:49 2018 UTC

27 27
28 # this is an example of an action sequence 28 # this is an example of an action sequence
29 $wd->actions 29 $wd->actions
30 ->move ($wd->find_element (...), 40, 5) 30 ->move ($wd->find_element (...), 40, 5)
31 ->click 31 ->click
32 ->pause # to separate the click from the keypress
33 ->type ("some text") 32 ->type ("some text")
34 ->key ("{Enter}") 33 ->key ("{Enter}")
35 ->perform; 34 ->perform;
36 35
37=head1 DESCRIPTION 36=head1 DESCRIPTION
933 id => "myfatfinger", 932 id => "myfatfinger",
934 type => "pointer", 933 type => "pointer",
935 pointerType => "touch", 934 pointerType => "touch",
936 actions => [ 935 actions => [
937 { type => "pointerMove", duration => 100, origin => $input, x => 40, y => 5 }, 936 { type => "pointerMove", duration => 100, origin => $input, x => 40, y => 5 },
938 { type => "pointerDown", button => 1 }, 937 { type => "pointerDown", button => 0 },
939 { type => "pause", duration => 40 }, 938 { type => "pause", duration => 40 },
940 { type => "pointerUp", button => 1 }, 939 { type => "pointerUp", button => 0 },
941 ], 940 ],
942 }, 941 },
943 { 942 {
944 id => "mykeyboard", 943 id => "mykeyboard",
945 type => "key", 944 type => "key",
963 ], 962 ],
964 }, 963 },
965 ]); 964 ]);
966 965
967And here is essentially the same (except for fewer pauses) example as 966And here is essentially the same (except for fewer pauses) example as
968above, using the much simpler C<AnyEvent::WebDriver::Actions> API. Note 967above, using the much simpler C<AnyEvent::WebDriver::Actions> API:
969that the pointer up and key down event happen concurrently in this
970example:
971 968
972 $wd->navigate_to ("https://duckduckgo.com/html"); 969 $wd->navigate_to ("https://duckduckgo.com/html");
973 my $input = $wd->find_element ("css selector", 'input[type="text"]'); 970 my $input = $wd->find_element ("css selector", 'input[type="text"]');
974 $wd->actions 971 $wd->actions
975 ->move ($input, 40, 5, "touch1") 972 ->move ($input, 40, 5, "touch1")
976 ->click 973 ->click
977 ->pause # to separate the click from the keypress
978 ->key ("a") 974 ->key ("a")
979 ->key ("b") 975 ->key ("b")
980 ->pause (2000) # so you can watch leisurely 976 ->pause (2000) # so you can watch leisurely
981 ->key ("{Enter}") 977 ->key ("{Enter}")
982 ->pause (5000) # so you can see the result 978 ->pause (5000) # so you can see the result
1078Action lists can be quite complicated. Or at least it took a while for 1074Action lists can be quite complicated. Or at least it took a while for
1079me to twist my head around them. Basically, an action list consists of a 1075me to twist my head around them. Basically, an action list consists of a
1080number of sources representing devices (such as a finger, a mouse, a pen 1076number of sources representing devices (such as a finger, a mouse, a pen
1081or a keyboard) and a list of actions for each source. 1077or a keyboard) and a list of actions for each source.
1082 1078
1083An action can be a key press, a pointer move or a pause (time 1079An action can be a key press, a pointer move or a pause (time delay).
1084delay). Actions from different sources can happen "at the same time",
1085while actions from a single source are executed in order.
1086 1080
1087While you can provide an action list manually, it is (hopefully) less 1081While you can provide these action lists manually, it is (hopefully) less
1088cumbersome to use the API described in this section to create them. 1082cumbersome to use the API described in this section to create them.
1089 1083
1090The basic process of creating and performing actions is to create a new 1084The basic process of creating and performing actions is to create a new
1091action list, adding action sources, followed by adding actions. Finally 1085action list, adding action sources, followed by adding actions. Finally
1092you would C<perform> those actions on the WebDriver. 1086you would C<perform> those actions on the WebDriver.
1093 1087
1094Virtual time progresses as long as you add actions to the same event
1095source. Adding events to different sources are considered to happen
1096concurrently. If you want to force time to progress, you can do this using
1097a call to C<< ->pause (0) >>.
1098
1099Most methods here are designed to chain, i.e. they return the web actions 1088Most methods here are designed to chain, i.e. they return the web actions
1100object, to simplify multiple calls. 1089object, to simplify multiple calls.
1101 1090
1091Also, while actions from different sources can happen "at the same time"
1092in the WebDriver protocol, this class ensures that actions will execute in
1093the order specified.
1094
1102For example, to simulate a mouse click to an input element, followed by 1095For example, to simulate a mouse click to an input element, followed by
1103entering some text and pressing enter, you can use this: 1096entering some text and pressing enter, you can use this:
1104 1097
1105 $wd->actions 1098 $wd->actions
1106 ->click (1, 100) 1099 ->click (0, 100)
1107 ->pause # to separate the click from keypressed
1108 ->type ("some text") 1100 ->type ("some text")
1109 ->key ("{Enter}") 1101 ->key ("{Enter}")
1110 ->perform; 1102 ->perform;
1111 1103
1112By default, keyboard and mouse input sources are provided. You can create 1104By default, keyboard and mouse input sources are provided. You can create
1114be more verbosely written like this: 1106be more verbosely written like this:
1115 1107
1116 $wd->actions 1108 $wd->actions
1117 ->source ("mouse", "pointer", pointerType => "mouse") 1109 ->source ("mouse", "pointer", pointerType => "mouse")
1118 ->source ("kbd", "key") 1110 ->source ("kbd", "key")
1119 ->click (1, 100, "mouse") 1111 ->click (0, 100, "mouse")
1120 ->pause
1121 ->type ("some text", "kbd") 1112 ->type ("some text", "kbd")
1122 ->key ("{Enter}", "kbd") 1113 ->key ("{Enter}", "kbd")
1123 ->perform; 1114 ->perform;
1124 1115
1125When you specify the event source explicitly it will switch the current 1116When you specify the event source explicitly it will switch the current
1227 my $source = $self->{source}{$source} ||= _default_source $source; 1218 my $source = $self->{source}{$source} ||= _default_source $source;
1228 1219
1229 my $al = $source->{actions}; 1220 my $al = $source->{actions};
1230 1221
1231 push @$al, { type => "pause" } 1222 push @$al, { type => "pause" }
1232 while @$al < $self->{tick} - 1; 1223 while @$al < $self->{tick}; # -1 == allow concurrent actions
1233 1224
1234 $kv{type} = $type; 1225 $kv{type} = $type;
1235 1226
1236 push @{ $source->{actions} }, \%kv; 1227 push @{ $source->{actions} }, \%kv;
1237 1228
1289 1280
1290=item $al = $al->pointer_down ($button, $source) 1281=item $al = $al->pointer_down ($button, $source)
1291 1282
1292=item $al = $al->pointer_up ($button, $source) 1283=item $al = $al->pointer_up ($button, $source)
1293 1284
1294Press or release the given button. C<$button> defaults to C<1>. 1285Press or release the given button. C<$button> defaults to C<0>.
1295 1286
1296=item $al = $al->click ($button, $source) 1287=item $al = $al->click ($button, $source)
1297 1288
1298Convenience function that creates a button press and release action 1289Convenience function that creates a button press and release action
1299without any delay between them. C<$button> defaults to C<1>. 1290without any delay between them. C<$button> defaults to C<0>.
1300 1291
1301=item $al = $al->doubleclick ($button, $source) 1292=item $al = $al->doubleclick ($button, $source)
1302 1293
1303Convenience function that creates two button press and release action 1294Convenience function that creates two button press and release action
1304pairs in a row, with no unnecessary delay between them. C<$button> 1295pairs in a row, with no unnecessary delay between them. C<$button>
1305defaults to C<1>. 1296defaults to C<0>.
1306 1297
1307=cut 1298=cut
1308 1299
1309sub pointer_down { 1300sub pointer_down {
1310 my ($self, $button, $source) = @_; 1301 my ($self, $button, $source) = @_;
1311 1302
1312 $self->_add ($source, ptr => pointerDown => button => ($button // 1)*1) 1303 $self->_add ($source, ptr => pointerDown => button => ($button // 0)*1)
1313} 1304}
1314 1305
1315sub pointer_up { 1306sub pointer_up {
1316 my ($self, $button, $source) = @_; 1307 my ($self, $button, $source) = @_;
1317 1308
1318 $self->_add ($source, ptr => pointerUp => button => ($button // 1)*1) 1309 $self->_add ($source, ptr => pointerUp => button => ($button // 0)*1)
1319} 1310}
1320 1311
1321sub click { 1312sub click {
1322 my ($self, $button, $source) = @_; 1313 my ($self, $button, $source) = @_;
1323 1314
1394"as-is" if possible. 1385"as-is" if possible.
1395 1386
1396=cut 1387=cut
1397 1388
1398our %SPECIAL_KEY = ( 1389our %SPECIAL_KEY = (
1390# "NULL" => \xE000,
1399 "Unidentified" => 0xE000, 1391 "Unidentified" => 0xE000,
1400 "Cancel" => 0xE001, 1392 "Cancel" => 0xE001,
1401 "Help" => 0xE002, 1393 "Help" => 0xE002,
1402 "Backspace" => 0xE003, 1394 "Backspace" => 0xE003,
1403 "Tab" => 0xE004, 1395 "Tab" => 0xE004,

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines