puppeteer-declarations.d.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /// <reference types="node" />
  2. import type { EventInitDict, EventSpy, ScreenshotDiff, ScreenshotOptions } from '@stencil/core/internal';
  3. import type { ClickOptions, HTTPResponse as PuppeteerHTTPResponse, Page, ScreenshotOptions as PuppeteerScreenshotOptions, WaitForOptions } from 'puppeteer';
  4. /**
  5. * This type helps with declaration merging as a part of Stencil's migration from Puppeteer v5.4.3 to v10.0.0. In
  6. * v5.4.3, `HttpResponse` was an interface whereas v10.0.0 declares it as a class. It is redeclared here to help teams
  7. * migrate to a newer minor version of Stencil without requiring a Puppeteer upgrade/major version of Stencil. This type
  8. * should be removed as a part of the Stencil 3.0 release.
  9. */
  10. export declare type HTTPResponse = PuppeteerHTTPResponse;
  11. /**
  12. * These types help with declaration merging as a part of Stencil's migration from Puppeteer v5.4.3 to v10.0.0. In
  13. * v10.0.0, `WaitForOptions` is a renamed version of `NavigationOptions` from v5.4.3, who has had its type hierarchy
  14. * flattened.
  15. *
  16. * See {@link https://github.com/DefinitelyTyped/DefinitelyTyped/blob/8290e943f6b398acf39ee1b2e486824144e15bc8/types/puppeteer/index.d.ts#L605-L622}
  17. * for the v5.4.3 types.
  18. *
  19. * These types are redeclared here to help teams migrate to a newer minor version of Stencil without requiring a
  20. * Puppeteer upgrade/major version of Stencil. These type additions should be removed as a part of the Stencil 3.0
  21. * release.
  22. */
  23. declare module 'puppeteer' {
  24. type LifeCycleEvent = 'load' | 'domcontentloaded' | 'networkidle0' | 'networkidle2';
  25. interface WaitForOptions {
  26. timeout?: number;
  27. waitUntil?: LifeCycleEvent | LifeCycleEvent[];
  28. }
  29. }
  30. /**
  31. * This type was once exported by Puppeteer, but has since moved to an object literal in (Puppeteer’s) native types.
  32. * Re-create it here as a named type to use across multiple Stencil-related testing files.
  33. */
  34. export declare type PageCloseOptions = {
  35. runBeforeUnload?: boolean;
  36. };
  37. export interface NewE2EPageOptions extends WaitForOptions {
  38. url?: string;
  39. html?: string;
  40. failOnConsoleError?: boolean;
  41. failOnNetworkError?: boolean;
  42. }
  43. declare type Omit<T, K> = Pick<T, Exclude<keyof T, K>>;
  44. declare type PuppeteerPage = Omit<Page, 'bringToFront' | 'browser' | 'screenshot' | 'emulate' | 'emulateMedia' | 'frames' | 'goBack' | 'goForward' | 'isClosed' | 'mainFrame' | 'pdf' | 'reload' | 'target' | 'title' | 'viewport' | 'waitForNavigation' | 'screenshot' | 'workers' | 'addListener' | 'prependListener' | 'prependOnceListener' | 'removeAllListeners' | 'setMaxListeners' | 'getMaxListeners' | 'listeners' | 'rawListeners' | 'emit' | 'eventNames' | 'listenerCount' | '$x' | 'waitForXPath'>;
  45. export interface PageDiagnostic {
  46. type: 'error' | 'pageerror' | 'requestfailed';
  47. message?: string;
  48. location?: string;
  49. }
  50. /**
  51. * The E2EPage is a wrapper utility to Puppeteer in order to
  52. * to create easier to write and read end-to-end tests.
  53. */
  54. export interface E2EPage extends PuppeteerPage {
  55. /**
  56. * `Experimental`
  57. * Takes a screenshot of the page, then compares the current screenshot
  58. * against the master screenshot. The returned screenshot compare
  59. * results can then be used to test pixel mismatches, such as
  60. * `expect(results).toMatchScreenshot()`.
  61. */
  62. compareScreenshot(): Promise<ScreenshotDiff>;
  63. /**
  64. * `Experimental`
  65. * Takes a screenshot of the page, then compares the current screenshot
  66. * against the master screenshot. The provided `description` will be
  67. * added onto its current description, which comes from the test description.
  68. */
  69. compareScreenshot(description: string): Promise<ScreenshotDiff>;
  70. /**
  71. * `Experimental`
  72. * Takes a screenshot of the page, then compares the current screenshot
  73. * against the master screenshot. The `opts` argument can be used to
  74. * customize screenshot options.
  75. */
  76. compareScreenshot(opts: ScreenshotOptions): Promise<ScreenshotDiff>;
  77. /**
  78. * `Experimental`
  79. * Takes a screenshot of the page, then compares the current screenshot
  80. * against the master screenshot. The `description` argument will be
  81. * added onto its current description, which comes from the test description.
  82. * The `opts` argument can be used to customize screenshot options.
  83. */
  84. compareScreenshot(description: string, opts: ScreenshotOptions): Promise<ScreenshotDiff>;
  85. /**
  86. * Sets a debugger;
  87. */
  88. debugger(): Promise<void>;
  89. /**
  90. * Find an element that matches the selector, which is the same as
  91. * `document.querySelector(selector)`. Use `>>>` within the
  92. * selector to find an element within the host element's shadow root.
  93. * For example, to select the first `div` inside of the component
  94. * `my-cmp`, the call would be `page.find('my-cmp >>> div')`.
  95. * Returns `null` if an element was not found.
  96. */
  97. find(selector: FindSelector): Promise<E2EElement>;
  98. /**
  99. * Find all elements that match the selector, which is the same as
  100. * `document.querySelectorAll(selector)`. Use `>>>` within the
  101. * selector to find elements within the host element's shadow root.
  102. * For example, to select all of the `li` elements inside of the component
  103. * `my-cmp`, the call would be `page.findAll('my-cmp >>> li')`.
  104. * Returns an empty array if no elements were found.
  105. */
  106. findAll(selector: string): Promise<E2EElement[]>;
  107. /**
  108. * During an end-to-end test, a dev-server is started so `page.goto(url)` can be used
  109. * on the app being tested. Urls are always relative since the dev server provides
  110. * a localhost address. A shortcut to `page.goto(url)` is to set the `url` option
  111. * when creating a new page, such as `const page = await newE2EPage({ url })`.
  112. */
  113. goTo(url: string, options?: WaitForOptions): Promise<HTTPResponse | null>;
  114. /**
  115. * Instead of testing a url directly, html content can be mocked using
  116. * `page.setContent(html)`. A shortcut to `page.setContent(html)` is to set
  117. * the `html` option when creating a new page, such as
  118. * `const page = await newE2EPage({ html })`.
  119. */
  120. setContent(html: string, options?: WaitForOptions): Promise<void>;
  121. /**
  122. * Used to test if an event was, or was not dispatched. This method
  123. * returns a promise, that resolves with an EventSpy. The EventSpy
  124. * can be used along with `expect(spy).toHaveReceivedEvent()`,
  125. * `expect(spy).toHaveReceivedEventTimes(x)` and
  126. * `expect(spy).toHaveReceivedEventDetail({...})`.
  127. */
  128. spyOnEvent(eventName: string, selector?: 'window' | 'document'): Promise<EventSpy>;
  129. /**
  130. * Both Stencil and Puppeteer have an asynchronous architecture, which is a good thing
  131. * for performance. Since all calls are async, it's required that
  132. * `await page.waitForChanges()` is called when changes are made to components.
  133. * An error will be thrown if changes were made to a component but `waitForChanges()`
  134. * was not called.
  135. */
  136. waitForChanges(): Promise<void>;
  137. /**
  138. * Waits for the event to be received on `window`. The optional second argument
  139. * allows the listener to be set to `document` if needed.
  140. */
  141. waitForEvent(eventName: string): Promise<any>;
  142. getDiagnostics(): PageDiagnostic[];
  143. }
  144. export interface E2EPageInternal extends E2EPage {
  145. isClosed(): boolean;
  146. _e2eElements: E2EElementInternal[];
  147. _e2eEvents: Map<number, WaitForEvent>;
  148. _e2eEventIds: number;
  149. _e2eGoto(url: string, options?: Partial<WaitForOptions>): Promise<HTTPResponse | null>;
  150. _e2eClose(options?: PageCloseOptions): Promise<void>;
  151. screenshot(options?: PuppeteerScreenshotOptions): Promise<Buffer>;
  152. }
  153. export interface E2EElement {
  154. /**
  155. * Used to call a method on a component. For example, if a component
  156. * has the method `cmp.myMethod(arg1, arg2)`, calling this method
  157. * from a e2e test could be `cmp.callMethod('myMethod', arg1, arg2)`.
  158. */
  159. callMethod(methodName: string, ...methodArgs: any[]): Promise<any>;
  160. /**
  161. * Gets and sets the value of the class attribute of the e2e element.
  162. * Note that `await page.waitForChanges()` must be called before reading
  163. * the value if content has changed.
  164. */
  165. className: string;
  166. /**
  167. * Using classList is a convenient alternative to accessing an element's list
  168. * of classes as a space-delimited string via `element.className`.
  169. */
  170. classList: {
  171. /**
  172. * Add specified class values. If these classes already exist in
  173. * attribute of the element, then they are ignored.
  174. */
  175. add: (...tokens: string[]) => void;
  176. /**
  177. * Remove specified class values. Note: Removing a class that does
  178. * not exist does NOT throw an error.
  179. */
  180. remove: (...tokens: string[]) => void;
  181. /**
  182. * If class exists then remove it, if not, then add it.
  183. */
  184. toggle: (token: string) => void;
  185. /**
  186. * Checks if specified class value exists in class attribute of the element.
  187. */
  188. contains: (className: string) => boolean;
  189. };
  190. /**
  191. * Calling `click()` on an element scrolls it into view if needed, and
  192. * then uses `page.mouse` to click in the center of the element.
  193. * Please see the puppeteer docs for more information.
  194. */
  195. click(options?: ClickOptions): Promise<void>;
  196. /**
  197. * Find a child element that matches the selector, which is the same as
  198. * `element.querySelector(selector)`. Use `>>>` within the
  199. * selector to find an element within a host element's shadow root.
  200. * For example, to select the first `div` inside of the component
  201. * `my-cmp`, which is a child of this element, the call would be
  202. * `element.find('my-cmp >>> div')`. Returns `null` if no
  203. * elements were found.
  204. */
  205. find(selector: FindSelector): Promise<E2EElement>;
  206. /**
  207. * Find all child elements that match the selector, which is the same as
  208. * `element.querySelectorAll(selector)`. Use `>>>` within the
  209. * selector to find elements within a host element's shadow root.
  210. * For example, to select all `li` elements inside of the component
  211. * `my-cmp`, which is a child of this element, the call would be
  212. * `element.findAll('my-cmp >>> li')`. Returns an empty array if
  213. * no elements were found.
  214. */
  215. findAll(selector: FindSelector): Promise<E2EElement[]>;
  216. /**
  217. * Sets focus on the element.
  218. */
  219. focus(): Promise<void>;
  220. /**
  221. * Returns the value of a specified attribute on the element. If the
  222. * given attribute does not exist, the value returned will be null.
  223. */
  224. getAttribute(name: string): string;
  225. /**
  226. * Used to get a property set on a component. For example, if a
  227. * component has the property `elm.myProp`, then calling
  228. * `elm.getProperty('myProp')` would return the `myProp` property value.
  229. */
  230. getProperty(propertyName: string): Promise<any>;
  231. /**
  232. * Returns an object that reports the values of all CSS properties of this
  233. * element after applying active stylesheets and resolving any basic computation
  234. * those values may contain. Individual CSS property values are accessed by
  235. * simply indexing with CSS property names. The method is shortcut and an async
  236. * version of using `window.getComputedStyle(element)` directly.
  237. */
  238. getComputedStyle(pseudoElt?: string | null): Promise<CSSStyleDeclaration>;
  239. /**
  240. * Sets hover on the element.
  241. */
  242. hover(): Promise<void>;
  243. /**
  244. * Gets and sets `id` property of the element.
  245. * Note that `await page.waitForChanges()` must be called before reading
  246. * the value if content has changed.
  247. */
  248. id: string;
  249. /**
  250. * Gets and sets `innerHTML` property of the element.
  251. * Note that `await page.waitForChanges()` must be called before reading
  252. * the value if content has changed.
  253. */
  254. innerHTML: string;
  255. /**
  256. * Gets and sets `innerText` property of the element.
  257. * Note that `await page.waitForChanges()` must be called before reading
  258. * the value if content has changed.
  259. */
  260. innerText: string;
  261. /**
  262. * Resolves to true if the element is visible in the current viewport.
  263. */
  264. isIntersectingViewport(): Promise<boolean>;
  265. /**
  266. * Resolves `true` when the element's style is `display !== 'none'`,
  267. * `visibility !== 'hidden'` and `opacity !== '0'`.
  268. */
  269. isVisible(): Promise<boolean>;
  270. /**
  271. * Node name of the node, which in an element's case is the tag name.
  272. * Note, this will always be upper-cased.
  273. */
  274. nodeName: string;
  275. /**
  276. * The type of a node represented by a number.
  277. * Element = 1, TextNode = 3, Comment = 8,
  278. * Document Fragment (also what a shadow root is) = 11.
  279. */
  280. nodeType: number;
  281. /**
  282. * Gets the element's `outerHTML. This is a read-only property and will
  283. * throw an error if set.
  284. */
  285. outerHTML: string;
  286. /**
  287. * Focuses the element, and then uses `keyboard.down` and `keyboard.up`.
  288. * If key is a single character and no modifier keys besides Shift are
  289. * being held down, a keypress/input event will also be generated. The
  290. * text option can be specified to force an input event to be generated.
  291. * Note: Modifier keys DO effect `elementHandle.press`. Holding down Shift
  292. * will type the text in upper case.
  293. * Key names: https://github.com/puppeteer/puppeteer/blob/main/src/common/USKeyboardLayout.ts
  294. */
  295. press(key: string, options?: {
  296. text?: string;
  297. delay?: number;
  298. }): Promise<void>;
  299. /**
  300. * Removes the attribute on the specified element. Note that
  301. * `await page.waitForChanges()` must be called before reading
  302. * the value if content has changed.
  303. */
  304. removeAttribute(name: string): void;
  305. /**
  306. * Sets the value of an attribute on the specified element. If the
  307. * attribute already exists, the value is updated; otherwise a new
  308. * attribute is added with the specified name and value. The value
  309. * will always be converted to a string. Note that
  310. * `await page.waitForChanges()` must be called before reading
  311. * the value if content has changed.
  312. */
  313. setAttribute(name: string, value: any): void;
  314. /**
  315. * Used to set a property set on a component. For example, if a
  316. * component has the property `elm.myProp`, then calling
  317. * `elm.setProperty('myProp', 88)` would set the value `88` to
  318. * the `myProp` property on the component.
  319. */
  320. setProperty(propertyName: string, value: any): void;
  321. /**
  322. * The ShadowRoot interface of the Shadow DOM API is the root node of a
  323. * DOM subtree that is rendered separately from a document's main DOM tree.
  324. * This value will be `null` if the element does not have a `shadowRoot`.
  325. */
  326. shadowRoot: ShadowRoot;
  327. /**
  328. * Used to test if an event was, or was not dispatched. This method
  329. * returns a promise, that resolves with an EventSpy. The EventSpy
  330. * can be used along with `expect(spy).toHaveReceivedEvent()`,
  331. * `expect(spy).toHaveReceivedEventTimes(x)` and
  332. * `expect(spy).toHaveReceivedEventDetail({...})`.
  333. */
  334. spyOnEvent(eventName: string): Promise<EventSpy>;
  335. /**
  336. * Represents the tab order of the current element. Setting the
  337. * `tabIndex` property will also set the `tabindex` attribute.
  338. */
  339. tabIndex: number;
  340. /**
  341. * Tag name of the element. Note, this will always be upper-cased.
  342. */
  343. tagName: string;
  344. /**
  345. * This method scrolls the element it into view if needed,
  346. * and then uses `page.touchscreen` to tap in the center of the element.
  347. */
  348. tap(): Promise<void>;
  349. /**
  350. * The `textContent` property represents the text content of a node
  351. * and its descendants. Note that `await page.waitForChanges()` must
  352. * be called before reading the value if content has changed.
  353. */
  354. textContent: string;
  355. /**
  356. * Represents the `title` of the element, the text usually displayed in a
  357. * 'tool tip' popup when the mouse is over the displayed node.
  358. */
  359. title: string;
  360. /**
  361. * Toggles a `boolean` attribute (removing it if it is present and adding
  362. * it if it is not present) on the given element. Note that
  363. * `await page.waitForChanges()` must be called before reading
  364. * the value if content has changed. The optional `force` argument is a
  365. * `boolean` value to determine whether the attribute should be added or
  366. * removed, no matter whether the attribute is present or not at the moment.
  367. */
  368. toggleAttribute(name: string, force?: boolean): void;
  369. /**
  370. * This is a convenience method to easily create a `CustomEvent`,
  371. * and dispatch it from the element, to include any custom event
  372. * `detail` data as the second argument.
  373. */
  374. triggerEvent(eventName: string, eventInitDict?: EventInitDict): void;
  375. /**
  376. * Sends a keydown, keypress/input, and keyup event for each character in the text.
  377. * To press a special key, like Control or ArrowDown, use `keyboard.press`.
  378. */
  379. type(text: string, options?: {
  380. delay: number;
  381. }): Promise<void>;
  382. /**
  383. * Waits until the element's style is `display !== 'none'`,
  384. * `visibility !== 'hidden'`, `opacity !== '0'` and the element
  385. * is connected to the document.
  386. */
  387. waitForVisible(): Promise<void>;
  388. /**
  389. * Waits until the element's style is `display === 'none'`, or
  390. * `visibility === 'hidden'`, or `opacity === '0'`, or the element
  391. * is no longer connected to the document.
  392. */
  393. waitForNotVisible(): Promise<void>;
  394. /**
  395. * Waits until the given event is listened in the element.
  396. */
  397. waitForEvent(eventName: string): Promise<any>;
  398. }
  399. export interface E2EElementInternal extends E2EElement {
  400. e2eDispose(): Promise<void>;
  401. e2eRunActions(): Promise<unknown>;
  402. e2eSync(): Promise<void>;
  403. }
  404. export declare type FindSelector = string | FindSelectorOptions;
  405. export interface FindSelectorOptions {
  406. /**
  407. * Finds an element with text content matching this
  408. * exact value after the whitespace has been trimmed.
  409. */
  410. text?: string;
  411. /**
  412. * Finds an element with text content containing this value.
  413. */
  414. contains?: string;
  415. }
  416. export interface WaitForEventOptions {
  417. timeout?: number;
  418. }
  419. export interface WaitForEvent {
  420. eventName: string;
  421. callback: (ev: any) => void;
  422. }
  423. export interface BrowserWindow extends Window {
  424. stencilOnEvent(id: number, event: any): void;
  425. stencilSerializeEvent(ev: CustomEvent): any;
  426. stencilSerializeEventTarget(target: any): any;
  427. stencilAppLoaded: boolean;
  428. }
  429. export {};