selenium-webdriver 3.5.0 → 4.0.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (366) hide show
  1. package/CHANGES.md +246 -0
  2. package/LICENSE +1 -1
  3. package/NOTICE +1 -1
  4. package/README.md +40 -40
  5. package/chrome.js +118 -134
  6. package/edge.js +43 -117
  7. package/example/chrome_android.js +8 -9
  8. package/example/chrome_mobile_emulation.js +8 -9
  9. package/example/firefox_channels.js +23 -19
  10. package/example/google_search.js +3 -3
  11. package/example/google_search_test.js +38 -27
  12. package/example/{chrome_headless.js → headless.js} +15 -7
  13. package/example/logging.js +40 -7
  14. package/firefox.js +789 -0
  15. package/http/index.js +62 -9
  16. package/http/util.js +11 -6
  17. package/ie.js +83 -97
  18. package/index.js +198 -114
  19. package/io/exec.js +29 -15
  20. package/io/index.js +1 -1
  21. package/io/zip.js +1 -1
  22. package/lib/actions.js +68 -63
  23. package/lib/atoms/get-attribute.js +86 -0
  24. package/lib/atoms/is-displayed.js +76 -76
  25. package/lib/by.js +7 -4
  26. package/lib/capabilities.js +310 -210
  27. package/lib/command.js +27 -51
  28. package/lib/error.js +100 -41
  29. package/lib/http.js +161 -120
  30. package/lib/input.js +1044 -8
  31. package/lib/logging.js +1 -5
  32. package/lib/promise.js +222 -3331
  33. package/lib/proxy.js +134 -39
  34. package/lib/session.js +2 -2
  35. package/lib/symbols.js +0 -0
  36. package/lib/until.js +1 -1
  37. package/lib/webdriver.js +717 -953
  38. package/net/index.js +21 -25
  39. package/net/portprober.js +51 -68
  40. package/package.json +17 -12
  41. package/proxy.js +0 -0
  42. package/remote/index.js +63 -44
  43. package/safari.js +41 -128
  44. package/testing/index.js +386 -315
  45. package/.npmignore +0 -2
  46. package/example/async_await_test.js +0 -68
  47. package/example/google_search_generator.js +0 -50
  48. package/example/parallel_flows.js +0 -54
  49. package/firefox/binary.js +0 -344
  50. package/firefox/extension.js +0 -224
  51. package/firefox/index.js +0 -626
  52. package/firefox/profile.js +0 -394
  53. package/lib/README +0 -5
  54. package/lib/atoms/getAttribute.js +0 -9
  55. package/lib/devmode.js +0 -34
  56. package/lib/events.js +0 -210
  57. package/lib/firefox/webdriver.json +0 -70
  58. package/lib/test/build.js +0 -151
  59. package/lib/test/data/ClickTest_testClicksASurroundingStrongTag.html +0 -11
  60. package/lib/test/data/Page.aspx +0 -17
  61. package/lib/test/data/Page.aspx.cs +0 -22
  62. package/lib/test/data/Redirect.aspx +0 -11
  63. package/lib/test/data/Redirect.aspx.cs +0 -9
  64. package/lib/test/data/Settings.StyleCop +0 -759
  65. package/lib/test/data/Web.Config +0 -59
  66. package/lib/test/data/actualXhtmlPage.xhtml +0 -14
  67. package/lib/test/data/ajaxy_page.html +0 -81
  68. package/lib/test/data/alerts.html +0 -85
  69. package/lib/test/data/banner.gif +0 -0
  70. package/lib/test/data/beach.jpg +0 -0
  71. package/lib/test/data/blank.html +0 -1
  72. package/lib/test/data/bodyTypingTest.html +0 -41
  73. package/lib/test/data/booleanAttributes.html +0 -19
  74. package/lib/test/data/child/childPage.html +0 -8
  75. package/lib/test/data/child/grandchild/grandchildPage.html +0 -8
  76. package/lib/test/data/clickEventPage.html +0 -26
  77. package/lib/test/data/click_frames.html +0 -10
  78. package/lib/test/data/click_jacker.html +0 -38
  79. package/lib/test/data/click_out_of_bounds.html +0 -23
  80. package/lib/test/data/click_out_of_bounds_overflow.html +0 -85
  81. package/lib/test/data/click_rtl.html +0 -19
  82. package/lib/test/data/click_source.html +0 -18
  83. package/lib/test/data/click_tests/click_iframe.html +0 -6
  84. package/lib/test/data/click_tests/click_in_iframe.html +0 -8
  85. package/lib/test/data/click_tests/disabled_element.html +0 -12
  86. package/lib/test/data/click_tests/disappearing_element.html +0 -62
  87. package/lib/test/data/click_tests/google_map.html +0 -15
  88. package/lib/test/data/click_tests/google_map.png +0 -0
  89. package/lib/test/data/click_tests/html5_submit_buttons.html +0 -16
  90. package/lib/test/data/click_tests/issue5237.html +0 -9
  91. package/lib/test/data/click_tests/issue5237_frame.html +0 -1
  92. package/lib/test/data/click_tests/issue5237_target.html +0 -10
  93. package/lib/test/data/click_tests/link_that_wraps.html +0 -11
  94. package/lib/test/data/click_tests/mapped_page1.html +0 -9
  95. package/lib/test/data/click_tests/mapped_page2.html +0 -9
  96. package/lib/test/data/click_tests/mapped_page3.html +0 -9
  97. package/lib/test/data/click_tests/overlapping_elements.html +0 -70
  98. package/lib/test/data/click_tests/partially_overlapping_elements.html +0 -124
  99. package/lib/test/data/click_tests/span_that_wraps.html +0 -11
  100. package/lib/test/data/click_tests/submitted_page.html +0 -9
  101. package/lib/test/data/click_tests/wrapped_overlapping_elements.html +0 -13
  102. package/lib/test/data/click_too_big.html +0 -10
  103. package/lib/test/data/click_too_big_in_frame.html +0 -11
  104. package/lib/test/data/clicks.html +0 -35
  105. package/lib/test/data/closeable_window.html +0 -8
  106. package/lib/test/data/cn-test.html +0 -156
  107. package/lib/test/data/colorPage.html +0 -20
  108. package/lib/test/data/cookies.html +0 -30
  109. package/lib/test/data/coordinates_tests/element_in_frame.html +0 -9
  110. package/lib/test/data/coordinates_tests/element_in_nested_frame.html +0 -9
  111. package/lib/test/data/coordinates_tests/page_with_element_out_of_view.html +0 -11
  112. package/lib/test/data/coordinates_tests/page_with_empty_element.html +0 -10
  113. package/lib/test/data/coordinates_tests/page_with_fixed_element.html +0 -12
  114. package/lib/test/data/coordinates_tests/page_with_hidden_element.html +0 -10
  115. package/lib/test/data/coordinates_tests/page_with_invisible_element.html +0 -10
  116. package/lib/test/data/coordinates_tests/page_with_transparent_element.html +0 -10
  117. package/lib/test/data/coordinates_tests/simple_page.html +0 -10
  118. package/lib/test/data/css/ui-lightness/images/ui-bg_diagonals-thick_18_b81900_40x40.png +0 -0
  119. package/lib/test/data/css/ui-lightness/images/ui-bg_diagonals-thick_20_666666_40x40.png +0 -0
  120. package/lib/test/data/css/ui-lightness/images/ui-bg_flat_10_000000_40x100.png +0 -0
  121. package/lib/test/data/css/ui-lightness/images/ui-bg_glass_100_f6f6f6_1x400.png +0 -0
  122. package/lib/test/data/css/ui-lightness/images/ui-bg_glass_100_fdf5ce_1x400.png +0 -0
  123. package/lib/test/data/css/ui-lightness/images/ui-bg_glass_65_ffffff_1x400.png +0 -0
  124. package/lib/test/data/css/ui-lightness/images/ui-bg_gloss-wave_35_f6a828_500x100.png +0 -0
  125. package/lib/test/data/css/ui-lightness/images/ui-bg_highlight-soft_100_eeeeee_1x100.png +0 -0
  126. package/lib/test/data/css/ui-lightness/images/ui-bg_highlight-soft_75_ffe45c_1x100.png +0 -0
  127. package/lib/test/data/css/ui-lightness/images/ui-icons_222222_256x240.png +0 -0
  128. package/lib/test/data/css/ui-lightness/images/ui-icons_228ef1_256x240.png +0 -0
  129. package/lib/test/data/css/ui-lightness/images/ui-icons_ef8c08_256x240.png +0 -0
  130. package/lib/test/data/css/ui-lightness/images/ui-icons_ffd27a_256x240.png +0 -0
  131. package/lib/test/data/css/ui-lightness/images/ui-icons_ffffff_256x240.png +0 -0
  132. package/lib/test/data/css/ui-lightness/jquery-ui-1.8.10.custom.css +0 -573
  133. package/lib/test/data/cssTransform.html +0 -61
  134. package/lib/test/data/cssTransform2.html +0 -20
  135. package/lib/test/data/document_write_in_onload.html +0 -13
  136. package/lib/test/data/dragAndDropInsideScrolledDiv.html +0 -67
  137. package/lib/test/data/dragAndDropTest.html +0 -102
  138. package/lib/test/data/dragDropOverflow.html +0 -104
  139. package/lib/test/data/draggableLists.html +0 -67
  140. package/lib/test/data/droppableItems.html +0 -65
  141. package/lib/test/data/dynamic.html +0 -39
  142. package/lib/test/data/dynamicallyModifiedPage.html +0 -42
  143. package/lib/test/data/errors.html +0 -15
  144. package/lib/test/data/firefox/jetpack-sample.xpi +0 -0
  145. package/lib/test/data/firefox/sample.xpi +0 -0
  146. package/lib/test/data/firefox/webextension.xpi +0 -0
  147. package/lib/test/data/fixedFooterNoScroll.html +0 -13
  148. package/lib/test/data/fixedFooterNoScrollQuirksMode.html +0 -12
  149. package/lib/test/data/formPage.html +0 -175
  150. package/lib/test/data/formSelectionPage.html +0 -46
  151. package/lib/test/data/form_handling_js_submit.html +0 -30
  152. package/lib/test/data/framePage3.html +0 -7
  153. package/lib/test/data/frameScrollChild.html +0 -26
  154. package/lib/test/data/frameScrollPage.html +0 -14
  155. package/lib/test/data/frameScrollParent.html +0 -11
  156. package/lib/test/data/frameWithAnimals.html +0 -11
  157. package/lib/test/data/frame_switching_tests/bug4876.html +0 -9
  158. package/lib/test/data/frame_switching_tests/bug4876_iframe.html +0 -9
  159. package/lib/test/data/frame_switching_tests/deletingFrame.html +0 -29
  160. package/lib/test/data/frame_switching_tests/deletingFrame_iframe.html +0 -8
  161. package/lib/test/data/frame_switching_tests/deletingFrame_iframe2.html +0 -7
  162. package/lib/test/data/frameset.html +0 -14
  163. package/lib/test/data/framesetPage2.html +0 -7
  164. package/lib/test/data/framesetPage3.html +0 -4
  165. package/lib/test/data/globalscope.html +0 -15
  166. package/lib/test/data/hidden.html +0 -5
  167. package/lib/test/data/hidden_partially.html +0 -45
  168. package/lib/test/data/html5/blue.jpg +0 -0
  169. package/lib/test/data/html5/database.js +0 -84
  170. package/lib/test/data/html5/geolocation.js +0 -18
  171. package/lib/test/data/html5/green.jpg +0 -0
  172. package/lib/test/data/html5/red.jpg +0 -0
  173. package/lib/test/data/html5/status.html +0 -1
  174. package/lib/test/data/html5/test.appcache +0 -11
  175. package/lib/test/data/html5/yellow.jpg +0 -0
  176. package/lib/test/data/html5Page.html +0 -32
  177. package/lib/test/data/icon.gif +0 -0
  178. package/lib/test/data/idElements.html +0 -2
  179. package/lib/test/data/iframeAtBottom.html +0 -15
  180. package/lib/test/data/iframeWithAlert.html +0 -1
  181. package/lib/test/data/iframeWithIframe.html +0 -1
  182. package/lib/test/data/iframes.html +0 -11
  183. package/lib/test/data/injectableContent.html +0 -22
  184. package/lib/test/data/javascriptEnhancedForm.html +0 -30
  185. package/lib/test/data/javascriptPage.html +0 -285
  186. package/lib/test/data/jquery-1.3.2.js +0 -4376
  187. package/lib/test/data/js/jquery-1.4.4.min.js +0 -167
  188. package/lib/test/data/js/jquery-ui-1.8.10.custom.min.js +0 -782
  189. package/lib/test/data/js/skins/lightgray/content.inline.min.css +0 -1
  190. package/lib/test/data/js/skins/lightgray/content.min.css +0 -1
  191. package/lib/test/data/js/skins/lightgray/fonts/readme.md +0 -1
  192. package/lib/test/data/js/skins/lightgray/fonts/tinymce-small.dev.svg +0 -175
  193. package/lib/test/data/js/skins/lightgray/fonts/tinymce-small.eot +0 -0
  194. package/lib/test/data/js/skins/lightgray/fonts/tinymce-small.svg +0 -62
  195. package/lib/test/data/js/skins/lightgray/fonts/tinymce-small.ttf +0 -0
  196. package/lib/test/data/js/skins/lightgray/fonts/tinymce-small.woff +0 -0
  197. package/lib/test/data/js/skins/lightgray/fonts/tinymce.dev.svg +0 -153
  198. package/lib/test/data/js/skins/lightgray/fonts/tinymce.eot +0 -0
  199. package/lib/test/data/js/skins/lightgray/fonts/tinymce.svg +0 -63
  200. package/lib/test/data/js/skins/lightgray/fonts/tinymce.ttf +0 -0
  201. package/lib/test/data/js/skins/lightgray/fonts/tinymce.woff +0 -0
  202. package/lib/test/data/js/skins/lightgray/img/anchor.gif +0 -0
  203. package/lib/test/data/js/skins/lightgray/img/loader.gif +0 -0
  204. package/lib/test/data/js/skins/lightgray/img/object.gif +0 -0
  205. package/lib/test/data/js/skins/lightgray/img/trans.gif +0 -0
  206. package/lib/test/data/js/skins/lightgray/skin.ie7.min.css +0 -1
  207. package/lib/test/data/js/skins/lightgray/skin.min.css +0 -1
  208. package/lib/test/data/js/themes/modern/theme.min.js +0 -1
  209. package/lib/test/data/js/tinymce.min.js +0 -10
  210. package/lib/test/data/key_logger.html +0 -34
  211. package/lib/test/data/key_tests/remove_on_keypress.html +0 -36
  212. package/lib/test/data/keyboard_shortcut.html +0 -36
  213. package/lib/test/data/linked_image.html +0 -16
  214. package/lib/test/data/locators_tests/boolean_attribute_selected.html +0 -13
  215. package/lib/test/data/locators_tests/boolean_attribute_selected_html4.html +0 -13
  216. package/lib/test/data/longContentPage.html +0 -55
  217. package/lib/test/data/macbeth.html +0 -5255
  218. package/lib/test/data/map.png +0 -0
  219. package/lib/test/data/map_visibility.html +0 -8
  220. package/lib/test/data/markerTransparent.png +0 -0
  221. package/lib/test/data/messages.html +0 -15
  222. package/lib/test/data/meta-redirect.html +0 -11
  223. package/lib/test/data/missedJsReference.html +0 -11
  224. package/lib/test/data/modal_dialogs/modal_1.html +0 -21
  225. package/lib/test/data/modal_dialogs/modal_2.html +0 -21
  226. package/lib/test/data/modal_dialogs/modal_3.html +0 -15
  227. package/lib/test/data/modal_dialogs/modalindex.html +0 -21
  228. package/lib/test/data/mouseOver.html +0 -17
  229. package/lib/test/data/mousePositionTracker.html +0 -33
  230. package/lib/test/data/nestedElements.html +0 -164
  231. package/lib/test/data/overflow/x_auto_y_auto.html +0 -30
  232. package/lib/test/data/overflow/x_auto_y_hidden.html +0 -30
  233. package/lib/test/data/overflow/x_auto_y_scroll.html +0 -30
  234. package/lib/test/data/overflow/x_hidden_y_auto.html +0 -30
  235. package/lib/test/data/overflow/x_hidden_y_hidden.html +0 -30
  236. package/lib/test/data/overflow/x_hidden_y_scroll.html +0 -30
  237. package/lib/test/data/overflow/x_scroll_y_auto.html +0 -30
  238. package/lib/test/data/overflow/x_scroll_y_hidden.html +0 -30
  239. package/lib/test/data/overflow/x_scroll_y_scroll.html +0 -30
  240. package/lib/test/data/overflow-body.html +0 -15
  241. package/lib/test/data/pageWithOnBeforeUnloadMessage.html +0 -20
  242. package/lib/test/data/pageWithOnLoad.html +0 -6
  243. package/lib/test/data/pageWithOnUnload.html +0 -6
  244. package/lib/test/data/page_with_link_to_slow_loading_page.html +0 -6
  245. package/lib/test/data/plain.txt +0 -1
  246. package/lib/test/data/proxy/page1.html +0 -20
  247. package/lib/test/data/proxy/page2.html +0 -24
  248. package/lib/test/data/proxy/page3.html +0 -5
  249. package/lib/test/data/readOnlyPage.html +0 -24
  250. package/lib/test/data/rectangles.html +0 -40
  251. package/lib/test/data/resultPage.html +0 -25
  252. package/lib/test/data/rich_text.html +0 -161
  253. package/lib/test/data/safari/frames_benchmark.html +0 -31
  254. package/lib/test/data/screen/screen.css +0 -19
  255. package/lib/test/data/screen/screen.html +0 -72
  256. package/lib/test/data/screen/screen.js +0 -7
  257. package/lib/test/data/screen/screen_frame1.html +0 -72
  258. package/lib/test/data/screen/screen_frame2.html +0 -72
  259. package/lib/test/data/screen/screen_frames.html +0 -11
  260. package/lib/test/data/screen/screen_iframes.html +0 -12
  261. package/lib/test/data/screen/screen_too_long.html +0 -68
  262. package/lib/test/data/screen/screen_x_long.html +0 -72
  263. package/lib/test/data/screen/screen_x_too_long.html +0 -72
  264. package/lib/test/data/screen/screen_y_long.html +0 -72
  265. package/lib/test/data/screen/screen_y_too_long.html +0 -72
  266. package/lib/test/data/scroll.html +0 -27
  267. package/lib/test/data/scroll2.html +0 -21
  268. package/lib/test/data/scroll3.html +0 -8
  269. package/lib/test/data/scroll4.html +0 -7
  270. package/lib/test/data/scroll5.html +0 -18
  271. package/lib/test/data/scrolling_tests/frame_with_height_above_200.html +0 -26
  272. package/lib/test/data/scrolling_tests/frame_with_height_above_2000.html +0 -26
  273. package/lib/test/data/scrolling_tests/frame_with_nested_scrolling_frame.html +0 -11
  274. package/lib/test/data/scrolling_tests/frame_with_nested_scrolling_frame_out_of_view.html +0 -12
  275. package/lib/test/data/scrolling_tests/frame_with_small_height.html +0 -10
  276. package/lib/test/data/scrolling_tests/page_with_double_overflow_auto.html +0 -19
  277. package/lib/test/data/scrolling_tests/page_with_frame_out_of_view.html +0 -12
  278. package/lib/test/data/scrolling_tests/page_with_nested_scrolling_frames.html +0 -11
  279. package/lib/test/data/scrolling_tests/page_with_nested_scrolling_frames_out_of_view.html +0 -12
  280. package/lib/test/data/scrolling_tests/page_with_non_scrolling_frame.html +0 -11
  281. package/lib/test/data/scrolling_tests/page_with_scrolling_frame.html +0 -11
  282. package/lib/test/data/scrolling_tests/page_with_scrolling_frame_out_of_view.html +0 -12
  283. package/lib/test/data/scrolling_tests/page_with_tall_frame.html +0 -11
  284. package/lib/test/data/scrolling_tests/page_with_y_overflow_auto.html +0 -14
  285. package/lib/test/data/scrolling_tests/target_page.html +0 -9
  286. package/lib/test/data/selectPage.html +0 -58
  287. package/lib/test/data/selectableItems.html +0 -65
  288. package/lib/test/data/sessionCookie.html +0 -21
  289. package/lib/test/data/sessionCookieDest.html +0 -34
  290. package/lib/test/data/simple.xml +0 -5
  291. package/lib/test/data/simpleTest.html +0 -98
  292. package/lib/test/data/single_text_input.html +0 -12
  293. package/lib/test/data/slowLoadingAlert.html +0 -10
  294. package/lib/test/data/slowLoadingResourcePage.html +0 -12
  295. package/lib/test/data/slow_loading_iframes.html +0 -14
  296. package/lib/test/data/styledPage.html +0 -28
  297. package/lib/test/data/svgPiechart.xhtml +0 -81
  298. package/lib/test/data/svgTest.svg +0 -4
  299. package/lib/test/data/tables.html +0 -36
  300. package/lib/test/data/tinymce.html +0 -10
  301. package/lib/test/data/transformable.xml +0 -11
  302. package/lib/test/data/transformable.xsl +0 -37
  303. package/lib/test/data/transparentUpload.html +0 -70
  304. package/lib/test/data/underscore.html +0 -9
  305. package/lib/test/data/unicode_ltr.html +0 -8
  306. package/lib/test/data/upload.html +0 -45
  307. package/lib/test/data/userDefinedProperty.html +0 -8
  308. package/lib/test/data/veryLargeCanvas.html +0 -81
  309. package/lib/test/data/visibility-css.html +0 -21
  310. package/lib/test/data/win32frameset.html +0 -8
  311. package/lib/test/data/window_switching_tests/page_with_frame.html +0 -12
  312. package/lib/test/data/window_switching_tests/simple_page.html +0 -9
  313. package/lib/test/data/xhtmlFormPage.xhtml +0 -17
  314. package/lib/test/data/xhtmlTest.html +0 -76
  315. package/lib/test/fileserver.js +0 -321
  316. package/lib/test/httpserver.js +0 -120
  317. package/lib/test/index.js +0 -270
  318. package/lib/test/promise.js +0 -79
  319. package/lib/test/resources.js +0 -44
  320. package/opera.js +0 -405
  321. package/phantomjs.js +0 -282
  322. package/test/actions_test.js +0 -52
  323. package/test/chrome/options_test.js +0 -227
  324. package/test/chrome/service_test.js +0 -45
  325. package/test/cookie_test.js +0 -214
  326. package/test/element_finding_test.js +0 -426
  327. package/test/execute_script_test.js +0 -350
  328. package/test/fingerprint_test.js +0 -62
  329. package/test/firefox/extension_test.js +0 -120
  330. package/test/firefox/firefox_test.js +0 -261
  331. package/test/firefox/profile_test.js +0 -140
  332. package/test/http/http_test.js +0 -223
  333. package/test/http/util_test.js +0 -178
  334. package/test/io/io_test.js +0 -360
  335. package/test/io/zip_test.js +0 -128
  336. package/test/lib/by_test.js +0 -160
  337. package/test/lib/capabilities_test.js +0 -111
  338. package/test/lib/error_test.js +0 -305
  339. package/test/lib/events_test.js +0 -177
  340. package/test/lib/http_test.js +0 -696
  341. package/test/lib/logging_test.js +0 -272
  342. package/test/lib/promise_aplus_test.js +0 -78
  343. package/test/lib/promise_error_test.js +0 -884
  344. package/test/lib/promise_flow_test.js +0 -2288
  345. package/test/lib/promise_generator_test.js +0 -310
  346. package/test/lib/promise_test.js +0 -1109
  347. package/test/lib/testutil.js +0 -90
  348. package/test/lib/until_test.js +0 -478
  349. package/test/lib/webdriver_test.js +0 -2311
  350. package/test/logging_test.js +0 -167
  351. package/test/net/index_test.js +0 -60
  352. package/test/net/portprober_test.js +0 -128
  353. package/test/page_loading_test.js +0 -166
  354. package/test/phantomjs/execute_phantomjs_test.js +0 -59
  355. package/test/proxy_test.js +0 -180
  356. package/test/rect_test.js +0 -60
  357. package/test/remote_test.js +0 -117
  358. package/test/safari_test.js +0 -108
  359. package/test/session_test.js +0 -99
  360. package/test/stale_element_test.js +0 -63
  361. package/test/tag_name_test.js +0 -36
  362. package/test/testing/assert_test.js +0 -373
  363. package/test/testing/index_test.js +0 -224
  364. package/test/upload_test.js +0 -86
  365. package/test/window_test.js +0 -161
  366. package/testing/assert.js +0 -378
package/lib/input.js CHANGED
@@ -21,6 +21,13 @@
21
21
  * @fileoverview Defines types related to user input with the WebDriver API.
22
22
  */
23
23
 
24
+ const promise = require('./promise');
25
+ const {Command, Executor, Name} = require('./command');
26
+ const {
27
+ InvalidArgumentError,
28
+ UnknownCommandError,
29
+ UnsupportedOperationError
30
+ } = require('./error');
24
31
 
25
32
  /**
26
33
  * Enumeration of the buttons used in the advanced interactions API.
@@ -37,9 +44,10 @@ const Button = {
37
44
  /**
38
45
  * Representations of pressable keys that aren't text. These are stored in
39
46
  * the Unicode PUA (Private Use Area) code points, 0xE000-0xF8FF. Refer to
40
- * http://www.google.com.au/search?&q=unicode+pua&btnG=Search
47
+ * http://www.google.com.au/search?&q=unicode+pua&btnK=Search
41
48
  *
42
49
  * @enum {string}
50
+ * @see <https://www.w3.org/TR/webdriver/#keyboard-actions>
43
51
  */
44
52
  const Key = {
45
53
  NULL: '\uE000',
@@ -104,7 +112,14 @@ const Key = {
104
112
  F12: '\uE03C',
105
113
 
106
114
  COMMAND: '\uE03D', // Apple command key
107
- META: '\uE03D' // alias for Windows key
115
+ META: '\uE03D', // alias for Windows key
116
+
117
+ /**
118
+ * Japanese modifier key for switching between full- and half-width
119
+ * characters.
120
+ * @see <https://en.wikipedia.org/wiki/Language_input_keys>
121
+ */
122
+ ZENKAKU_HANKAKU: '\uE040',
108
123
  };
109
124
 
110
125
 
@@ -117,11 +132,11 @@ const Key = {
117
132
  * Note: when the low-level webdriver key handlers see Keys.NULL, active
118
133
  * modifier keys (CTRL/ALT/SHIFT/etc) release via a keyup event.
119
134
  *
120
- * @param {...string} var_args The key sequence to concatenate.
135
+ * @param {...string} keys The key sequence to concatenate.
121
136
  * @return {string} The null-terminated key sequence.
122
137
  */
123
- Key.chord = function(var_args) {
124
- return Array.prototype.slice.call(arguments, 0).join('') + Key.NULL;
138
+ Key.chord = function(...keys) {
139
+ return keys.join('') + Key.NULL;
125
140
  };
126
141
 
127
142
 
@@ -161,11 +176,1032 @@ class FileDetector {
161
176
  }
162
177
 
163
178
 
179
+ /**
180
+ * Generic description of a single action to send to the remote end.
181
+ *
182
+ * @record
183
+ * @package
184
+ */
185
+ class Action {
186
+ constructor() {
187
+ /** @type {!Action.Type} */
188
+ this.type;
189
+ /** @type {(number|undefined)} */
190
+ this.duration;
191
+ /** @type {(string|undefined)} */
192
+ this.value;
193
+ /** @type {(Button|undefined)} */
194
+ this.button;
195
+ /** @type {(number|undefined)} */
196
+ this.x;
197
+ /** @type {(number|undefined)} */
198
+ this.y;
199
+ }
200
+ }
201
+
202
+
203
+ /**
204
+ * @enum {string}
205
+ * @package
206
+ * @see <https://w3c.github.io/webdriver/webdriver-spec.html#terminology-0>
207
+ */
208
+ Action.Type = {
209
+ KEY_DOWN: 'keyDown',
210
+ KEY_UP: 'keyUp',
211
+ PAUSE: 'pause',
212
+ POINTER_DOWN: 'pointerDown',
213
+ POINTER_UP: 'pointerUp',
214
+ POINTER_MOVE: 'pointerMove',
215
+ POINTER_CANCEL: 'pointerCancel'
216
+ };
217
+
218
+
219
+ /**
220
+ * Represents a user input device.
221
+ *
222
+ * @abstract
223
+ */
224
+ class Device {
225
+ /**
226
+ * @param {Device.Type} type the input type.
227
+ * @param {string} id a unique ID for this device.
228
+ */
229
+ constructor(type, id) {
230
+ /** @private @const */ this.type_ = type;
231
+ /** @private @const */ this.id_ = id;
232
+ }
233
+
234
+ /** @return {!Object} the JSON encoding for this device. */
235
+ toJSON() {
236
+ return {'type': this.type_, 'id': this.id_};
237
+ }
238
+ }
239
+
240
+
241
+ /**
242
+ * Device types supported by the WebDriver protocol.
243
+ *
244
+ * @enum {string}
245
+ * @see <https://w3c.github.io/webdriver/webdriver-spec.html#input-source-state>
246
+ */
247
+ Device.Type = {
248
+ KEY: 'key',
249
+ NONE: 'none',
250
+ POINTER: 'pointer'
251
+ };
252
+
253
+
254
+ /**
255
+ * @param {(string|Key|number)} key
256
+ * @return {string}
257
+ * @throws {!(InvalidArgumentError|RangeError)}
258
+ */
259
+ function checkCodePoint(key) {
260
+ if (typeof key === 'number') {
261
+ return String.fromCodePoint(key);
262
+ }
263
+
264
+ if (typeof key !== 'string') {
265
+ throw new InvalidArgumentError(`key is not a string: ${key}`);
266
+ }
267
+
268
+ key = key.normalize();
269
+ if (Array.from(key).length != 1) {
270
+ throw new InvalidArgumentError(
271
+ `key input is not a single code point: ${key}`);
272
+ }
273
+ return key;
274
+ }
275
+
276
+
277
+ /**
278
+ * Keyboard input device.
279
+ *
280
+ * @final
281
+ * @see <https://www.w3.org/TR/webdriver/#dfn-key-input-source>
282
+ */
283
+ class Keyboard extends Device {
284
+ /** @param {string} id the device ID. */
285
+ constructor(id) {
286
+ super(Device.Type.KEY, id);
287
+ }
288
+
289
+ /**
290
+ * Generates a key down action.
291
+ *
292
+ * @param {(Key|string|number)} key the key to press. This key may be
293
+ * specified as a {@link Key} value, a specific unicode code point,
294
+ * or a string containing a single unicode code point.
295
+ * @return {!Action} a new key down action.
296
+ * @package
297
+ */
298
+ keyDown(key) {
299
+ return {type: Action.Type.KEY_DOWN, value: checkCodePoint(key)};
300
+ }
301
+
302
+ /**
303
+ * Generates a key up action.
304
+ *
305
+ * @param {(Key|string|number)} key the key to press. This key may be
306
+ * specified as a {@link Key} value, a specific unicode code point,
307
+ * or a string containing a single unicode code point.
308
+ * @return {!Action} a new key up action.
309
+ * @package
310
+ */
311
+ keyUp(key) {
312
+ return {type: Action.Type.KEY_UP, value: checkCodePoint(key)};
313
+ }
314
+ }
315
+
316
+
317
+ /**
318
+ * Defines the reference point from which to compute offsets for
319
+ * {@linkplain ./input.Pointer#move pointer move} actions.
320
+ *
321
+ * @enum {string}
322
+ */
323
+ const Origin = {
324
+ /** Compute offsets relative to the pointer's current position. */
325
+ POINTER: 'pointer',
326
+ /** Compute offsets relative to the viewport. */
327
+ VIEWPORT: 'viewport'
328
+ };
329
+
330
+
331
+ /**
332
+ * Pointer input device.
333
+ *
334
+ * @final
335
+ * @see <https://www.w3.org/TR/webdriver/#dfn-pointer-input-source>
336
+ */
337
+ class Pointer extends Device {
338
+ /**
339
+ * @param {string} id the device ID.
340
+ * @param {Pointer.Type} type the pointer type.
341
+ */
342
+ constructor(id, type) {
343
+ super(Device.Type.POINTER, id);
344
+ /** @private @const */ this.pointerType_ = type;
345
+ }
346
+
347
+ /** @override */
348
+ toJSON() {
349
+ return Object.assign(
350
+ {'parameters': {'pointerType': this.pointerType_}},
351
+ super.toJSON());
352
+ }
353
+
354
+ /**
355
+ * @return {!Action} An action that cancels this pointer's current input.
356
+ * @package
357
+ */
358
+ cancel() {
359
+ return {type: Action.Type.POINTER_CANCEL};
360
+ }
361
+
362
+ /**
363
+ * @param {!Button=} button The button to press.
364
+ * @return {!Action} An action to press the specified button with this device.
365
+ * @package
366
+ */
367
+ press(button = Button.LEFT) {
368
+ return {type: Action.Type.POINTER_DOWN, button};
369
+ }
370
+
371
+ /**
372
+ * @param {!Button=} button The button to release.
373
+ * @return {!Action} An action to release the specified button with this
374
+ * device.
375
+ * @package
376
+ */
377
+ release(button = Button.LEFT) {
378
+ return {type: Action.Type.POINTER_UP, button};
379
+ }
380
+
381
+ /**
382
+ * Creates an action for moving the pointer `x` and `y` pixels from the
383
+ * specified `origin`. The `origin` may be defined as the pointer's
384
+ * {@linkplain Origin.POINTER current position}, the
385
+ * {@linkplain Origin.VIEWPORT viewport}, or the center of a specific
386
+ * {@linkplain ./webdriver.WebElement WebElement}.
387
+ *
388
+ * @param {{
389
+ * x: (number|undefined),
390
+ * y: (number|undefined),
391
+ * duration: (number|undefined),
392
+ * origin: (!Origin|!./webdriver.WebElement|undefined),
393
+ * }=} options the move options.
394
+ * @return {!Action} The new action.
395
+ * @package
396
+ */
397
+ move({x = 0, y = 0, duration = 100, origin = Origin.VIEWPORT}) {
398
+ return {type: Action.Type.POINTER_MOVE, origin, duration, x, y};
399
+ }
400
+ }
401
+
402
+
403
+ /**
404
+ * The supported types of pointers.
405
+ * @enum {string}
406
+ */
407
+ Pointer.Type = {
408
+ MOUSE: 'mouse',
409
+ PEN: 'pen',
410
+ TOUCH: 'touch'
411
+ };
412
+
413
+
414
+ /**
415
+ * User facing API for generating complex user gestures. This class should not
416
+ * be instantiated directly. Instead, users should create new instances by
417
+ * calling {@link ./webdriver.WebDriver#actions WebDriver.actions()}.
418
+ *
419
+ * ### Action Ticks
420
+ *
421
+ * Action sequences are divided into a series of "ticks". At each tick, the
422
+ * WebDriver remote end will perform a single action for each device included
423
+ * in the action sequence. At tick 0, the driver will perform the first action
424
+ * defined for each device, at tick 1 the second action for each device, and
425
+ * so on until all actions have been executed. If an individual device does
426
+ * not have an action defined at a particular tick, it will automatically
427
+ * pause.
428
+ *
429
+ * By default, action sequences will be synchronized so only one device has a
430
+ * define action in each tick. Consider the following code sample:
431
+ *
432
+ * const actions = driver.actions();
433
+ *
434
+ * await actions
435
+ * .keyDown(SHIFT)
436
+ * .move({origin: el})
437
+ * .press()
438
+ * .release()
439
+ * .keyUp(SHIFT)
440
+ * .perform();
441
+ *
442
+ * This sample produces the following sequence of ticks:
443
+ *
444
+ * | Device | Tick 1 | Tick 2 | Tick 3 | Tick 4 | Tick 5 |
445
+ * | -------- | -------------- | ------------------ | ------- | --------- | ------------ |
446
+ * | Keyboard | keyDown(SHIFT) | pause() | pause() | pause() | keyUp(SHIFT) |
447
+ * | Mouse | pause() | move({origin: el}) | press() | release() | pause() |
448
+ *
449
+ * If you'd like the remote end to execute actions with multiple devices
450
+ * simultaneously, you may pass `{async: true}` when creating the actions
451
+ * builder. With synchronization disabled (`{async: true}`), the ticks from our
452
+ * previous example become:
453
+ *
454
+ * | Device | Tick 1 | Tick 2 | Tick 3 |
455
+ * | -------- | ------------------ | ------------ | --------- |
456
+ * | Keyboard | keyDown(SHIFT) | keyUp(SHIFT) | |
457
+ * | Mouse | move({origin: el}) | press() | release() |
458
+ *
459
+ * When synchronization is disabled, it is your responsibility to insert
460
+ * {@linkplain #pause() pauses} for each device, as needed:
461
+ *
462
+ * const actions = driver.actions({async: true});
463
+ * const kb = actions.keyboard();
464
+ * const mouse = actions.mouse();
465
+ *
466
+ * actions.keyDown(SHIFT).pause(kb).pause(kb).key(SHIFT);
467
+ * actions.pause(mouse).move({origin: el}).press().release();
468
+ * actions.perform();
469
+ *
470
+ * With pauses insert for individual devices, we're back to:
471
+ *
472
+ * | Device | Tick 1 | Tick 2 | Tick 3 | Tick 4 |
473
+ * | -------- | -------------- | ------------------ | ------- | ------------ |
474
+ * | Keyboard | keyDown(SHIFT) | pause() | pause() | keyUp(SHIFT) |
475
+ * | Mouse | pause() | move({origin: el}) | press() | release() |
476
+ *
477
+ * #### Tick Durations
478
+ *
479
+ * The length of each action tick is however long it takes the remote end to
480
+ * execute the actions for every device in that tick. Most actions are
481
+ * "instaneous", however, {@linkplain #pause pause} and
482
+ * {@linkplain #move pointer move} actions allow you to specify a duration for
483
+ * how long that action should take. The remote end will always wait for all
484
+ * actions within a tick to finish before starting the next tick, so a device
485
+ * may implicitly pause while waiting for other devices to finish.
486
+ *
487
+ * | Device | Tick 1 | Tick 2 |
488
+ * | --------- | --------------------- | ------- |
489
+ * | Pointer 1 | move({duration: 200}) | press() |
490
+ * | Pointer 2 | move({duration: 300}) | press() |
491
+ *
492
+ * In table above, the move for Pointer 1 should only take 200 ms, but the
493
+ * remote end will wait for the move for Pointer 2 to finish
494
+ * (an additional 100 ms) before proceeding to Tick 2.
495
+ *
496
+ * This implicit waiting also applies to pauses. In the table below, even though
497
+ * the keyboard only defines a pause of 100 ms, the remote end will wait an
498
+ * additional 200 ms for the mouse mmove to finish before moving to Tick 2.
499
+ *
500
+ * | Device | Tick 1 | Tick 2 |
501
+ * | -------- | --------------------- | -------------- |
502
+ * | Keyboard | pause(100) | keyDown(SHIFT) |
503
+ * | Mouse | move({duration: 300}) | |
504
+ *
505
+ *
506
+ * #### Bridge Mode
507
+ *
508
+ * As of January 2018, only Firefox natively supports this API. For other
509
+ * browsers, you may either use the
510
+ * {@link ./actions.LegacyActionSequence LegacyActionSequence} class, _or_ you
511
+ * can put the Actions class into bridge mode by passing `{bridge: true}` on
512
+ * creation:
513
+ *
514
+ * const actions = driver.actions({bridge: true});
515
+ * await actions.click(element).sendKeys('abc').perform();
516
+ *
517
+ * In bridge mode, {@link #perform perform()} will first attempt to execute the
518
+ * configured action sequence using the W3C action protocol. If this is rejected
519
+ * by the remote end, the sequence will be translated to and executed against
520
+ * the legacy protocol.
521
+ *
522
+ * Bridge mode __is not enabled by default__ as there are several notable
523
+ * differences between W3C-specified and legacy protocols. Care must be
524
+ * taken to configure your action sequences to account for these differences:
525
+ *
526
+ * 1. For W3C actions, the entire action sequence is executed in a single
527
+ * call to the remote end. For legacy sequences, multiple calls must be
528
+ * made for each step in the sequence. This introduces additional latency
529
+ * which may impact how the browser responds to the emulated user actions.
530
+ *
531
+ * 2. For the legacy actions, {@linkplain #pause pauses} are handled _locally_.
532
+ *
533
+ * 3. For legacy actions, a {@linkplain #keyDown keyDown()} for a
534
+ * _non-modifier key_ **must** be followed by a {@linkplain #keyUP keyUp()}
535
+ * for the same key. This will be handled for you if you use the
536
+ * {@linkplain #sendKeys sendKeys()} method.
537
+ *
538
+ * 4. Mouse movements may not be specified relative to
539
+ * {@linkplain ./input.Origin.VIEWPORT Origin.VIEWPORT}.
540
+ * All movements must be relative to an element or the mouse's current
541
+ * position ({@linkplain ./input.Origin.POINTER Origin.POINTER}).
542
+ * The {@linkplain #move move()} method defaults to viewport relative
543
+ * offsets, so you must always specify an appropriate origin in bridge mode:
544
+ *
545
+ * driver.actions({bridge: true})
546
+ * .move({x: 0, y: 0, origin: Origin.POINTER})
547
+ * .perform();
548
+ * driver.actions({bridge: true})
549
+ * .move({x: 0, y: 0, origin: someWebElement})
550
+ * .perform();
551
+ *
552
+ * 5. The legacy protocol does not support specifying the duration of a
553
+ * {@linkplain #move mouse movement}; any specified duration _is ignored_
554
+ * when translating actions to the legacy protocol.
555
+ *
556
+ * 6. For W3C actions, move offsets relative to a
557
+ * {@linkplain ./webdriver.WebElement WebElement} are interpretted relative
558
+ * to the center of an element's _first_ [client rect] in the viewport. For
559
+ * legacy actions, element offsets are relative to the top-left corner of
560
+ * the element's [bounding client rect]. When translating actions to the
561
+ * legacy protocol in bridge mode, an extra command must be inserted to
562
+ * translate move offsets from one frame of reference to the other. This
563
+ * extra command conributes to the overall latency issue outlined in
564
+ * point 1.
565
+ *
566
+ * [client rect]: https://developer.mozilla.org/en-US/docs/Web/API/Element/getClientRects
567
+ * [bounding client rect]: https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
568
+ *
569
+ * @final
570
+ * @see <https://www.w3.org/TR/webdriver/#actions>
571
+ */
572
+ class Actions {
573
+ /**
574
+ * @param {!Executor} executor The object to execute the configured
575
+ * actions with.
576
+ * @param {{async: (boolean|undefined),
577
+ * bridge: (boolean|undefined)}} options Options for this action
578
+ * sequence (see class description for details).
579
+ */
580
+ constructor(executor, {async = false, bridge = false} = {}) {
581
+ if (async && bridge) {
582
+ throw new InvalidArgumentError(
583
+ 'Async sequences not supported with bridge mode');
584
+ }
585
+
586
+ /** @private @const */
587
+ this.executor_ = executor;
588
+
589
+ /** @private @const */
590
+ this.sync_ = !async;
591
+
592
+ /** @private @const */
593
+ this.bridge_ = !!bridge;
594
+
595
+ /** @private @const */
596
+ this.keyboard_ = new Keyboard('default keyboard');
597
+
598
+ /** @private @const */
599
+ this.mouse_ = new Pointer('default mouse', Pointer.Type.MOUSE);
600
+
601
+ /** @private @const {!Map<!Device, !Array<!Action>>} */
602
+ this.sequences_ = new Map([
603
+ [this.keyboard_, []],
604
+ [this.mouse_, []],
605
+ ]);
606
+ }
607
+
608
+
609
+ /** @return {!Keyboard} the keyboard device handle. */
610
+ keyboard() {
611
+ return this.keyboard_;
612
+ }
613
+
614
+ /** @return {!Pointer} the mouse pointer device handle. */
615
+ mouse() {
616
+ return this.mouse_;
617
+ }
618
+
619
+ /**
620
+ * @param {!Device} device
621
+ * @return {!Array<!Action>}
622
+ * @private
623
+ */
624
+ sequence_(device) {
625
+ let sequence = this.sequences_.get(device);
626
+ if (!sequence) {
627
+ sequence = [];
628
+ this.sequences_.set(device, sequence);
629
+ }
630
+ return sequence;
631
+ }
632
+
633
+ /**
634
+ * Appends `actions` to the end of the current sequence for the given
635
+ * `device`. If device synchronization is enabled, after inserting the
636
+ * actions, pauses will be inserted for all other devices to ensure all action
637
+ * sequences are the same length.
638
+ *
639
+ * @param {!Device} device the device to update.
640
+ * @param {...!Action} actions the actions to insert.
641
+ * @return {!Actions} a self reference.
642
+ */
643
+ insert(device, ...actions) {
644
+ this.sequence_(device).push(...actions);
645
+ return this.sync_ ? this.synchronize() : this;
646
+ }
647
+
648
+ /**
649
+ * Ensures the action sequence for every device referenced in this action
650
+ * sequence is the same length. For devices whose sequence is too short,
651
+ * this will insert {@linkplain #pause pauses} so that every device has an
652
+ * explicit action defined at each tick.
653
+ *
654
+ * @param {...!Device} devices The specific devices to synchronize.
655
+ * If unspecified, the action sequences for every device will be
656
+ * synchronized.
657
+ * @return {!Actions} a self reference.
658
+ */
659
+ synchronize(...devices) {
660
+ let sequences;
661
+ let max = 0;
662
+ if (devices.length === 0) {
663
+ for (const s of this.sequences_.values()) {
664
+ max = Math.max(max, s.length);
665
+ }
666
+ sequences = this.sequences_.values();
667
+ } else {
668
+ sequences = [];
669
+ for (const device of devices) {
670
+ const seq = this.sequence_(device);
671
+ max = Math.max(max, seq.length);
672
+ sequences.push(seq);
673
+ }
674
+ }
675
+
676
+ const pause = {type: Action.Type.PAUSE, duration: 0};
677
+ for (const seq of sequences) {
678
+ while (seq.length < max) {
679
+ seq.push(pause);
680
+ }
681
+ }
682
+
683
+ return this;
684
+ }
685
+
686
+ /**
687
+ * Inserts a pause action for the specified devices, ensuring each device is
688
+ * idle for a tick. The length of the pause (in milliseconds) may be specified
689
+ * as the first parameter to this method (defaults to 0). Otherwise, you may
690
+ * just specify the individual devices that should pause.
691
+ *
692
+ * If no devices are specified, a pause action will be created (using the same
693
+ * duration) for every device.
694
+ *
695
+ * When device synchroniation is enabled (the default for new {@link Actions}
696
+ * objects), there is no need to specify devices as pausing one automatically
697
+ * pauses the others for the same duration. In other words, the following are
698
+ * all equivalent:
699
+ *
700
+ * let a1 = driver.actions();
701
+ * a1.pause(100).perform();
702
+ *
703
+ * let a2 = driver.actions();
704
+ * a2.pause(100, a2.keyboard()).perform();
705
+ * // Synchronization ensures a2.mouse() is automatically paused too.
706
+ *
707
+ * let a3 = driver.actions();
708
+ * a3.pause(100, a3.keyboard(), a3.mouse()).perform();
709
+ *
710
+ * When device synchronization is _disabled_, you can cause individual devices
711
+ * to pause during a tick. For example, to hold the SHIFT key down while
712
+ * moving the mouse:
713
+ *
714
+ * let actions = driver.actions({async: true});
715
+ *
716
+ * actions.keyDown(Key.SHIFT);
717
+ * actions.pause(actions.mouse()) // Pause for shift down
718
+ * .press(Button.LEFT)
719
+ * .move({x: 10, y: 10})
720
+ * .release(Button.LEFT);
721
+ * actions
722
+ * .pause(
723
+ * actions.keyboard(), // Pause for press left
724
+ * actions.keyboard(), // Pause for move
725
+ * actions.keyboard()) // Pause for release left
726
+ * .keyUp(Key.SHIFT);
727
+ * await actions.perform();
728
+ *
729
+ * @param {(number|!Device)=} duration The length of the pause to insert, in
730
+ * milliseconds. Alternatively, the duration may be omitted (yielding a
731
+ * default 0 ms pause), and the first device to pause may be specified.
732
+ * @param {...!Device} devices The devices to insert the pause for. If no
733
+ * devices are specified, the pause will be inserted for _all_ devices.
734
+ * @return {!Actions} a self reference.
735
+ */
736
+ pause(duration, ...devices) {
737
+ if (duration instanceof Device) {
738
+ devices.push(duration);
739
+ duration = 0;
740
+ } else if (!duration) {
741
+ duration = 0;
742
+ }
743
+
744
+ const action = {type: Action.Type.PAUSE, duration};
745
+
746
+ // NB: need a properly typed variable for type checking.
747
+ /** @type {!Iterable<!Device>} */
748
+ const iterable = devices.length === 0 ? this.sequences_.keys() : devices;
749
+ for (const device of iterable) {
750
+ this.sequence_(device).push(action);
751
+ }
752
+ return this.sync_ ? this.synchronize() : this;
753
+ }
754
+
755
+ /**
756
+ * Inserts an action to press a single key.
757
+ *
758
+ * @param {(Key|string|number)} key the key to press. This key may be
759
+ * specified as a {@link Key} value, a specific unicode code point,
760
+ * or a string containing a single unicode code point.
761
+ * @return {!Actions} a self reference.
762
+ */
763
+ keyDown(key) {
764
+ return this.insert(this.keyboard_, this.keyboard_.keyDown(key));
765
+ }
766
+
767
+ /**
768
+ * Inserts an action to release a single key.
769
+ *
770
+ * @param {(Key|string|number)} key the key to release. This key may be
771
+ * specified as a {@link Key} value, a specific unicode code point,
772
+ * or a string containing a single unicode code point.
773
+ * @return {!Actions} a self reference.
774
+ */
775
+ keyUp(key) {
776
+ return this.insert(this.keyboard_, this.keyboard_.keyUp(key));
777
+ }
778
+
779
+ /**
780
+ * Inserts a sequence of actions to type the provided key sequence.
781
+ * For each key, this will record a pair of {@linkplain #keyDown keyDown}
782
+ * and {@linkplain #keyUp keyUp} actions. An implication of this pairing
783
+ * is that modifier keys (e.g. {@link ./input.Key.SHIFT Key.SHIFT}) will
784
+ * always be immediately released. In other words, `sendKeys(Key.SHIFT, 'a')`
785
+ * is the same as typing `sendKeys('a')`, _not_ `sendKeys('A')`.
786
+ *
787
+ * @param {...(Key|string|number)} keys the keys to type.
788
+ * @return {!Actions} a self reference.
789
+ */
790
+ sendKeys(...keys) {
791
+ const actions = [];
792
+ for (const key of keys) {
793
+ if (typeof key === 'string') {
794
+ for (const symbol of key) {
795
+ actions.push(
796
+ this.keyboard_.keyDown(symbol),
797
+ this.keyboard_.keyUp(symbol));
798
+ }
799
+ } else {
800
+ actions.push(
801
+ this.keyboard_.keyDown(key),
802
+ this.keyboard_.keyUp(key));
803
+ }
804
+ }
805
+ return this.insert(this.keyboard_, ...actions);
806
+ }
807
+
808
+ /**
809
+ * Inserts an action to press a mouse button at the mouse's current location.
810
+ *
811
+ * @param {!Button=} button The button to press; defaults to `LEFT`.
812
+ * @return {!Actions} a self reference.
813
+ */
814
+ press(button = Button.LEFT) {
815
+ return this.insert(this.mouse_, this.mouse_.press(button));
816
+ }
817
+
818
+ /**
819
+ * Inserts an action to release a mouse button at the mouse's current
820
+ * location.
821
+ *
822
+ * @param {!Button=} button The button to release; defaults to `LEFT`.
823
+ * @return {!Actions} a self reference.
824
+ */
825
+ release(button = Button.LEFT) {
826
+ return this.insert(this.mouse_, this.mouse_.release(button));
827
+ }
828
+
829
+ /**
830
+ * Inserts an action for moving the mouse `x` and `y` pixels relative to the
831
+ * specified `origin`. The `origin` may be defined as the mouse's
832
+ * {@linkplain ./input.Origin.POINTER current position}, the
833
+ * {@linkplain ./input.Origin.VIEWPORT viewport}, or the center of a specific
834
+ * {@linkplain ./webdriver.WebElement WebElement}.
835
+ *
836
+ * You may adjust how long the remote end should take, in milliseconds, to
837
+ * perform the move using the `duration` parameter (defaults to 100 ms).
838
+ * The number of incremental move events generated over this duration is an
839
+ * implementation detail for the remote end.
840
+ *
841
+ * @param {{
842
+ * x: (number|undefined),
843
+ * y: (number|undefined),
844
+ * duration: (number|undefined),
845
+ * origin: (!Origin|!./webdriver.WebElement|undefined),
846
+ * }=} options The move options. Defaults to moving the mouse to the top-left
847
+ * corner of the viewport over 100ms.
848
+ * @return {!Actions} a self reference.
849
+ */
850
+ move({x = 0, y = 0, duration = 100, origin = Origin.VIEWPORT} = {}) {
851
+ return this.insert(
852
+ this.mouse_, this.mouse_.move({x, y, duration, origin}));
853
+ }
854
+
855
+ /**
856
+ * Short-hand for performing a simple left-click (down/up) with the mouse.
857
+ *
858
+ * @param {./webdriver.WebElement=} element If specified, the mouse will
859
+ * first be moved to the center of the element before performing the
860
+ * click.
861
+ * @return {!Actions} a self reference.
862
+ */
863
+ click(element) {
864
+ if (element) {
865
+ this.move({origin: element});
866
+ }
867
+ return this.press().release();
868
+ }
869
+
870
+ /**
871
+ * Short-hand for performing a simple right-click (down/up) with the mouse.
872
+ *
873
+ * @param {./webdriver.WebElement=} element If specified, the mouse will
874
+ * first be moved to the center of the element before performing the
875
+ * click.
876
+ * @return {!Actions} a self reference.
877
+ */
878
+ contextClick(element) {
879
+ if (element) {
880
+ this.move({origin: element});
881
+ }
882
+ return this.press(Button.RIGHT).release(Button.RIGHT);
883
+ }
884
+
885
+ /**
886
+ * Short-hand for performing a double left-click with the mouse.
887
+ *
888
+ * @param {./webdriver.WebElement=} element If specified, the mouse will
889
+ * first be moved to the center of the element before performing the
890
+ * click.
891
+ * @return {!Actions} a self reference.
892
+ */
893
+ doubleClick(element) {
894
+ return this.click(element).press().release();
895
+ }
896
+
897
+ /**
898
+ * Configures a drag-and-drop action consisting of the following steps:
899
+ *
900
+ * 1. Move to the center of the `from` element (element to be dragged).
901
+ * 2. Press the left mouse button.
902
+ * 3. If the `to` target is a {@linkplain ./webdriver.WebElement WebElement},
903
+ * move the mouse to its center. Otherwise, move the mouse by the
904
+ * specified offset.
905
+ * 4. Release the left mouse button.
906
+ *
907
+ * @param {!./webdriver.WebElement} from The element to press the left mouse
908
+ * button on to start the drag.
909
+ * @param {(!./webdriver.WebElement|{x: number, y: number})} to Either another
910
+ * element to drag to (will drag to the center of the element), or an
911
+ * object specifying the offset to drag by, in pixels.
912
+ * @return {!Actions} a self reference.
913
+ */
914
+ dragAndDrop(from, to) {
915
+ // Do not require up top to avoid a cycle that breaks static analysis.
916
+ const {WebElement} = require('./webdriver');
917
+ if (!(to instanceof WebElement)
918
+ && (!to || typeof to.x !== 'number' || typeof to.y !== 'number')) {
919
+ throw new InvalidArgumentError(
920
+ 'Invalid drag target; must specify a WebElement or {x, y} offset');
921
+ }
922
+
923
+ this.move({origin: from}).press();
924
+ if (to instanceof WebElement) {
925
+ this.move({origin: to});
926
+ } else {
927
+ this.move({x: to.x, y: to.y, origin: Origin.POINTER});
928
+ }
929
+ return this.release();
930
+ }
931
+
932
+ /**
933
+ * Releases all keys, pointers, and clears internal state.
934
+ *
935
+ * @return {!Promise<void>} a promise that will resolve when finished
936
+ * clearing all action state.
937
+ */
938
+ clear() {
939
+ for (const s of this.sequences_.values()) {
940
+ s.length = 0;
941
+ }
942
+ return this.executor_.execute(new Command(Name.CLEAR_ACTIONS));
943
+ }
944
+
945
+ /**
946
+ * Performs the configured action sequence.
947
+ *
948
+ * @return {!Promise<void>} a promise that will resolve when all actions have
949
+ * been completed.
950
+ */
951
+ async perform() {
952
+ const _actions = [];
953
+ this.sequences_.forEach((actions, device) => {
954
+ if (!isIdle(actions)) {
955
+ actions = actions.concat(); // Defensive copy.
956
+ _actions.push(Object.assign({actions}, device.toJSON()));
957
+ }
958
+ });
959
+
960
+ if (_actions.length === 0) {
961
+ return Promise.resolve();
962
+ }
963
+
964
+ try {
965
+ await this.executor_.execute(
966
+ new Command(Name.ACTIONS).setParameter('actions', _actions));
967
+ } catch (ex) {
968
+ if (this.bridge_
969
+ && (ex instanceof UnknownCommandError
970
+ || ex instanceof UnsupportedOperationError)) {
971
+ return executeLegacy(this.executor_, this.sequences_);
972
+ }
973
+ throw ex;
974
+ }
975
+ }
976
+ }
977
+
978
+
979
+ /**
980
+ * @param {!Array<!Action>} actions
981
+ * @return {boolean}
982
+ */
983
+ function isIdle(actions) {
984
+ return actions.length === 0
985
+ || actions.every(a => a.type === Action.Type.PAUSE && !a.duration);
986
+ }
987
+
988
+
989
+ const MODIFIER_KEYS = new Set([Key.ALT, Key.CONTROL, Key.SHIFT, Key.COMMAND]);
990
+
991
+
992
+ /**
993
+ * @param {!Executor} executor
994
+ * @param {!Map<!Device, !Array<!Action>>} sequences
995
+ * @return {!Promise<void>}
996
+ * @suppress {deprecated} Ignore warnings about using LegacyActionSequence.
997
+ */
998
+ async function executeLegacy(executor, sequences) {
999
+ let maxLength = 0;
1000
+ sequences.forEach(seq => maxLength = Math.max(maxLength, seq.length));
1001
+
1002
+ const actions = [];
1003
+ for (let i = 0; i < maxLength; i++) {
1004
+ let next;
1005
+ for (const device of sequences.keys()) {
1006
+ const seq = sequences.get(device);
1007
+
1008
+ if (device instanceof Pointer
1009
+ && device.pointerType_ !== Pointer.Type.MOUSE) {
1010
+ throw new UnsupportedOperationError(
1011
+ `${device.pointerType_} pointer not supported in bridge mode`);
1012
+ }
1013
+
1014
+ const action = seq[i];
1015
+ if (!action || (action.type === Action.Type.PAUSE && !action.duration)) {
1016
+ continue;
1017
+ }
1018
+
1019
+ // - If we've already found an action for this tick:
1020
+ // - If we have two pauses, use the one with a longer duration
1021
+ // - If one is a pause and the other isn't, use the non-pause
1022
+ // - Otherwise, two non-pauses is an error.
1023
+ // - Otherwise, we haven't selected an action yet.
1024
+ if (next) {
1025
+ if (next.type === Action.Type.PAUSE) {
1026
+ if (action.type === Action.Type.PAUSE) {
1027
+ next = next.duration > action.duration ? next : action;
1028
+ } else {
1029
+ next = action;
1030
+ }
1031
+ } else if (action.type !== Action.Type.PAUSE) {
1032
+ throw new UnsupportedOperationError(
1033
+ 'parallel actions not supported in bridge mode');
1034
+ }
1035
+ } else {
1036
+ next = action;
1037
+ }
1038
+
1039
+ if (action.type === Action.Type.KEY_DOWN) {
1040
+ // If this action is a keydown for a non-modifier key, the next action
1041
+ // must be a keyup for the same key, otherwise it cannot be translated
1042
+ // to the legacy action API.
1043
+ if (!MODIFIER_KEYS.has(action.value)) {
1044
+ const nextAction = seq[i + 1];
1045
+ if (!nextAction
1046
+ || nextAction.type !== Action.Type.KEY_UP
1047
+ || nextAction.value !== action.value) {
1048
+ throw new UnsupportedOperationError(
1049
+ `in bridge mode, keydown for <${action.value}> must be followed`
1050
+ + ' by a keyup for the same key');
1051
+ }
1052
+ }
1053
+ } else if (action.type === Action.Type.KEY_UP
1054
+ && !MODIFIER_KEYS.has(action.value)) {
1055
+ next = null;
1056
+ }
1057
+ }
1058
+
1059
+ if (next) {
1060
+ actions.push(next);
1061
+ }
1062
+ }
1063
+
1064
+ for (let i = 0; i < actions.length; i++) {
1065
+ const action = actions[i];
1066
+ switch (action.type) {
1067
+ case Action.Type.PAUSE:
1068
+ await promise.delayed(action.duration || 0);
1069
+ break;
1070
+ case Action.Type.KEY_DOWN:
1071
+ const keys = [action.value];
1072
+ if (!MODIFIER_KEYS.has(action.value)) {
1073
+ function nextIsKeyDown() {
1074
+ const next = actions[i + 1];
1075
+ return !!next
1076
+ && next.type === Action.Type.KEY_DOWN
1077
+ && !MODIFIER_KEYS.has(next.value);
1078
+ }
1079
+ for (; nextIsKeyDown(); i++) {
1080
+ keys.push(actions[i + 1].value);
1081
+ }
1082
+ }
1083
+ await executor.execute(
1084
+ new Command(Name.LEGACY_ACTION_SEND_KEYS)
1085
+ .setParameter('value', keys));
1086
+ break;
1087
+ case Action.Type.KEY_UP:
1088
+ await executor.execute(
1089
+ new Command(Name.LEGACY_ACTION_SEND_KEYS)
1090
+ .setParameter('value', [action.value]));
1091
+ break;
1092
+ case Action.Type.POINTER_DOWN: {
1093
+ function isClick(startAt) {
1094
+ const first = actions[startAt];
1095
+ const second = actions[startAt + 1];
1096
+ return !!first && !!second
1097
+ && first.type === Action.Type.POINTER_DOWN
1098
+ && second.type === Action.Type.POINTER_UP
1099
+ && first.button === second.button;
1100
+ }
1101
+
1102
+ function isDoubleClick(startAt) {
1103
+ return isClick(startAt)
1104
+ && isClick(startAt + 2)
1105
+ && actions[startAt].button === actions[startAt = 2].button;
1106
+ }
1107
+
1108
+ let cmd;
1109
+ if (isDoubleClick(i)) {
1110
+ i += 3; // Consume the pointer up/down/up.
1111
+ cmd = Name.LEGACY_ACTION_DOUBLE_CLICK;
1112
+ } else if (isClick(i)) {
1113
+ i++; // Consume the pointer-up.
1114
+ cmd = Name.LEGACY_ACTION_CLICK;
1115
+ } else {
1116
+ cmd = Name.LEGACY_ACTION_MOUSE_DOWN;
1117
+ }
1118
+ await executor.execute(
1119
+ new Command(cmd).setParameter('button', action.button));
1120
+ break;
1121
+ }
1122
+ case Action.Type.POINTER_UP:
1123
+ await executor.execute(
1124
+ new Command(Name.LEGACY_ACTION_MOUSE_UP)
1125
+ .setParameter('button', action.button));
1126
+ break;
1127
+ case Action.Type.POINTER_MOVE:
1128
+ if (action.origin === Origin.VIEWPORT) {
1129
+ throw new UnsupportedOperationError(
1130
+ `pointer movements relative to ${Origin.VIEWPORT} are not`
1131
+ + ' supported in bridge mode');
1132
+
1133
+ }
1134
+
1135
+ let x = action.x;
1136
+ let y = action.y;
1137
+ const cmd = new Command(Name.LEGACY_ACTION_MOUSE_MOVE);
1138
+ if (action.origin && action.origin !== Origin.POINTER) {
1139
+ const el = /** @type {!./webdriver.WebElement} */(action.origin);
1140
+
1141
+ // Need to translate frame of reference from center of element's first
1142
+ // client rect to the top-left of its bounding client rect. See:
1143
+ // https://w3c.github.io/webdriver/webdriver-spec.html#dfn-center-point
1144
+ let diff = await executor.execute(
1145
+ new Command(Name.EXECUTE_SCRIPT)
1146
+ .setParameter('script', INTERNAL_COMPUTE_OFFSET_SCRIPT)
1147
+ .setParameter('args', [el]));
1148
+ x += diff[0];
1149
+ y += diff[1];
1150
+
1151
+ const id = await el.getId();
1152
+ cmd.setParameter('element', id);
1153
+ }
1154
+ cmd.setParameter('xoffset', x).setParameter('yoffset', y);
1155
+ await executor.execute(cmd);
1156
+ break;
1157
+ default:
1158
+ throw new UnsupportedOperationError(
1159
+ `${action.type} actions not supported in bridge mode`);
1160
+ }
1161
+ }
1162
+ }
1163
+
1164
+
1165
+ /**
1166
+ * Script used to compute the offset from the center of a DOM element's first
1167
+ * client rect from the top-left corner of the element's bounding client rect.
1168
+ * The element's center point is computed using the algorithm defined here:
1169
+ * <https://w3c.github.io/webdriver/webdriver-spec.html#dfn-center-point>.
1170
+ *
1171
+ * __This is only exported for use in internal unit tests. DO NOT USE.__
1172
+ *
1173
+ * @package
1174
+ */
1175
+ const INTERNAL_COMPUTE_OFFSET_SCRIPT = `
1176
+ function computeOffset(el) {
1177
+ var rect = el.getClientRects()[0];
1178
+ var left = Math.max(0, Math.min(rect.x, rect.x + rect.width));
1179
+ var right =
1180
+ Math.min(window.innerWidth, Math.max(rect.x, rect.x + rect.width));
1181
+ var top = Math.max(0, Math.min(rect.y, rect.y + rect.height));
1182
+ var bot =
1183
+ Math.min(window.innerHeight, Math.max(rect.y, rect.y + rect.height));
1184
+ var x = Math.floor(0.5 * (left + right));
1185
+ var y = Math.floor(0.5 * (top + bot));
1186
+
1187
+ var bbox = el.getBoundingClientRect();
1188
+ return [x - bbox.left, y - bbox.top];
1189
+ }
1190
+ return computeOffset(arguments[0]);`;
1191
+
1192
+
164
1193
  // PUBLIC API
165
1194
 
166
1195
 
167
1196
  module.exports = {
168
- Button: Button,
169
- Key: Key,
170
- FileDetector: FileDetector
1197
+ Action, // For documentation only.
1198
+ Actions,
1199
+ Button,
1200
+ Device,
1201
+ Key,
1202
+ Keyboard,
1203
+ FileDetector,
1204
+ Origin,
1205
+ Pointer,
1206
+ INTERNAL_COMPUTE_OFFSET_SCRIPT,
171
1207
  };