frameSelectors.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.FrameSelectors = void 0;
  6. var _selectorParser = require("../utils/isomorphic/selectorParser");
  7. var _locatorGenerators = require("../utils/isomorphic/locatorGenerators");
  8. /**
  9. * Copyright (c) Microsoft Corporation.
  10. *
  11. * Licensed under the Apache License, Version 2.0 (the "License");
  12. * you may not use this file except in compliance with the License.
  13. * You may obtain a copy of the License at
  14. *
  15. * http://www.apache.org/licenses/LICENSE-2.0
  16. *
  17. * Unless required by applicable law or agreed to in writing, software
  18. * distributed under the License is distributed on an "AS IS" BASIS,
  19. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. * See the License for the specific language governing permissions and
  21. * limitations under the License.
  22. */
  23. class FrameSelectors {
  24. constructor(frame) {
  25. this.frame = void 0;
  26. this.frame = frame;
  27. }
  28. _parseSelector(selector, options) {
  29. const strict = typeof (options === null || options === void 0 ? void 0 : options.strict) === 'boolean' ? options.strict : !!this.frame._page.context()._options.strictSelectors;
  30. return this.frame._page.context().selectors().parseSelector(selector, strict);
  31. }
  32. async query(selector, options, scope) {
  33. const resolved = await this.resolveInjectedForSelector(selector, options, scope);
  34. // Be careful, |this.frame| can be different from |resolved.frame|.
  35. if (!resolved) return null;
  36. const handle = await resolved.injected.evaluateHandle((injected, {
  37. info,
  38. scope
  39. }) => {
  40. return injected.querySelector(info.parsed, scope || document, info.strict);
  41. }, {
  42. info: resolved.info,
  43. scope: resolved.scope
  44. });
  45. const elementHandle = handle.asElement();
  46. if (!elementHandle) {
  47. handle.dispose();
  48. return null;
  49. }
  50. return adoptIfNeeded(elementHandle, await resolved.frame._mainContext());
  51. }
  52. async queryArrayInMainWorld(selector, scope) {
  53. const resolved = await this.resolveInjectedForSelector(selector, {
  54. mainWorld: true
  55. }, scope);
  56. // Be careful, |this.frame| can be different from |resolved.frame|.
  57. if (!resolved) throw new Error(`Failed to find frame for selector "${selector}"`);
  58. return await resolved.injected.evaluateHandle((injected, {
  59. info,
  60. scope
  61. }) => {
  62. return injected.querySelectorAll(info.parsed, scope || document);
  63. }, {
  64. info: resolved.info,
  65. scope: resolved.scope
  66. });
  67. }
  68. async queryCount(selector) {
  69. const resolved = await this.resolveInjectedForSelector(selector);
  70. // Be careful, |this.frame| can be different from |resolved.frame|.
  71. if (!resolved) throw new Error(`Failed to find frame for selector "${selector}"`);
  72. return await resolved.injected.evaluate((injected, {
  73. info
  74. }) => {
  75. return injected.querySelectorAll(info.parsed, document).length;
  76. }, {
  77. info: resolved.info
  78. });
  79. }
  80. async queryAll(selector, scope) {
  81. const resolved = await this.resolveInjectedForSelector(selector, {}, scope);
  82. // Be careful, |this.frame| can be different from |resolved.frame|.
  83. if (!resolved) return [];
  84. const arrayHandle = await resolved.injected.evaluateHandle((injected, {
  85. info,
  86. scope
  87. }) => {
  88. return injected.querySelectorAll(info.parsed, scope || document);
  89. }, {
  90. info: resolved.info,
  91. scope: resolved.scope
  92. });
  93. const properties = await arrayHandle.getProperties();
  94. arrayHandle.dispose();
  95. // Note: adopting elements one by one may be slow. If we encounter the issue here,
  96. // we might introduce 'useMainContext' option or similar to speed things up.
  97. const targetContext = await resolved.frame._mainContext();
  98. const result = [];
  99. for (const property of properties.values()) {
  100. const elementHandle = property.asElement();
  101. if (elementHandle) result.push(adoptIfNeeded(elementHandle, targetContext));else property.dispose();
  102. }
  103. return Promise.all(result);
  104. }
  105. async resolveFrameForSelector(selector, options = {}, scope) {
  106. let frame = this.frame;
  107. const frameChunks = (0, _selectorParser.splitSelectorByFrame)(selector);
  108. for (const chunk of frameChunks) {
  109. (0, _selectorParser.visitAllSelectorParts)(chunk, (part, nested) => {
  110. if (nested && part.name === 'internal:control' && part.body === 'enter-frame') {
  111. const locator = (0, _locatorGenerators.asLocator)(this.frame._page.attribution.playwright.options.sdkLanguage, selector);
  112. throw new _selectorParser.InvalidSelectorError(`Frame locators are not allowed inside composite locators, while querying "${locator}"`);
  113. }
  114. });
  115. }
  116. for (let i = 0; i < frameChunks.length - 1; ++i) {
  117. const info = this._parseSelector(frameChunks[i], options);
  118. const context = await frame._context(info.world);
  119. const injectedScript = await context.injectedScript();
  120. const handle = await injectedScript.evaluateHandle((injected, {
  121. info,
  122. scope,
  123. selectorString
  124. }) => {
  125. const element = injected.querySelector(info.parsed, scope || document, info.strict);
  126. if (element && element.nodeName !== 'IFRAME' && element.nodeName !== 'FRAME') throw injected.createStacklessError(`Selector "${selectorString}" resolved to ${injected.previewNode(element)}, <iframe> was expected`);
  127. return element;
  128. }, {
  129. info,
  130. scope: i === 0 ? scope : undefined,
  131. selectorString: (0, _selectorParser.stringifySelector)(info.parsed)
  132. });
  133. const element = handle.asElement();
  134. if (!element) return null;
  135. const maybeFrame = await frame._page._delegate.getContentFrame(element);
  136. element.dispose();
  137. if (!maybeFrame) return null;
  138. frame = maybeFrame;
  139. }
  140. // If we end up in the different frame, we should start from the frame root, so throw away the scope.
  141. if (frame !== this.frame) scope = undefined;
  142. return {
  143. frame,
  144. info: frame.selectors._parseSelector(frameChunks[frameChunks.length - 1], options),
  145. scope
  146. };
  147. }
  148. async resolveInjectedForSelector(selector, options, scope) {
  149. const resolved = await this.resolveFrameForSelector(selector, options, scope);
  150. // Be careful, |this.frame| can be different from |resolved.frame|.
  151. if (!resolved) return;
  152. const context = await resolved.frame._context(options !== null && options !== void 0 && options.mainWorld ? 'main' : resolved.info.world);
  153. const injected = await context.injectedScript();
  154. return {
  155. injected,
  156. info: resolved.info,
  157. frame: resolved.frame,
  158. scope: resolved.scope
  159. };
  160. }
  161. }
  162. exports.FrameSelectors = FrameSelectors;
  163. async function adoptIfNeeded(handle, context) {
  164. if (handle._context === context) return handle;
  165. const adopted = handle._page._delegate.adoptElementHandle(handle, context);
  166. handle.dispose();
  167. return adopted;
  168. }