elementHandlerDispatcher.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.ElementHandleDispatcher = void 0;
  6. var _dispatcher = require("./dispatcher");
  7. var _jsHandleDispatcher = require("./jsHandleDispatcher");
  8. var _frameDispatcher = require("./frameDispatcher");
  9. var _browserContextDispatcher = require("./browserContextDispatcher");
  10. var _pageDispatcher = require("./pageDispatcher");
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class ElementHandleDispatcher extends _jsHandleDispatcher.JSHandleDispatcher {
  27. static from(scope, handle) {
  28. return (0, _dispatcher.existingDispatcher)(handle) || new ElementHandleDispatcher(scope, handle);
  29. }
  30. static fromNullable(scope, handle) {
  31. if (!handle) return undefined;
  32. return (0, _dispatcher.existingDispatcher)(handle) || new ElementHandleDispatcher(scope, handle);
  33. }
  34. static fromJSHandle(scope, handle) {
  35. const result = (0, _dispatcher.existingDispatcher)(handle);
  36. if (result) return result;
  37. return handle.asElement() ? new ElementHandleDispatcher(scope, handle.asElement()) : new _jsHandleDispatcher.JSHandleDispatcher(scope, handle);
  38. }
  39. constructor(scope, elementHandle) {
  40. super(scope, elementHandle);
  41. this._type_ElementHandle = true;
  42. this._elementHandle = void 0;
  43. this._elementHandle = elementHandle;
  44. }
  45. async ownerFrame(params, metadata) {
  46. const frame = await this._elementHandle.ownerFrame();
  47. return {
  48. frame: frame ? _frameDispatcher.FrameDispatcher.from(this._browserContextDispatcher(), frame) : undefined
  49. };
  50. }
  51. async contentFrame(params, metadata) {
  52. const frame = await this._elementHandle.contentFrame();
  53. return {
  54. frame: frame ? _frameDispatcher.FrameDispatcher.from(this._browserContextDispatcher(), frame) : undefined
  55. };
  56. }
  57. async getAttribute(params, metadata) {
  58. const value = await this._elementHandle.getAttribute(metadata, params.name);
  59. return {
  60. value: value === null ? undefined : value
  61. };
  62. }
  63. async inputValue(params, metadata) {
  64. const value = await this._elementHandle.inputValue(metadata);
  65. return {
  66. value
  67. };
  68. }
  69. async textContent(params, metadata) {
  70. const value = await this._elementHandle.textContent(metadata);
  71. return {
  72. value: value === null ? undefined : value
  73. };
  74. }
  75. async innerText(params, metadata) {
  76. return {
  77. value: await this._elementHandle.innerText(metadata)
  78. };
  79. }
  80. async innerHTML(params, metadata) {
  81. return {
  82. value: await this._elementHandle.innerHTML(metadata)
  83. };
  84. }
  85. async isChecked(params, metadata) {
  86. return {
  87. value: await this._elementHandle.isChecked(metadata)
  88. };
  89. }
  90. async isDisabled(params, metadata) {
  91. return {
  92. value: await this._elementHandle.isDisabled(metadata)
  93. };
  94. }
  95. async isEditable(params, metadata) {
  96. return {
  97. value: await this._elementHandle.isEditable(metadata)
  98. };
  99. }
  100. async isEnabled(params, metadata) {
  101. return {
  102. value: await this._elementHandle.isEnabled(metadata)
  103. };
  104. }
  105. async isHidden(params, metadata) {
  106. return {
  107. value: await this._elementHandle.isHidden(metadata)
  108. };
  109. }
  110. async isVisible(params, metadata) {
  111. return {
  112. value: await this._elementHandle.isVisible(metadata)
  113. };
  114. }
  115. async dispatchEvent(params, metadata) {
  116. await this._elementHandle.dispatchEvent(metadata, params.type, (0, _jsHandleDispatcher.parseArgument)(params.eventInit));
  117. }
  118. async scrollIntoViewIfNeeded(params, metadata) {
  119. await this._elementHandle.scrollIntoViewIfNeeded(metadata, params);
  120. }
  121. async hover(params, metadata) {
  122. return await this._elementHandle.hover(metadata, params);
  123. }
  124. async click(params, metadata) {
  125. return await this._elementHandle.click(metadata, params);
  126. }
  127. async dblclick(params, metadata) {
  128. return await this._elementHandle.dblclick(metadata, params);
  129. }
  130. async tap(params, metadata) {
  131. return await this._elementHandle.tap(metadata, params);
  132. }
  133. async selectOption(params, metadata) {
  134. const elements = (params.elements || []).map(e => e._elementHandle);
  135. return {
  136. values: await this._elementHandle.selectOption(metadata, elements, params.options || [], params)
  137. };
  138. }
  139. async fill(params, metadata) {
  140. return await this._elementHandle.fill(metadata, params.value, params);
  141. }
  142. async selectText(params, metadata) {
  143. await this._elementHandle.selectText(metadata, params);
  144. }
  145. async setInputFiles(params, metadata) {
  146. return await this._elementHandle.setInputFiles(metadata, params);
  147. }
  148. async focus(params, metadata) {
  149. await this._elementHandle.focus(metadata);
  150. }
  151. async type(params, metadata) {
  152. return await this._elementHandle.type(metadata, params.text, params);
  153. }
  154. async press(params, metadata) {
  155. return await this._elementHandle.press(metadata, params.key, params);
  156. }
  157. async check(params, metadata) {
  158. return await this._elementHandle.check(metadata, params);
  159. }
  160. async uncheck(params, metadata) {
  161. return await this._elementHandle.uncheck(metadata, params);
  162. }
  163. async boundingBox(params, metadata) {
  164. const value = await this._elementHandle.boundingBox();
  165. return {
  166. value: value || undefined
  167. };
  168. }
  169. async screenshot(params, metadata) {
  170. const mask = (params.mask || []).map(({
  171. frame,
  172. selector
  173. }) => ({
  174. frame: frame._object,
  175. selector
  176. }));
  177. return {
  178. binary: await this._elementHandle.screenshot(metadata, {
  179. ...params,
  180. mask
  181. })
  182. };
  183. }
  184. async querySelector(params, metadata) {
  185. const handle = await this._elementHandle.querySelector(params.selector, params);
  186. return {
  187. element: ElementHandleDispatcher.fromNullable(this.parentScope(), handle)
  188. };
  189. }
  190. async querySelectorAll(params, metadata) {
  191. const elements = await this._elementHandle.querySelectorAll(params.selector);
  192. return {
  193. elements: elements.map(e => ElementHandleDispatcher.from(this.parentScope(), e))
  194. };
  195. }
  196. async evalOnSelector(params, metadata) {
  197. return {
  198. value: (0, _jsHandleDispatcher.serializeResult)(await this._elementHandle.evalOnSelector(params.selector, !!params.strict, params.expression, params.isFunction, (0, _jsHandleDispatcher.parseArgument)(params.arg)))
  199. };
  200. }
  201. async evalOnSelectorAll(params, metadata) {
  202. return {
  203. value: (0, _jsHandleDispatcher.serializeResult)(await this._elementHandle.evalOnSelectorAll(params.selector, params.expression, params.isFunction, (0, _jsHandleDispatcher.parseArgument)(params.arg)))
  204. };
  205. }
  206. async waitForElementState(params, metadata) {
  207. await this._elementHandle.waitForElementState(metadata, params.state, params);
  208. }
  209. async waitForSelector(params, metadata) {
  210. return {
  211. element: ElementHandleDispatcher.fromNullable(this.parentScope(), await this._elementHandle.waitForSelector(metadata, params.selector, params))
  212. };
  213. }
  214. _browserContextDispatcher() {
  215. const scope = this.parentScope();
  216. if (scope instanceof _browserContextDispatcher.BrowserContextDispatcher) return scope;
  217. if (scope instanceof _pageDispatcher.PageDispatcher) return scope.parentScope();
  218. if (scope instanceof _pageDispatcher.WorkerDispatcher || scope instanceof _frameDispatcher.FrameDispatcher) {
  219. const parentScope = scope.parentScope();
  220. if (parentScope instanceof _browserContextDispatcher.BrowserContextDispatcher) return parentScope;
  221. return parentScope.parentScope();
  222. }
  223. throw new Error('ElementHandle belongs to unexpected scope');
  224. }
  225. }
  226. exports.ElementHandleDispatcher = ElementHandleDispatcher;