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
@@ -1,394 +0,0 @@
1
- // Licensed to the Software Freedom Conservancy (SFC) under one
2
- // or more contributor license agreements. See the NOTICE file
3
- // distributed with this work for additional information
4
- // regarding copyright ownership. The SFC licenses this file
5
- // to you under the Apache License, Version 2.0 (the
6
- // "License"); you may not use this file except in compliance
7
- // with the License. You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing,
12
- // software distributed under the License is distributed on an
13
- // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- // KIND, either express or implied. See the License for the
15
- // specific language governing permissions and limitations
16
- // under the License.
17
-
18
- /**
19
- * @fileoverview Profile management module. This module is considered internal;
20
- * users should use {@link selenium-webdriver/firefox}.
21
- */
22
-
23
- 'use strict';
24
-
25
- const fs = require('fs'),
26
- path = require('path'),
27
- vm = require('vm');
28
-
29
- const isDevMode = require('../lib/devmode'),
30
- Symbols = require('../lib/symbols'),
31
- io = require('../io'),
32
- {Zip, unzip} = require('../io/zip'),
33
- extension = require('./extension');
34
-
35
-
36
- /** @const */
37
- const WEBDRIVER_PREFERENCES_PATH = isDevMode
38
- ? path.join(__dirname, '../../../firefox-driver/webdriver.json')
39
- : path.join(__dirname, '../lib/firefox/webdriver.json');
40
-
41
- /** @type {Object} */
42
- var defaultPreferences = null;
43
-
44
- /**
45
- * Synchronously loads the default preferences used for the FirefoxDriver.
46
- * @return {!Object} The default preferences JSON object.
47
- */
48
- function getDefaultPreferences() {
49
- if (!defaultPreferences) {
50
- var contents = /** @type {string} */(
51
- fs.readFileSync(WEBDRIVER_PREFERENCES_PATH, 'utf8'));
52
- defaultPreferences = /** @type {!Object} */(JSON.parse(contents));
53
- }
54
- return defaultPreferences;
55
- }
56
-
57
-
58
- /**
59
- * Parses a user.js file in a Firefox profile directory.
60
- * @param {string} f Path to the file to parse.
61
- * @return {!Promise<!Object>} A promise for the parsed preferences as
62
- * a JSON object. If the file does not exist, an empty object will be
63
- * returned.
64
- */
65
- function loadUserPrefs(f) {
66
- return io.read(f).then(
67
- function onSuccess(contents) {
68
- var prefs = {};
69
- var context = vm.createContext({
70
- 'user_pref': function(key, value) {
71
- prefs[key] = value;
72
- }
73
- });
74
- vm.runInContext(contents.toString(), context, f);
75
- return prefs;
76
- },
77
- function onError(err) {
78
- if (err && err.code === 'ENOENT') {
79
- return {};
80
- }
81
- throw err;
82
- });
83
- }
84
-
85
-
86
-
87
- /**
88
- * @param {!Object} prefs The default preferences to write. Will be
89
- * overridden by user.js preferences in the template directory and the
90
- * frozen preferences required by WebDriver.
91
- * @param {string} dir Path to the directory write the file to.
92
- * @return {!Promise<string>} A promise for the profile directory,
93
- * to be fulfilled when user preferences have been written.
94
- */
95
- function writeUserPrefs(prefs, dir) {
96
- var userPrefs = path.join(dir, 'user.js');
97
- return loadUserPrefs(userPrefs).then(function(overrides) {
98
- Object.assign(prefs, overrides);
99
- Object.assign(prefs, getDefaultPreferences()['frozen']);
100
-
101
- var contents = Object.keys(prefs).map(function(key) {
102
- return 'user_pref(' + JSON.stringify(key) + ', ' +
103
- JSON.stringify(prefs[key]) + ');';
104
- }).join('\n');
105
-
106
- return new Promise((resolve, reject) => {
107
- fs.writeFile(userPrefs, contents, function(err) {
108
- err && reject(err) || resolve(dir);
109
- });
110
- });
111
- });
112
- };
113
-
114
-
115
- /**
116
- * Installs a group of extensions in the given profile directory. If the
117
- * WebDriver extension is not included in this set, the default version
118
- * bundled with this package will be installed.
119
- * @param {!Array.<string>} extensions The extensions to install, as a
120
- * path to an unpacked extension directory or a path to a xpi file.
121
- * @param {string} dir The profile directory to install to.
122
- * @return {!Promise<string>} A promise for the main profile directory
123
- * once all extensions have been installed.
124
- */
125
- function installExtensions(extensions, dir) {
126
- var next = 0;
127
- var extensionDir = path.join(dir, 'extensions');
128
-
129
- return new Promise(function(fulfill, reject) {
130
- io.mkdir(extensionDir).then(installNext, reject);
131
-
132
- function installNext() {
133
- if (next >= extensions.length) {
134
- fulfill(dir);
135
- } else {
136
- install(extensions[next++]);
137
- }
138
- }
139
-
140
- function install(ext) {
141
- extension.install(ext, extensionDir).then(function(id) {
142
- installNext();
143
- }, reject);
144
- }
145
- });
146
- }
147
-
148
-
149
- /**
150
- * Decodes a base64 encoded profile.
151
- * @param {string} data The base64 encoded string.
152
- * @return {!Promise<string>} A promise for the path to the decoded profile
153
- * directory.
154
- */
155
- function decode(data) {
156
- return io.tmpFile().then(function(file) {
157
- let buf = new Buffer(data, 'base64');
158
- return io.write(file, buf)
159
- .then(io.tmpDir)
160
- .then(dir => unzip(file, dir));
161
- });
162
- }
163
-
164
-
165
-
166
- /**
167
- * Models a Firefox profile directory for use with the FirefoxDriver. The
168
- * {@code Profile} directory uses an in-memory model until
169
- * {@link #writeToDisk} or {@link #encode} is called.
170
- */
171
- class Profile {
172
- /**
173
- * @param {string=} opt_dir Path to an existing Firefox profile directory to
174
- * use a template for this profile. If not specified, a blank profile will
175
- * be used.
176
- */
177
- constructor(opt_dir) {
178
- /** @private {!Object} */
179
- this.preferences_ = {};
180
-
181
- /** @private {boolean} */
182
- this.nativeEventsEnabled_ = true;
183
-
184
- /** @private {(string|undefined)} */
185
- this.template_ = opt_dir;
186
-
187
- /** @private {number} */
188
- this.port_ = 0;
189
-
190
- /** @private {!Array<string>} */
191
- this.extensions_ = [];
192
- }
193
-
194
- /**
195
- * @return {(string|undefined)} Path to an existing Firefox profile directory
196
- * to use as a template when writing this Profile to disk.
197
- */
198
- getTemplateDir() {
199
- return this.template_;
200
- }
201
-
202
- /**
203
- * Registers an extension to be included with this profile.
204
- * @param {string} extension Path to the extension to include, as either an
205
- * unpacked extension directory or the path to a xpi file.
206
- */
207
- addExtension(extension) {
208
- this.extensions_.push(extension);
209
- }
210
-
211
- /**
212
- * @return {!Array<string>} A list of extensions to install in this profile.
213
- */
214
- getExtensions() {
215
- return this.extensions_;
216
- }
217
-
218
- /**
219
- * Sets a desired preference for this profile.
220
- * @param {string} key The preference key.
221
- * @param {(string|number|boolean)} value The preference value.
222
- * @throws {Error} If attempting to set a frozen preference.
223
- */
224
- setPreference(key, value) {
225
- var frozen = getDefaultPreferences()['frozen'];
226
- if (frozen.hasOwnProperty(key) && frozen[key] !== value) {
227
- throw Error('You may not set ' + key + '=' + JSON.stringify(value)
228
- + '; value is frozen for proper WebDriver functionality ('
229
- + key + '=' + JSON.stringify(frozen[key]) + ')');
230
- }
231
- this.preferences_[key] = value;
232
- }
233
-
234
- /**
235
- * Returns the currently configured value of a profile preference. This does
236
- * not include any defaults defined in the profile's template directory user.js
237
- * file (if a template were specified on construction).
238
- * @param {string} key The desired preference.
239
- * @return {(string|number|boolean|undefined)} The current value of the
240
- * requested preference.
241
- */
242
- getPreference(key) {
243
- return this.preferences_[key];
244
- }
245
-
246
- /**
247
- * @return {!Object} A copy of all currently configured preferences.
248
- */
249
- getPreferences() {
250
- return Object.assign({}, this.preferences_);
251
- }
252
-
253
- /**
254
- * Specifies which host the driver should listen for commands on. If not
255
- * specified, the driver will default to "localhost". This option should be
256
- * specified when "localhost" is not mapped to the loopback address
257
- * (127.0.0.1) in `/etc/hosts`.
258
- *
259
- * @param {string} host the host the driver should listen for commands on
260
- */
261
- setHost(host) {
262
- this.preferences_['webdriver_firefox_allowed_hosts'] = host;
263
- }
264
-
265
- /**
266
- * @return {number} The port this profile is currently configured to use, or
267
- * 0 if the port will be selected at random when the profile is written
268
- * to disk.
269
- */
270
- getPort() {
271
- return this.port_;
272
- }
273
-
274
- /**
275
- * Sets the port to use for the WebDriver extension loaded by this profile.
276
- * @param {number} port The desired port, or 0 to use any free port.
277
- */
278
- setPort(port) {
279
- this.port_ = port;
280
- }
281
-
282
- /**
283
- * @return {boolean} Whether the FirefoxDriver is configured to automatically
284
- * accept untrusted SSL certificates.
285
- */
286
- acceptUntrustedCerts() {
287
- return !!this.preferences_['webdriver_accept_untrusted_certs'];
288
- }
289
-
290
- /**
291
- * Sets whether the FirefoxDriver should automatically accept untrusted SSL
292
- * certificates.
293
- * @param {boolean} value .
294
- */
295
- setAcceptUntrustedCerts(value) {
296
- this.preferences_['webdriver_accept_untrusted_certs'] = !!value;
297
- }
298
-
299
- /**
300
- * Sets whether to assume untrusted certificates come from untrusted issuers.
301
- * @param {boolean} value .
302
- */
303
- setAssumeUntrustedCertIssuer(value) {
304
- this.preferences_['webdriver_assume_untrusted_issuer'] = !!value;
305
- }
306
-
307
- /**
308
- * @return {boolean} Whether to assume untrusted certs come from untrusted
309
- * issuers.
310
- */
311
- assumeUntrustedCertIssuer() {
312
- return !!this.preferences_['webdriver_assume_untrusted_issuer'];
313
- }
314
-
315
- /**
316
- * Sets whether to use native events with this profile.
317
- * @param {boolean} enabled .
318
- */
319
- setNativeEventsEnabled(enabled) {
320
- this.nativeEventsEnabled_ = enabled;
321
- }
322
-
323
- /**
324
- * Returns whether native events are enabled in this profile.
325
- * @return {boolean} .
326
- */
327
- nativeEventsEnabled() {
328
- return this.nativeEventsEnabled_;
329
- }
330
-
331
- /**
332
- * Writes this profile to disk.
333
- * @return {!Promise<string>} A promise for the path to the new profile
334
- * directory.
335
- */
336
- writeToDisk() {
337
- var profileDir = io.tmpDir();
338
- if (this.template_) {
339
- profileDir = profileDir.then(function(dir) {
340
- return io.copyDir(
341
- /** @type {string} */(this.template_),
342
- dir, /(parent\.lock|lock|\.parentlock)/);
343
- }.bind(this));
344
- }
345
-
346
- // Freeze preferences for async operations.
347
- var prefs = {};
348
- Object.assign(prefs, getDefaultPreferences()['mutable']);
349
- Object.assign(prefs, getDefaultPreferences()['frozen']);
350
- Object.assign(prefs, this.preferences_);
351
-
352
- // Freeze extensions for async operations.
353
- var extensions = this.extensions_.concat();
354
-
355
- return profileDir.then(function(dir) {
356
- return writeUserPrefs(prefs, dir);
357
- }).then(function(dir) {
358
- return installExtensions(extensions, dir);
359
- });
360
- }
361
-
362
- /**
363
- * Write profile to disk, compress its containing directory, and return
364
- * it as a Base64 encoded string.
365
- *
366
- * @return {!Promise<string>} A promise for the encoded profile as
367
- * Base64 string.
368
- *
369
- */
370
- encode() {
371
- return this.writeToDisk().then(function(dir) {
372
- let zip = new Zip;
373
- return zip.addDir(dir)
374
- .then(() => zip.toBuffer())
375
- .then(buf => buf.toString('base64'));
376
- });
377
- }
378
-
379
- /**
380
- * Encodes this profile as a zipped, base64 encoded directory.
381
- * @return {!Promise<string>} A promise for the encoded profile.
382
- */
383
- [Symbols.serialize]() {
384
- return this.encode();
385
- }
386
- }
387
-
388
-
389
- // PUBLIC API
390
-
391
-
392
- exports.Profile = Profile;
393
- exports.decode = decode;
394
- exports.loadUserPrefs = loadUserPrefs;
package/lib/README DELETED
@@ -1,5 +0,0 @@
1
- This directory contains modules internal to selenium-webdriver that are not
2
- intended for general consumption. They may change at any time.
3
-
4
- All files in this directory and the atoms/ subdirectory may only depend on
5
- built-in JavaScript features and other modules in this directory.
@@ -1,9 +0,0 @@
1
- // GENERATED CODE - DO NOT EDIT
2
- module.exports = function(){return function(){var d=this;function f(a){return"string"==typeof a};function h(a,b){this.code=a;this.a=l[a]||m;this.message=b||"";a=this.a.replace(/((?:^|\s+)[a-z])/g,function(a){return a.toUpperCase().replace(/^[\s\xa0]+/g,"")});b=a.length-5;if(0>b||a.indexOf("Error",b)!=b)a+="Error";this.name=a;a=Error(this.message);a.name=this.name;this.stack=a.stack||""}
3
- (function(){var a=Error;function b(){}b.prototype=a.prototype;h.b=a.prototype;h.prototype=new b;h.prototype.constructor=h;h.a=function(b,c,g){for(var e=Array(arguments.length-2),k=2;k<arguments.length;k++)e[k-2]=arguments[k];return a.prototype[c].apply(b,e)}})();var m="unknown error",l={15:"element not selectable",11:"element not visible"};l[31]=m;l[30]=m;l[24]="invalid cookie domain";l[29]="invalid element coordinates";l[12]="invalid element state";l[32]="invalid selector";l[51]="invalid selector";
4
- l[52]="invalid selector";l[17]="javascript error";l[405]="unsupported operation";l[34]="move target out of bounds";l[27]="no such alert";l[7]="no such element";l[8]="no such frame";l[23]="no such window";l[28]="script timeout";l[33]="session not created";l[10]="stale element reference";l[21]="timeout";l[25]="unable to set cookie";l[26]="unexpected alert open";l[13]=m;l[9]="unknown command";h.prototype.toString=function(){return this.name+": "+this.message};var n;a:{var p=d.navigator;if(p){var q=p.userAgent;if(q){n=q;break a}}n=""}function r(a){return-1!=n.indexOf(a)};function t(a,b){for(var e=a.length,c=f(a)?a.split(""):a,g=0;g<e;g++)g in c&&b.call(void 0,c[g],g,a)};function v(){return r("iPhone")&&!r("iPod")&&!r("iPad")};function w(){return(r("Chrome")||r("CriOS"))&&!r("Edge")};var x=r("Opera"),y=r("Trident")||r("MSIE"),z=r("Edge"),A=r("Gecko")&&!(-1!=n.toLowerCase().indexOf("webkit")&&!r("Edge"))&&!(r("Trident")||r("MSIE"))&&!r("Edge"),aa=-1!=n.toLowerCase().indexOf("webkit")&&!r("Edge");function B(){var a=d.document;return a?a.documentMode:void 0}var C;
5
- a:{var D="",E=function(){var a=n;if(A)return/rv\:([^\);]+)(\)|;)/.exec(a);if(z)return/Edge\/([\d\.]+)/.exec(a);if(y)return/\b(?:MSIE|rv)[: ]([^\);]+)(\)|;)/.exec(a);if(aa)return/WebKit\/(\S+)/.exec(a);if(x)return/(?:Version)[ \/]?(\S+)/.exec(a)}();E&&(D=E?E[1]:"");if(y){var F=B();if(null!=F&&F>parseFloat(D)){C=String(F);break a}}C=D}var G;var H=d.document;G=H&&y?B()||("CSS1Compat"==H.compatMode?parseInt(C,10):5):void 0;var ba=r("Firefox"),ca=v()||r("iPod"),da=r("iPad"),I=r("Android")&&!(w()||r("Firefox")||r("Opera")||r("Silk")),ea=w(),J=r("Safari")&&!(w()||r("Coast")||r("Opera")||r("Edge")||r("Silk")||r("Android"))&&!(v()||r("iPad")||r("iPod"));function K(a){return(a=a.exec(n))?a[1]:""}(function(){if(ba)return K(/Firefox\/([0-9.]+)/);if(y||z||x)return C;if(ea)return v()||r("iPad")||r("iPod")?K(/CriOS\/([0-9.]+)/):K(/Chrome\/([0-9.]+)/);if(J&&!(v()||r("iPad")||r("iPod")))return K(/Version\/([0-9.]+)/);if(ca||da){var a=/Version\/(\S+).*Mobile\/(\S+)/.exec(n);if(a)return a[1]+"."+a[2]}else if(I)return(a=K(/Android\s+([0-9.]+)/))?a:K(/Version\/([0-9.]+)/);return""})();var L,M=function(){if(!A)return!1;var a=d.Components;if(!a)return!1;try{if(!a.classes)return!1}catch(g){return!1}var b=a.classes,a=a.interfaces,e=b["@mozilla.org/xpcom/version-comparator;1"].getService(a.nsIVersionComparator),c=b["@mozilla.org/xre/app-info;1"].getService(a.nsIXULAppInfo).version;L=function(a){e.compare(c,""+a)};return!0}(),N=y&&!(8<=Number(G)),fa=y&&!(9<=Number(G));I&&M&&L(2.3);I&&M&&L(4);J&&M&&L(6);var ga={SCRIPT:1,STYLE:1,HEAD:1,IFRAME:1,OBJECT:1},O={IMG:" ",BR:"\n"};function P(a,b,e){if(!(a.nodeName in ga))if(3==a.nodeType)e?b.push(String(a.nodeValue).replace(/(\r\n|\r|\n)/g,"")):b.push(a.nodeValue);else if(a.nodeName in O)b.push(O[a.nodeName]);else for(a=a.firstChild;a;)P(a,b,e),a=a.nextSibling};function Q(a,b){b=b.toLowerCase();return"style"==b?ha(a.style.cssText):N&&"value"==b&&R(a,"INPUT")?a.value:fa&&!0===a[b]?String(a.getAttribute(b)):(a=a.getAttributeNode(b))&&a.specified?a.value:null}var ia=/[;]+(?=(?:(?:[^"]*"){2})*[^"]*$)(?=(?:(?:[^']*'){2})*[^']*$)(?=(?:[^()]*\([^()]*\))*[^()]*$)/;
6
- function ha(a){var b=[];t(a.split(ia),function(a){var c=a.indexOf(":");0<c&&(a=[a.slice(0,c),a.slice(c+1)],2==a.length&&b.push(a[0].toLowerCase(),":",a[1],";"))});b=b.join("");return b=";"==b.charAt(b.length-1)?b:b+";"}function S(a,b){N&&"value"==b&&R(a,"OPTION")&&null===Q(a,"value")?(b=[],P(a,b,!1),a=b.join("")):a=a[b];return a}function R(a,b){b&&"string"!==typeof b&&(b=b.toString());return!!a&&1==a.nodeType&&(!b||a.tagName.toUpperCase()==b)}
7
- function T(a){return R(a,"OPTION")?!0:R(a,"INPUT")?(a=a.type.toLowerCase(),"checkbox"==a||"radio"==a):!1};var ja={"class":"className",readonly:"readOnly"},U="async autofocus autoplay checked compact complete controls declare defaultchecked defaultselected defer disabled draggable ended formnovalidate hidden indeterminate iscontenteditable ismap itemscope loop multiple muted nohref noresize noshade novalidate nowrap open paused pubdate readonly required reversed scoped seamless seeking selected spellcheck truespeed willvalidate".split(" ");function V(a,b){var e=null,c=b.toLowerCase();if("style"==c)return(e=a.style)&&!f(e)&&(e=e.cssText),e;if(("selected"==c||"checked"==c)&&T(a)){if(!T(a))throw new h(15,"Element is not selectable");b="selected";e=a.type&&a.type.toLowerCase();if("checkbox"==e||"radio"==e)b="checked";return S(a,b)?"true":null}var g=R(a,"A");if(R(a,"IMG")&&"src"==c||g&&"href"==c)return(e=Q(a,c))&&(e=S(a,c)),e;if("spellcheck"==c){e=Q(a,c);if(null!==e){if("false"==e.toLowerCase())return"false";if("true"==e.toLowerCase())return"true"}return S(a,
8
- c)+""}g=ja[b]||b;a:if(f(U))c=f(c)&&1==c.length?U.indexOf(c,0):-1;else{for(var u=0;u<U.length;u++)if(u in U&&U[u]===c){c=u;break a}c=-1}if(0<=c)return(e=null!==Q(a,b)||S(a,g))?"true":null;try{var k=S(a,g)}catch(ka){}(c=null==k)||(c=typeof k,c="object"==c&&null!=k||"function"==c);c?e=Q(a,b):e=k;return null!=e?e.toString():null}var W=["_"],X=d;W[0]in X||!X.execScript||X.execScript("var "+W[0]);
9
- for(var Y;W.length&&(Y=W.shift());){var Z;if(Z=!W.length)Z=void 0!==V;Z?X[Y]=V:X[Y]&&X[Y]!==Object.prototype[Y]?X=X[Y]:X=X[Y]={}};; return this._.apply(null,arguments);}.apply({navigator:typeof window!='undefined'?window.navigator:null,document:typeof window!='undefined'?window.document:null}, arguments);};
package/lib/devmode.js DELETED
@@ -1,34 +0,0 @@
1
- // Licensed to the Software Freedom Conservancy (SFC) under one
2
- // or more contributor license agreements. See the NOTICE file
3
- // distributed with this work for additional information
4
- // regarding copyright ownership. The SFC licenses this file
5
- // to you under the Apache License, Version 2.0 (the
6
- // "License"); you may not use this file except in compliance
7
- // with the License. You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing,
12
- // software distributed under the License is distributed on an
13
- // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- // KIND, either express or implied. See the License for the
15
- // specific language governing permissions and limitations
16
- // under the License.
17
-
18
- /**
19
- * @fileoverview Module used to detect if scripts are loaded from the Selenium
20
- * project repo instead of from a deployed package.
21
- */
22
-
23
- 'use strict';
24
-
25
- const fs = require('fs');
26
- const path = require('path');
27
-
28
- /**
29
- * @const {boolean}
30
- */
31
- module.exports = (function() {
32
- let buildDescFile = path.join(__dirname, '..', '..', 'build.desc');
33
- return fs.existsSync(buildDescFile);
34
- })();
package/lib/events.js DELETED
@@ -1,210 +0,0 @@
1
- // Licensed to the Software Freedom Conservancy (SFC) under one
2
- // or more contributor license agreements. See the NOTICE file
3
- // distributed with this work for additional information
4
- // regarding copyright ownership. The SFC licenses this file
5
- // to you under the Apache License, Version 2.0 (the
6
- // "License"); you may not use this file except in compliance
7
- // with the License. You may obtain a copy of the License at
8
- //
9
- // http://www.apache.org/licenses/LICENSE-2.0
10
- //
11
- // Unless required by applicable law or agreed to in writing,
12
- // software distributed under the License is distributed on an
13
- // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
- // KIND, either express or implied. See the License for the
15
- // specific language governing permissions and limitations
16
- // under the License.
17
-
18
- 'use strict';
19
-
20
- /**
21
- * Describes an event listener registered on an {@linkplain EventEmitter}.
22
- */
23
- class Listener {
24
- /**
25
- * @param {!Function} fn The actual listener function.
26
- * @param {(Object|undefined)} scope The object in whose scope to invoke the
27
- * listener.
28
- * @param {boolean} oneshot Whether this listener should only be used once.
29
- */
30
- constructor(fn, scope, oneshot) {
31
- this.fn = fn;
32
- this.scope = scope;
33
- this.oneshot = oneshot;
34
- }
35
- }
36
-
37
-
38
- /** @type {!WeakMap<!EventEmitter, !Map<string, !Set<!Listener>>>} */
39
- const EVENTS = new WeakMap;
40
-
41
-
42
- /**
43
- * Object that can emit events for others to listen for.
44
- */
45
- class EventEmitter {
46
- /**
47
- * Fires an event and calls all listeners.
48
- * @param {string} type The type of event to emit.
49
- * @param {...*} var_args Any arguments to pass to each listener.
50
- */
51
- emit(type, var_args) {
52
- let events = EVENTS.get(this);
53
- if (!events) {
54
- return;
55
- }
56
-
57
- let args = Array.prototype.slice.call(arguments, 1);
58
-
59
- let listeners = events.get(type);
60
- if (listeners) {
61
- for (let listener of listeners) {
62
- listener.fn.apply(listener.scope, args);
63
- if (listener.oneshot) {
64
- listeners.delete(listener);
65
- }
66
- }
67
- }
68
- }
69
-
70
- /**
71
- * Returns a mutable list of listeners for a specific type of event.
72
- * @param {string} type The type of event to retrieve the listeners for.
73
- * @return {!Set<!Listener>} The registered listeners for the given event
74
- * type.
75
- */
76
- listeners(type) {
77
- let events = EVENTS.get(this);
78
- if (!events) {
79
- events = new Map;
80
- EVENTS.set(this, events);
81
- }
82
-
83
- let listeners = events.get(type);
84
- if (!listeners) {
85
- listeners = new Set;
86
- events.set(type, listeners);
87
- }
88
- return listeners;
89
- }
90
-
91
- /**
92
- * Registers a listener.
93
- * @param {string} type The type of event to listen for.
94
- * @param {!Function} fn The function to invoke when the event is fired.
95
- * @param {Object=} opt_self The object in whose scope to invoke the listener.
96
- * @param {boolean=} opt_oneshot Whether the listener should b (e removed after
97
- * the first event is fired.
98
- * @return {!EventEmitter} A self reference.
99
- * @private
100
- */
101
- addListener_(type, fn, opt_self, opt_oneshot) {
102
- let listeners = this.listeners(type);
103
- for (let listener of listeners) {
104
- if (listener.fn === fn) {
105
- return this;
106
- }
107
- }
108
- listeners.add(new Listener(fn, opt_self || undefined, !!opt_oneshot));
109
- return this;
110
- }
111
-
112
- /**
113
- * Registers a listener.
114
- * @param {string} type The type of event to listen for.
115
- * @param {!Function} fn The function to invoke when the event is fired.
116
- * @param {Object=} opt_self The object in whose scope to invoke the listener.
117
- * @return {!EventEmitter} A self reference.
118
- */
119
- addListener(type, fn, opt_self) {
120
- return this.addListener_(type, fn, opt_self, false);
121
- }
122
-
123
- /**
124
- * Registers a one-time listener which will be called only the first time an
125
- * event is emitted, after which it will be removed.
126
- * @param {string} type The type of event to listen for.
127
- * @param {!Function} fn The function to invoke when the event is fired.
128
- * @param {Object=} opt_self The object in whose scope to invoke the listener.
129
- * @return {!EventEmitter} A self reference.
130
- */
131
- once(type, fn, opt_self) {
132
- return this.addListener_(type, fn, opt_self, true);
133
- }
134
-
135
- /**
136
- * An alias for {@link #addListener() addListener()}.
137
- * @param {string} type The type of event to listen for.
138
- * @param {!Function} fn The function to invoke when the event is fired.
139
- * @param {Object=} opt_self The object in whose scope to invoke the listener.
140
- * @return {!EventEmitter} A self reference.
141
- */
142
- on(type, fn, opt_self) {
143
- return this.addListener(type, fn, opt_self);
144
- }
145
-
146
- /**
147
- * Removes a previously registered event listener.
148
- * @param {string} type The type of event to unregister.
149
- * @param {!Function} listenerFn The handler function to remove.
150
- * @return {!EventEmitter} A self reference.
151
- */
152
- removeListener(type, listenerFn) {
153
- if (typeof type !== 'string' || typeof listenerFn !== 'function') {
154
- throw TypeError('invalid args: expected (string, function), got ('
155
- + (typeof type) + ', ' + (typeof listenerFn) + ')');
156
- }
157
-
158
- let events = EVENTS.get(this);
159
- if (!events) {
160
- return this;
161
- }
162
-
163
- let listeners = events.get(type);
164
- if (!listeners) {
165
- return this;
166
- }
167
-
168
- let match;
169
- for (let listener of listeners) {
170
- if (listener.fn === listenerFn) {
171
- match = listener;
172
- break;
173
- }
174
- }
175
- if (match) {
176
- listeners.delete(match);
177
- if (!listeners.size) {
178
- events.delete(type);
179
- }
180
- }
181
- return this;
182
- }
183
-
184
- /**
185
- * Removes all listeners for a specific type of event. If no event is
186
- * specified, all listeners across all types will be removed.
187
- * @param {string=} opt_type The type of event to remove listeners from.
188
- * @return {!EventEmitter} A self reference.
189
- */
190
- removeAllListeners(opt_type) {
191
- let events = EVENTS.get(this);
192
- if (events) {
193
- if (typeof opt_type === 'string') {
194
- events.delete(opt_type);
195
- } else {
196
- EVENTS.delete(this);
197
- }
198
- }
199
- return this;
200
- }
201
- }
202
-
203
-
204
- // PUBLIC API
205
-
206
-
207
- module.exports = {
208
- EventEmitter: EventEmitter,
209
- Listener: Listener
210
- };