frameDispatcher.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FrameDispatcher = void 0;
  6. var _frames = require("../frames");
  7. var _dispatcher = require("./dispatcher");
  8. var _elementHandlerDispatcher = require("./elementHandlerDispatcher");
  9. var _jsHandleDispatcher = require("./jsHandleDispatcher");
  10. var _networkDispatchers = require("./networkDispatchers");
  11. var _utils = require("../../utils");
  12. /**
  13. * Copyright (c) Microsoft Corporation.
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the "License");
  16. * you may not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * http://www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an "AS IS" BASIS,
  23. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. class FrameDispatcher extends _dispatcher.Dispatcher {
  28. static from(scope, frame) {
  29. const result = (0, _dispatcher.existingDispatcher)(frame);
  30. return result || new FrameDispatcher(scope, frame);
  31. }
  32. static fromNullable(scope, frame) {
  33. if (!frame) return;
  34. return FrameDispatcher.from(scope, frame);
  35. }
  36. constructor(scope, frame) {
  37. // Main frames are gc'ed separately from any other frames, so that
  38. // methods on Page that redirect to the main frame remain operational.
  39. // Note: we cannot check parentFrame() here because it may be null after the frame has been detached.
  40. (0, _utils.debugAssert)(frame._page.mainFrame(), 'Cannot determine whether the frame is a main frame');
  41. const gcBucket = frame._page.mainFrame() === frame ? 'MainFrame' : 'Frame';
  42. const pageDispatcher = (0, _dispatcher.existingDispatcher)(frame._page);
  43. super(pageDispatcher || scope, frame, 'Frame', {
  44. url: frame.url(),
  45. name: frame.name(),
  46. parentFrame: FrameDispatcher.fromNullable(scope, frame.parentFrame()),
  47. loadStates: Array.from(frame._firedLifecycleEvents)
  48. }, gcBucket);
  49. this._type_Frame = true;
  50. this._frame = void 0;
  51. this._browserContextDispatcher = void 0;
  52. this._browserContextDispatcher = scope;
  53. this._frame = frame;
  54. this.addObjectListener(_frames.Frame.Events.AddLifecycle, lifecycleEvent => {
  55. this._dispatchEvent('loadstate', {
  56. add: lifecycleEvent
  57. });
  58. });
  59. this.addObjectListener(_frames.Frame.Events.RemoveLifecycle, lifecycleEvent => {
  60. this._dispatchEvent('loadstate', {
  61. remove: lifecycleEvent
  62. });
  63. });
  64. this.addObjectListener(_frames.Frame.Events.InternalNavigation, event => {
  65. if (!event.isPublic) return;
  66. const params = {
  67. url: event.url,
  68. name: event.name,
  69. error: event.error ? event.error.message : undefined
  70. };
  71. if (event.newDocument) params.newDocument = {
  72. request: _networkDispatchers.RequestDispatcher.fromNullable(this._browserContextDispatcher, event.newDocument.request || null)
  73. };
  74. this._dispatchEvent('navigated', params);
  75. });
  76. }
  77. async goto(params, metadata) {
  78. return {
  79. response: _networkDispatchers.ResponseDispatcher.fromNullable(this._browserContextDispatcher, await this._frame.goto(metadata, params.url, params))
  80. };
  81. }
  82. async frameElement() {
  83. return {
  84. element: _elementHandlerDispatcher.ElementHandleDispatcher.from(this, await this._frame.frameElement())
  85. };
  86. }
  87. async evaluateExpression(params, metadata) {
  88. return {
  89. value: (0, _jsHandleDispatcher.serializeResult)(await this._frame.evaluateExpression(params.expression, {
  90. isFunction: params.isFunction,
  91. exposeUtilityScript: params.exposeUtilityScript
  92. }, (0, _jsHandleDispatcher.parseArgument)(params.arg)))
  93. };
  94. }
  95. async evaluateExpressionHandle(params, metadata) {
  96. return {
  97. handle: _elementHandlerDispatcher.ElementHandleDispatcher.fromJSHandle(this, await this._frame.evaluateExpressionHandle(params.expression, {
  98. isFunction: params.isFunction
  99. }, (0, _jsHandleDispatcher.parseArgument)(params.arg)))
  100. };
  101. }
  102. async waitForSelector(params, metadata) {
  103. return {
  104. element: _elementHandlerDispatcher.ElementHandleDispatcher.fromNullable(this, await this._frame.waitForSelector(metadata, params.selector, params))
  105. };
  106. }
  107. async dispatchEvent(params, metadata) {
  108. return this._frame.dispatchEvent(metadata, params.selector, params.type, (0, _jsHandleDispatcher.parseArgument)(params.eventInit), params);
  109. }
  110. async evalOnSelector(params, metadata) {
  111. return {
  112. value: (0, _jsHandleDispatcher.serializeResult)(await this._frame.evalOnSelector(params.selector, !!params.strict, params.expression, params.isFunction, (0, _jsHandleDispatcher.parseArgument)(params.arg)))
  113. };
  114. }
  115. async evalOnSelectorAll(params, metadata) {
  116. return {
  117. value: (0, _jsHandleDispatcher.serializeResult)(await this._frame.evalOnSelectorAll(params.selector, params.expression, params.isFunction, (0, _jsHandleDispatcher.parseArgument)(params.arg)))
  118. };
  119. }
  120. async querySelector(params, metadata) {
  121. return {
  122. element: _elementHandlerDispatcher.ElementHandleDispatcher.fromNullable(this, await this._frame.querySelector(params.selector, params))
  123. };
  124. }
  125. async querySelectorAll(params, metadata) {
  126. const elements = await this._frame.querySelectorAll(params.selector);
  127. return {
  128. elements: elements.map(e => _elementHandlerDispatcher.ElementHandleDispatcher.from(this, e))
  129. };
  130. }
  131. async queryCount(params) {
  132. return {
  133. value: await this._frame.queryCount(params.selector)
  134. };
  135. }
  136. async content() {
  137. return {
  138. value: await this._frame.content()
  139. };
  140. }
  141. async setContent(params, metadata) {
  142. return await this._frame.setContent(metadata, params.html, params);
  143. }
  144. async addScriptTag(params, metadata) {
  145. return {
  146. element: _elementHandlerDispatcher.ElementHandleDispatcher.from(this, await this._frame.addScriptTag(params))
  147. };
  148. }
  149. async addStyleTag(params, metadata) {
  150. return {
  151. element: _elementHandlerDispatcher.ElementHandleDispatcher.from(this, await this._frame.addStyleTag(params))
  152. };
  153. }
  154. async click(params, metadata) {
  155. metadata.potentiallyClosesScope = true;
  156. return await this._frame.click(metadata, params.selector, params);
  157. }
  158. async dblclick(params, metadata) {
  159. return await this._frame.dblclick(metadata, params.selector, params);
  160. }
  161. async dragAndDrop(params, metadata) {
  162. return await this._frame.dragAndDrop(metadata, params.source, params.target, params);
  163. }
  164. async tap(params, metadata) {
  165. return await this._frame.tap(metadata, params.selector, params);
  166. }
  167. async fill(params, metadata) {
  168. return await this._frame.fill(metadata, params.selector, params.value, params);
  169. }
  170. async focus(params, metadata) {
  171. await this._frame.focus(metadata, params.selector, params);
  172. }
  173. async blur(params, metadata) {
  174. await this._frame.blur(metadata, params.selector, params);
  175. }
  176. async textContent(params, metadata) {
  177. const value = await this._frame.textContent(metadata, params.selector, params);
  178. return {
  179. value: value === null ? undefined : value
  180. };
  181. }
  182. async innerText(params, metadata) {
  183. return {
  184. value: await this._frame.innerText(metadata, params.selector, params)
  185. };
  186. }
  187. async innerHTML(params, metadata) {
  188. return {
  189. value: await this._frame.innerHTML(metadata, params.selector, params)
  190. };
  191. }
  192. async getAttribute(params, metadata) {
  193. const value = await this._frame.getAttribute(metadata, params.selector, params.name, params);
  194. return {
  195. value: value === null ? undefined : value
  196. };
  197. }
  198. async inputValue(params, metadata) {
  199. const value = await this._frame.inputValue(metadata, params.selector, params);
  200. return {
  201. value
  202. };
  203. }
  204. async isChecked(params, metadata) {
  205. return {
  206. value: await this._frame.isChecked(metadata, params.selector, params)
  207. };
  208. }
  209. async isDisabled(params, metadata) {
  210. return {
  211. value: await this._frame.isDisabled(metadata, params.selector, params)
  212. };
  213. }
  214. async isEditable(params, metadata) {
  215. return {
  216. value: await this._frame.isEditable(metadata, params.selector, params)
  217. };
  218. }
  219. async isEnabled(params, metadata) {
  220. return {
  221. value: await this._frame.isEnabled(metadata, params.selector, params)
  222. };
  223. }
  224. async isHidden(params, metadata) {
  225. return {
  226. value: await this._frame.isHidden(metadata, params.selector, params)
  227. };
  228. }
  229. async isVisible(params, metadata) {
  230. return {
  231. value: await this._frame.isVisible(metadata, params.selector, params)
  232. };
  233. }
  234. async hover(params, metadata) {
  235. return await this._frame.hover(metadata, params.selector, params);
  236. }
  237. async selectOption(params, metadata) {
  238. const elements = (params.elements || []).map(e => e._elementHandle);
  239. return {
  240. values: await this._frame.selectOption(metadata, params.selector, elements, params.options || [], params)
  241. };
  242. }
  243. async setInputFiles(params, metadata) {
  244. return await this._frame.setInputFiles(metadata, params.selector, params);
  245. }
  246. async type(params, metadata) {
  247. return await this._frame.type(metadata, params.selector, params.text, params);
  248. }
  249. async press(params, metadata) {
  250. return await this._frame.press(metadata, params.selector, params.key, params);
  251. }
  252. async check(params, metadata) {
  253. return await this._frame.check(metadata, params.selector, params);
  254. }
  255. async uncheck(params, metadata) {
  256. return await this._frame.uncheck(metadata, params.selector, params);
  257. }
  258. async waitForTimeout(params, metadata) {
  259. return await this._frame.waitForTimeout(metadata, params.timeout);
  260. }
  261. async waitForFunction(params, metadata) {
  262. return {
  263. handle: _elementHandlerDispatcher.ElementHandleDispatcher.fromJSHandle(this, await this._frame._waitForFunctionExpression(metadata, params.expression, params.isFunction, (0, _jsHandleDispatcher.parseArgument)(params.arg), params))
  264. };
  265. }
  266. async title(params, metadata) {
  267. return {
  268. value: await this._frame.title()
  269. };
  270. }
  271. async highlight(params, metadata) {
  272. return await this._frame.highlight(params.selector);
  273. }
  274. async expect(params, metadata) {
  275. metadata.potentiallyClosesScope = true;
  276. const expectedValue = params.expectedValue ? (0, _jsHandleDispatcher.parseArgument)(params.expectedValue) : undefined;
  277. const result = await this._frame.expect(metadata, params.selector, {
  278. ...params,
  279. expectedValue
  280. });
  281. if (result.received !== undefined) result.received = (0, _jsHandleDispatcher.serializeResult)(result.received);
  282. if (result.matches === params.isNot) metadata.error = {
  283. error: {
  284. name: 'Expect',
  285. message: 'Expect failed'
  286. }
  287. };
  288. return result;
  289. }
  290. }
  291. exports.FrameDispatcher = FrameDispatcher;