@wordpress/e2e-test-utils-playwright 1.46.0 → 1.48.0

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 (295) hide show
  1. package/build-types/admin/index.d.ts +3 -0
  2. package/build-types/admin/index.d.ts.map +1 -1
  3. package/build-types/admin/visit-site-editor.d.ts.map +1 -1
  4. package/build-types/admin/wait-for-site-editor.d.ts +12 -0
  5. package/build-types/admin/wait-for-site-editor.d.ts.map +1 -0
  6. package/build-types/metrics/index.d.ts +22 -5
  7. package/build-types/metrics/index.d.ts.map +1 -1
  8. package/build-types/request-utils/index.d.ts +3 -1
  9. package/build-types/request-utils/index.d.ts.map +1 -1
  10. package/build-types/request-utils/preferences.d.ts +11 -0
  11. package/build-types/request-utils/preferences.d.ts.map +1 -1
  12. package/package.json +4 -5
  13. package/src/admin/create-new-post.ts +47 -0
  14. package/src/admin/edit-post.ts +24 -0
  15. package/{build/admin/get-page-error.js → src/admin/get-page-error.ts} +12 -9
  16. package/src/admin/index.ts +52 -0
  17. package/src/admin/visit-admin-page.ts +43 -0
  18. package/src/admin/visit-site-editor.ts +62 -0
  19. package/src/admin/wait-for-site-editor.ts +52 -0
  20. package/src/config.ts +12 -0
  21. package/src/editor/click-block-options-menu-item.ts +18 -0
  22. package/src/editor/click-block-toolbar-button.ts +21 -0
  23. package/src/editor/get-blocks.ts +75 -0
  24. package/src/editor/get-edited-post-content.ts +19 -0
  25. package/src/editor/index.ts +95 -0
  26. package/src/editor/insert-block.ts +54 -0
  27. package/src/editor/open-document-settings-sidebar.ts +31 -0
  28. package/src/editor/preview.ts +32 -0
  29. package/src/editor/publish-post.ts +51 -0
  30. package/src/editor/save-draft.ts +20 -0
  31. package/src/editor/select-blocks.ts +48 -0
  32. package/src/editor/set-content.ts +24 -0
  33. package/src/editor/set-is-fixed-toolbar.ts +20 -0
  34. package/src/editor/set-preferences.ts +37 -0
  35. package/src/editor/show-block-toolbar.ts +20 -0
  36. package/src/editor/site-editor.ts +55 -0
  37. package/src/editor/switch-editor-tool.ts +34 -0
  38. package/src/editor/switch-to-legacy-canvas.ts +20 -0
  39. package/src/editor/transform-block-to.ts +34 -0
  40. package/src/index.ts +8 -0
  41. package/src/lighthouse/index.ts +75 -0
  42. package/src/metrics/index.ts +441 -0
  43. package/src/page-utils/drag-files.ts +173 -0
  44. package/src/page-utils/emulate-network-conditions.ts +101 -0
  45. package/src/page-utils/index.ts +54 -0
  46. package/src/page-utils/is-current-url.ts +20 -0
  47. package/src/page-utils/keycodes.ts +48 -0
  48. package/src/page-utils/press-keys.ts +195 -0
  49. package/{build → src}/page-utils/set-browser-viewport.js +14 -12
  50. package/src/request-utils/blocks.ts +58 -0
  51. package/src/request-utils/comments.ts +80 -0
  52. package/src/request-utils/gutenberg-experiments.ts +62 -0
  53. package/src/request-utils/index.ts +222 -0
  54. package/src/request-utils/login.ts +34 -0
  55. package/src/request-utils/media.ts +102 -0
  56. package/src/request-utils/menus.ts +145 -0
  57. package/src/request-utils/pages.ts +75 -0
  58. package/src/request-utils/patterns.ts +31 -0
  59. package/src/request-utils/plugins.ts +103 -0
  60. package/src/request-utils/posts.ts +69 -0
  61. package/src/request-utils/preferences.ts +63 -0
  62. package/{build/request-utils/records.js → src/request-utils/records.ts} +15 -10
  63. package/src/request-utils/rest.ts +222 -0
  64. package/src/request-utils/site-settings.ts +58 -0
  65. package/src/request-utils/templates.ts +83 -0
  66. package/src/request-utils/themes.ts +110 -0
  67. package/src/request-utils/users.ts +125 -0
  68. package/src/request-utils/widgets.js +68 -0
  69. package/src/test.ts +204 -0
  70. package/src/types.ts +13 -0
  71. package/build/admin/create-new-post.cjs +0 -51
  72. package/build/admin/create-new-post.cjs.map +0 -7
  73. package/build/admin/create-new-post.js +0 -31
  74. package/build/admin/create-new-post.js.map +0 -1
  75. package/build/admin/edit-post.cjs +0 -40
  76. package/build/admin/edit-post.cjs.map +0 -7
  77. package/build/admin/edit-post.js +0 -20
  78. package/build/admin/edit-post.js.map +0 -1
  79. package/build/admin/get-page-error.cjs +0 -36
  80. package/build/admin/get-page-error.cjs.map +0 -7
  81. package/build/admin/get-page-error.js.map +0 -1
  82. package/build/admin/index.cjs +0 -59
  83. package/build/admin/index.cjs.map +0 -7
  84. package/build/admin/index.js +0 -37
  85. package/build/admin/index.js.map +0 -1
  86. package/build/admin/visit-admin-page.cjs +0 -47
  87. package/build/admin/visit-admin-page.cjs.map +0 -7
  88. package/build/admin/visit-admin-page.js +0 -32
  89. package/build/admin/visit-admin-page.js.map +0 -1
  90. package/build/admin/visit-site-editor.cjs +0 -90
  91. package/build/admin/visit-site-editor.cjs.map +0 -7
  92. package/build/admin/visit-site-editor.js +0 -73
  93. package/build/admin/visit-site-editor.js.map +0 -1
  94. package/build/config.cjs +0 -45
  95. package/build/config.cjs.map +0 -7
  96. package/build/config.js +0 -13
  97. package/build/config.js.map +0 -1
  98. package/build/editor/click-block-options-menu-item.cjs +0 -34
  99. package/build/editor/click-block-options-menu-item.cjs.map +0 -7
  100. package/build/editor/click-block-options-menu-item.js +0 -17
  101. package/build/editor/click-block-options-menu-item.js.map +0 -1
  102. package/build/editor/click-block-toolbar-button.cjs +0 -38
  103. package/build/editor/click-block-toolbar-button.cjs.map +0 -7
  104. package/build/editor/click-block-toolbar-button.js +0 -16
  105. package/build/editor/click-block-toolbar-button.js.map +0 -1
  106. package/build/editor/get-blocks.cjs +0 -64
  107. package/build/editor/get-blocks.cjs.map +0 -7
  108. package/build/editor/get-blocks.js +0 -45
  109. package/build/editor/get-blocks.js.map +0 -1
  110. package/build/editor/get-edited-post-content.cjs +0 -36
  111. package/build/editor/get-edited-post-content.cjs.map +0 -7
  112. package/build/editor/get-edited-post-content.js +0 -15
  113. package/build/editor/get-edited-post-content.js.map +0 -1
  114. package/build/editor/index.cjs +0 -97
  115. package/build/editor/index.cjs.map +0 -7
  116. package/build/editor/index.js +0 -75
  117. package/build/editor/index.js.map +0 -1
  118. package/build/editor/insert-block.cjs +0 -55
  119. package/build/editor/insert-block.cjs.map +0 -7
  120. package/build/editor/insert-block.js +0 -23
  121. package/build/editor/insert-block.js.map +0 -1
  122. package/build/editor/open-document-settings-sidebar.cjs +0 -42
  123. package/build/editor/open-document-settings-sidebar.cjs.map +0 -7
  124. package/build/editor/open-document-settings-sidebar.js +0 -27
  125. package/build/editor/open-document-settings-sidebar.js.map +0 -1
  126. package/build/editor/preview.cjs +0 -42
  127. package/build/editor/preview.cjs.map +0 -7
  128. package/build/editor/preview.js +0 -21
  129. package/build/editor/preview.js.map +0 -1
  130. package/build/editor/publish-post.cjs +0 -47
  131. package/build/editor/publish-post.cjs.map +0 -7
  132. package/build/editor/publish-post.js +0 -45
  133. package/build/editor/publish-post.js.map +0 -1
  134. package/build/editor/save-draft.cjs +0 -34
  135. package/build/editor/save-draft.cjs.map +0 -7
  136. package/build/editor/save-draft.js +0 -18
  137. package/build/editor/save-draft.js.map +0 -1
  138. package/build/editor/select-blocks.cjs +0 -51
  139. package/build/editor/select-blocks.cjs.map +0 -7
  140. package/build/editor/select-blocks.js +0 -28
  141. package/build/editor/select-blocks.js.map +0 -1
  142. package/build/editor/set-content.cjs +0 -39
  143. package/build/editor/set-content.cjs.map +0 -7
  144. package/build/editor/set-content.js +0 -17
  145. package/build/editor/set-content.js.map +0 -1
  146. package/build/editor/set-is-fixed-toolbar.cjs +0 -36
  147. package/build/editor/set-is-fixed-toolbar.cjs.map +0 -7
  148. package/build/editor/set-is-fixed-toolbar.js +0 -18
  149. package/build/editor/set-is-fixed-toolbar.js.map +0 -1
  150. package/build/editor/set-preferences.cjs +0 -43
  151. package/build/editor/set-preferences.cjs.map +0 -7
  152. package/build/editor/set-preferences.js +0 -21
  153. package/build/editor/set-preferences.js.map +0 -1
  154. package/build/editor/show-block-toolbar.cjs +0 -35
  155. package/build/editor/show-block-toolbar.cjs.map +0 -7
  156. package/build/editor/show-block-toolbar.js +0 -19
  157. package/build/editor/show-block-toolbar.js.map +0 -1
  158. package/build/editor/site-editor.cjs +0 -51
  159. package/build/editor/site-editor.cjs.map +0 -7
  160. package/build/editor/site-editor.js +0 -44
  161. package/build/editor/site-editor.js.map +0 -1
  162. package/build/editor/switch-editor-tool.cjs +0 -47
  163. package/build/editor/switch-editor-tool.cjs.map +0 -7
  164. package/build/editor/switch-editor-tool.js +0 -33
  165. package/build/editor/switch-editor-tool.js.map +0 -1
  166. package/build/editor/switch-to-legacy-canvas.cjs +0 -39
  167. package/build/editor/switch-to-legacy-canvas.cjs.map +0 -7
  168. package/build/editor/switch-to-legacy-canvas.js +0 -18
  169. package/build/editor/switch-to-legacy-canvas.js.map +0 -1
  170. package/build/editor/transform-block-to.cjs +0 -46
  171. package/build/editor/transform-block-to.cjs.map +0 -7
  172. package/build/editor/transform-block-to.js +0 -24
  173. package/build/editor/transform-block-to.js.map +0 -1
  174. package/build/index.cjs +0 -51
  175. package/build/index.cjs.map +0 -7
  176. package/build/index.js +0 -19
  177. package/build/index.js.map +0 -1
  178. package/build/lighthouse/index.cjs +0 -96
  179. package/build/lighthouse/index.cjs.map +0 -7
  180. package/build/lighthouse/index.js +0 -61
  181. package/build/lighthouse/index.js.map +0 -1
  182. package/build/metrics/index.cjs +0 -309
  183. package/build/metrics/index.cjs.map +0 -7
  184. package/build/metrics/index.js +0 -258
  185. package/build/metrics/index.js.map +0 -1
  186. package/build/page-utils/drag-files.cjs +0 -143
  187. package/build/page-utils/drag-files.cjs.map +0 -7
  188. package/build/page-utils/drag-files.js +0 -119
  189. package/build/page-utils/drag-files.js.map +0 -1
  190. package/build/page-utils/emulate-network-conditions.cjs +0 -99
  191. package/build/page-utils/emulate-network-conditions.cjs.map +0 -7
  192. package/build/page-utils/emulate-network-conditions.js +0 -71
  193. package/build/page-utils/emulate-network-conditions.js.map +0 -1
  194. package/build/page-utils/index.cjs +0 -59
  195. package/build/page-utils/index.cjs.map +0 -7
  196. package/build/page-utils/index.js +0 -37
  197. package/build/page-utils/index.js.map +0 -1
  198. package/build/page-utils/is-current-url.cjs +0 -36
  199. package/build/page-utils/is-current-url.cjs.map +0 -7
  200. package/build/page-utils/is-current-url.js +0 -21
  201. package/build/page-utils/is-current-url.js.map +0 -1
  202. package/build/page-utils/keycodes.cjs +0 -55
  203. package/build/page-utils/keycodes.cjs.map +0 -7
  204. package/build/page-utils/keycodes.js +0 -41
  205. package/build/page-utils/keycodes.js.map +0 -1
  206. package/build/page-utils/press-keys.cjs +0 -165
  207. package/build/page-utils/press-keys.cjs.map +0 -7
  208. package/build/page-utils/press-keys.js +0 -141
  209. package/build/page-utils/press-keys.js.map +0 -1
  210. package/build/page-utils/set-browser-viewport.cjs +0 -39
  211. package/build/page-utils/set-browser-viewport.cjs.map +0 -7
  212. package/build/page-utils/set-browser-viewport.js.map +0 -1
  213. package/build/request-utils/blocks.cjs +0 -51
  214. package/build/request-utils/blocks.cjs.map +0 -7
  215. package/build/request-utils/blocks.js +0 -40
  216. package/build/request-utils/blocks.js.map +0 -1
  217. package/build/request-utils/comments.cjs +0 -66
  218. package/build/request-utils/comments.cjs.map +0 -7
  219. package/build/request-utils/comments.js +0 -52
  220. package/build/request-utils/comments.js.map +0 -1
  221. package/build/request-utils/gutenberg-experiments.cjs +0 -56
  222. package/build/request-utils/gutenberg-experiments.cjs.map +0 -7
  223. package/build/request-utils/gutenberg-experiments.js +0 -47
  224. package/build/request-utils/gutenberg-experiments.js.map +0 -1
  225. package/build/request-utils/index.cjs +0 -199
  226. package/build/request-utils/index.cjs.map +0 -7
  227. package/build/request-utils/index.js +0 -194
  228. package/build/request-utils/index.js.map +0 -1
  229. package/build/request-utils/login.cjs +0 -48
  230. package/build/request-utils/login.cjs.map +0 -7
  231. package/build/request-utils/login.js +0 -21
  232. package/build/request-utils/login.js.map +0 -1
  233. package/build/request-utils/media.cjs +0 -83
  234. package/build/request-utils/media.cjs.map +0 -7
  235. package/build/request-utils/media.js +0 -106
  236. package/build/request-utils/media.js.map +0 -1
  237. package/build/request-utils/menus.cjs +0 -128
  238. package/build/request-utils/menus.cjs.map +0 -7
  239. package/build/request-utils/menus.js +0 -116
  240. package/build/request-utils/menus.js.map +0 -1
  241. package/build/request-utils/pages.cjs +0 -66
  242. package/build/request-utils/pages.cjs.map +0 -7
  243. package/build/request-utils/pages.js +0 -53
  244. package/build/request-utils/pages.js.map +0 -1
  245. package/build/request-utils/patterns.cjs +0 -44
  246. package/build/request-utils/patterns.cjs.map +0 -7
  247. package/build/request-utils/patterns.js +0 -27
  248. package/build/request-utils/patterns.js.map +0 -1
  249. package/build/request-utils/plugins.cjs +0 -81
  250. package/build/request-utils/plugins.cjs.map +0 -7
  251. package/build/request-utils/plugins.js +0 -85
  252. package/build/request-utils/plugins.js.map +0 -1
  253. package/build/request-utils/posts.cjs +0 -56
  254. package/build/request-utils/posts.cjs.map +0 -7
  255. package/build/request-utils/posts.js +0 -42
  256. package/build/request-utils/posts.js.map +0 -1
  257. package/build/request-utils/preferences.cjs +0 -41
  258. package/build/request-utils/preferences.cjs.map +0 -7
  259. package/build/request-utils/preferences.js +0 -20
  260. package/build/request-utils/preferences.js.map +0 -1
  261. package/build/request-utils/records.cjs +0 -37
  262. package/build/request-utils/records.cjs.map +0 -7
  263. package/build/request-utils/records.js.map +0 -1
  264. package/build/request-utils/rest.cjs +0 -174
  265. package/build/request-utils/rest.cjs.map +0 -7
  266. package/build/request-utils/rest.js +0 -181
  267. package/build/request-utils/rest.js.map +0 -1
  268. package/build/request-utils/site-settings.cjs +0 -45
  269. package/build/request-utils/site-settings.cjs.map +0 -7
  270. package/build/request-utils/site-settings.js +0 -33
  271. package/build/request-utils/site-settings.js.map +0 -1
  272. package/build/request-utils/templates.cjs +0 -68
  273. package/build/request-utils/templates.cjs.map +0 -7
  274. package/build/request-utils/templates.js +0 -54
  275. package/build/request-utils/templates.js.map +0 -1
  276. package/build/request-utils/themes.cjs +0 -92
  277. package/build/request-utils/themes.cjs.map +0 -7
  278. package/build/request-utils/themes.js +0 -69
  279. package/build/request-utils/themes.js.map +0 -1
  280. package/build/request-utils/users.cjs +0 -83
  281. package/build/request-utils/users.cjs.map +0 -7
  282. package/build/request-utils/users.js +0 -81
  283. package/build/request-utils/users.js.map +0 -1
  284. package/build/request-utils/widgets.cjs +0 -79
  285. package/build/request-utils/widgets.cjs.map +0 -7
  286. package/build/request-utils/widgets.js +0 -61
  287. package/build/request-utils/widgets.js.map +0 -1
  288. package/build/test.cjs +0 -136
  289. package/build/test.cjs.map +0 -7
  290. package/build/test.js +0 -189
  291. package/build/test.js.map +0 -1
  292. package/build/types.cjs +0 -19
  293. package/build/types.cjs.map +0 -7
  294. package/build/types.js +0 -3
  295. package/build/types.js.map +0 -1
@@ -0,0 +1,54 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import type {
5
+ Browser,
6
+ Page,
7
+ BrowserContext,
8
+ PlaywrightWorkerOptions,
9
+ } from '@playwright/test';
10
+
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+ import { dragFiles } from './drag-files';
15
+ import { isCurrentURL } from './is-current-url';
16
+ import { setClipboardData, pressKeys } from './press-keys';
17
+ import { setBrowserViewport } from './set-browser-viewport';
18
+ import { emulateNetworkConditions } from './emulate-network-conditions';
19
+
20
+ type PageUtilConstructorParams = {
21
+ page: Page;
22
+ browserName: PlaywrightWorkerOptions[ 'browserName' ];
23
+ };
24
+
25
+ class PageUtils {
26
+ browser: Browser;
27
+ page: Page;
28
+ context: BrowserContext;
29
+ browserName: PlaywrightWorkerOptions[ 'browserName' ];
30
+
31
+ constructor( { page, browserName }: PageUtilConstructorParams ) {
32
+ this.page = page;
33
+ this.context = page.context();
34
+ this.browser = this.context.browser()!;
35
+ this.browserName = browserName;
36
+ }
37
+
38
+ /** @borrows dragFiles as this.dragFiles */
39
+ dragFiles: typeof dragFiles = dragFiles.bind( this );
40
+ /** @borrows isCurrentURL as this.isCurrentURL */
41
+ isCurrentURL: typeof isCurrentURL = isCurrentURL.bind( this );
42
+ /** @borrows pressKeys as this.pressKeys */
43
+ pressKeys: typeof pressKeys = pressKeys.bind( this );
44
+ /** @borrows setBrowserViewport as this.setBrowserViewport */
45
+ setBrowserViewport: typeof setBrowserViewport =
46
+ setBrowserViewport.bind( this );
47
+ /** @borrows setClipboardData as this.setClipboardData */
48
+ setClipboardData: typeof setClipboardData = setClipboardData.bind( this );
49
+ /** @borrows emulateNetworkConditions as this.emulateNetworkConditions */
50
+ emulateNetworkConditions: typeof emulateNetworkConditions =
51
+ emulateNetworkConditions.bind( this );
52
+ }
53
+
54
+ export { PageUtils };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import { WP_BASE_URL } from '../config';
5
+ import type { PageUtils } from './';
6
+
7
+ /**
8
+ * Checks if current path of the URL matches the provided path.
9
+ *
10
+ * @param this
11
+ * @param path String to be serialized as pathname.
12
+ *
13
+ * @return Boolean represents whether current URL is or not a WordPress path.
14
+ */
15
+ export function isCurrentURL( this: PageUtils, path: string ) {
16
+ const currentURL = new URL( this.page.url() );
17
+ const expectedURL = new URL( path, WP_BASE_URL );
18
+
19
+ return expectedURL.pathname === currentURL.pathname;
20
+ }
@@ -0,0 +1,48 @@
1
+ /**
2
+ * This filed is partially copied from @wordpress/keycodes to keep the package
3
+ * (internal-)dependencies free.
4
+ */
5
+
6
+ /**
7
+ * Keycode for ALT key.
8
+ */
9
+ export const ALT = 'alt';
10
+
11
+ /**
12
+ * Keycode for CTRL key.
13
+ */
14
+ export const CTRL = 'ctrl';
15
+
16
+ /**
17
+ * Keycode for COMMAND key.
18
+ */
19
+ export const COMMAND = 'meta';
20
+
21
+ /**
22
+ * Keycode for SHIFT key.
23
+ */
24
+ export const SHIFT = 'shift';
25
+
26
+ /**
27
+ * Object that contains functions that return the available modifier
28
+ * depending on platform.
29
+ */
30
+ export const modifiers: Record<
31
+ string,
32
+ ( _isApple: () => boolean ) => string[]
33
+ > = {
34
+ primary: ( _isApple ) => ( _isApple() ? [ COMMAND ] : [ CTRL ] ),
35
+ primaryShift: ( _isApple ) =>
36
+ _isApple() ? [ SHIFT, COMMAND ] : [ CTRL, SHIFT ],
37
+ primaryAlt: ( _isApple ) =>
38
+ _isApple() ? [ ALT, COMMAND ] : [ CTRL, ALT ],
39
+ secondary: ( _isApple ) =>
40
+ _isApple() ? [ SHIFT, ALT, COMMAND ] : [ CTRL, SHIFT, ALT ],
41
+ access: ( _isApple ) => ( _isApple() ? [ CTRL, ALT ] : [ SHIFT, ALT ] ),
42
+ ctrl: () => [ CTRL ],
43
+ alt: () => [ ALT ],
44
+ ctrlShift: () => [ CTRL, SHIFT ],
45
+ shift: () => [ SHIFT ],
46
+ shiftAlt: () => [ SHIFT, ALT ],
47
+ undefined: () => [],
48
+ };
@@ -0,0 +1,195 @@
1
+ /**
2
+ * External dependencies
3
+ */
4
+ import { capitalCase } from 'change-case';
5
+ import type { Page } from '@playwright/test';
6
+
7
+ /**
8
+ * Internal dependencies
9
+ */
10
+ import type { PageUtils } from './';
11
+ import { modifiers as baseModifiers, SHIFT, ALT, CTRL } from './keycodes';
12
+
13
+ let clipboardDataHolder: {
14
+ 'text/plain': string;
15
+ 'text/html': string;
16
+ 'rich-text': string;
17
+ } = {
18
+ 'text/plain': '',
19
+ 'text/html': '',
20
+ 'rich-text': '',
21
+ };
22
+
23
+ /**
24
+ * Sets the clipboard data that can be pasted with
25
+ * `pressKeys( 'primary+v' )`.
26
+ *
27
+ * @param clipboardData
28
+ * @param clipboardData.plainText
29
+ * @param clipboardData.html
30
+ */
31
+ export function setClipboardData(
32
+ this: PageUtils,
33
+ { plainText = '', html = '' }
34
+ ) {
35
+ clipboardDataHolder = {
36
+ 'text/plain': plainText,
37
+ 'text/html': html,
38
+ 'rich-text': '',
39
+ };
40
+ }
41
+
42
+ async function emulateClipboard( page: Page, type: 'copy' | 'cut' | 'paste' ) {
43
+ const output = await page.evaluate(
44
+ ( [ _type, _clipboardData ] ) => {
45
+ const canvasDoc =
46
+ // @ts-ignore
47
+ document.activeElement?.contentDocument ?? document;
48
+ const event = new ClipboardEvent( _type, {
49
+ bubbles: true,
50
+ cancelable: true,
51
+ clipboardData: new DataTransfer(),
52
+ } );
53
+
54
+ if ( ! event.clipboardData ) {
55
+ throw new Error( 'ClipboardEvent.clipboardData is null' );
56
+ }
57
+
58
+ if ( _type === 'paste' ) {
59
+ event.clipboardData.setData(
60
+ 'text/plain',
61
+ _clipboardData[ 'text/plain' ]
62
+ );
63
+ event.clipboardData.setData(
64
+ 'text/html',
65
+ _clipboardData[ 'text/html' ]
66
+ );
67
+ event.clipboardData.setData(
68
+ 'rich-text',
69
+ _clipboardData[ 'rich-text' ]
70
+ );
71
+ } else {
72
+ const selection = canvasDoc.defaultView.getSelection()!;
73
+ const plainText = selection.toString();
74
+ let html = plainText;
75
+ if ( selection.rangeCount ) {
76
+ const range = selection.getRangeAt( 0 );
77
+ const fragment = range.cloneContents();
78
+ html = Array.from( fragment.childNodes )
79
+ .map(
80
+ ( node ) =>
81
+ ( node as Element ).outerHTML ??
82
+ ( node as Element ).nodeValue
83
+ )
84
+ .join( '' );
85
+ }
86
+ event.clipboardData.setData( 'text/plain', plainText );
87
+ event.clipboardData.setData( 'text/html', html );
88
+ }
89
+
90
+ canvasDoc.activeElement.dispatchEvent( event );
91
+
92
+ if ( _type === 'paste' ) {
93
+ return event.defaultPrevented;
94
+ }
95
+
96
+ return {
97
+ 'text/plain': event.clipboardData.getData( 'text/plain' ),
98
+ 'text/html': event.clipboardData.getData( 'text/html' ),
99
+ 'rich-text': event.clipboardData.getData( 'rich-text' ),
100
+ };
101
+ },
102
+ [ type, clipboardDataHolder ] as const
103
+ );
104
+
105
+ if ( typeof output === 'object' ) {
106
+ clipboardDataHolder = output;
107
+ }
108
+
109
+ if ( output === false ) {
110
+ // Emulate paste by typing the clipboard content, which works across all
111
+ // elements and documents (keyboard.type does uses the nested active
112
+ // element automatically).
113
+ await page.keyboard.type( clipboardDataHolder[ 'text/plain' ] );
114
+ }
115
+ }
116
+
117
+ const isAppleOS = () => process.platform === 'darwin';
118
+
119
+ const isWebkit = ( page: Page ) =>
120
+ page.context().browser()!.browserType().name() === 'webkit';
121
+
122
+ const browserCache = new WeakMap();
123
+ const getHasNaturalTabNavigation = async ( page: Page ) => {
124
+ if ( ! isAppleOS() || ! isWebkit( page ) ) {
125
+ return true;
126
+ }
127
+ if ( browserCache.has( page.context().browser()! ) ) {
128
+ return browserCache.get( page.context().browser()! );
129
+ }
130
+ const testPage = await page.context().newPage();
131
+ await testPage.setContent( `<button>1</button><button>2</button>` );
132
+ await testPage.getByText( '1' ).focus();
133
+ await testPage.keyboard.press( 'Tab' );
134
+ const featureDetected = await testPage
135
+ .getByText( '2' )
136
+ .evaluate( ( node ) => node === document.activeElement );
137
+ browserCache.set( page.context().browser()!, featureDetected );
138
+ await testPage.close();
139
+ return featureDetected;
140
+ };
141
+
142
+ type Options = {
143
+ times?: number;
144
+ delay?: number;
145
+ };
146
+
147
+ const modifiers = {
148
+ ...baseModifiers,
149
+ shiftAlt: ( _isApple: () => boolean ) =>
150
+ _isApple() ? [ SHIFT, ALT ] : [ SHIFT, CTRL ],
151
+ };
152
+
153
+ export async function pressKeys(
154
+ this: PageUtils,
155
+ key: string,
156
+ { times, ...pressOptions }: Options = {}
157
+ ) {
158
+ const hasNaturalTabNavigation = await getHasNaturalTabNavigation(
159
+ this.page
160
+ );
161
+
162
+ let command: () => Promise< void >;
163
+
164
+ if ( key.toLowerCase() === 'primary+c' ) {
165
+ command = () => emulateClipboard( this.page, 'copy' );
166
+ } else if ( key.toLowerCase() === 'primary+x' ) {
167
+ command = () => emulateClipboard( this.page, 'cut' );
168
+ } else if ( key.toLowerCase() === 'primary+v' ) {
169
+ command = () => emulateClipboard( this.page, 'paste' );
170
+ } else {
171
+ const keys = key.split( '+' ).flatMap( ( keyCode ) => {
172
+ if ( Object.prototype.hasOwnProperty.call( modifiers, keyCode ) ) {
173
+ return modifiers[ keyCode as keyof typeof modifiers ](
174
+ isAppleOS
175
+ ).map( ( modifier ) =>
176
+ modifier === CTRL ? 'Control' : capitalCase( modifier )
177
+ );
178
+ } else if ( keyCode === 'Tab' && ! hasNaturalTabNavigation ) {
179
+ return [ 'Alt', 'Tab' ];
180
+ }
181
+ return keyCode;
182
+ } );
183
+ const normalizedKeys = keys.join( '+' );
184
+ command = () => this.page.keyboard.press( normalizedKeys );
185
+ }
186
+
187
+ times = times ?? 1;
188
+ for ( let i = 0; i < times; i += 1 ) {
189
+ await command();
190
+
191
+ if ( times > 1 && pressOptions.delay ) {
192
+ await this.page.waitForTimeout( pressOptions.delay );
193
+ }
194
+ }
195
+ }
@@ -1,11 +1,9 @@
1
- "use strict";
2
1
  /**
3
2
  * Named viewport options.
4
3
  *
5
4
  * @typedef {"large"|"medium"|"small"} WPDimensionsName
6
5
  */
7
- Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.setBrowserViewport = setBrowserViewport;
6
+
9
7
  /**
10
8
  * Viewport dimensions object.
11
9
  *
@@ -14,6 +12,7 @@ exports.setBrowserViewport = setBrowserViewport;
14
12
  * @property {number} width Width, in pixels.
15
13
  * @property {number} height Height, in pixels.
16
14
  */
15
+
17
16
  /**
18
17
  * Predefined viewport dimensions to reference by name.
19
18
  *
@@ -22,25 +21,28 @@ exports.setBrowserViewport = setBrowserViewport;
22
21
  * @type {Record<WPDimensionsName, WPViewportDimensions>}
23
22
  */
24
23
  const PREDEFINED_DIMENSIONS = {
25
- large: { width: 960, height: 700 },
26
- medium: { width: 768, height: 700 },
27
- small: { width: 600, height: 700 },
24
+ large: { width: 960, height: 700 },
25
+ medium: { width: 768, height: 700 },
26
+ small: { width: 600, height: 700 },
28
27
  };
28
+
29
29
  /**
30
30
  * Valid argument argument type from which to derive viewport dimensions.
31
31
  *
32
32
  * @typedef {WPDimensionsName|WPViewportDimensions} WPViewport
33
33
  */
34
+
34
35
  /**
35
36
  * Sets browser viewport to specified type.
36
37
  *
37
38
  * @this {import('./').PageUtils}
38
39
  * @param {WPViewport} viewport Viewport name or dimensions object to assign.
39
40
  */
40
- async function setBrowserViewport(viewport) {
41
- const dimensions = typeof viewport === 'string'
42
- ? PREDEFINED_DIMENSIONS[viewport]
43
- : viewport;
44
- await this.page.setViewportSize(dimensions);
41
+ export async function setBrowserViewport( viewport ) {
42
+ const dimensions =
43
+ typeof viewport === 'string'
44
+ ? PREDEFINED_DIMENSIONS[ viewport ]
45
+ : viewport;
46
+
47
+ await this.page.setViewportSize( dimensions );
45
48
  }
46
- //# sourceMappingURL=set-browser-viewport.js.map
@@ -0,0 +1,58 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { RequestUtils } from './index';
5
+
6
+ type CreateBlockPayload = {
7
+ date?: string;
8
+ date_gmt?: string;
9
+ slug?: string;
10
+ title: string;
11
+ status: 'publish' | 'future' | 'draft' | 'pending' | 'private';
12
+ content?: string;
13
+ meta?: unknown;
14
+ wp_pattern_category?: number[];
15
+ };
16
+
17
+ /**
18
+ * Delete all blocks using REST API.
19
+ *
20
+ * @see https://developer.wordpress.org/rest-api/reference/blocks/#list-editor-blocks
21
+ * @param this
22
+ */
23
+ export async function deleteAllBlocks( this: RequestUtils ) {
24
+ // List all blocks.
25
+ // https://developer.wordpress.org/rest-api/reference/blocks/#list-editor-blocks
26
+ const blocks = await this.rest( {
27
+ path: '/wp/v2/blocks',
28
+ params: {
29
+ per_page: 100,
30
+ // All possible statuses.
31
+ status: 'publish,future,draft,pending,private,trash',
32
+ },
33
+ } );
34
+
35
+ // Delete blocks.
36
+ // https://developer.wordpress.org/rest-api/reference/blocks/#delete-a-editor-block
37
+ // "/wp/v2/posts" not yet supports batch requests.
38
+ await this.batchRest(
39
+ blocks.map( ( block: { id: number } ) => ( {
40
+ method: 'DELETE',
41
+ path: `/wp/v2/blocks/${ block.id }?force=true`,
42
+ } ) )
43
+ );
44
+ }
45
+
46
+ /**
47
+ * Creates a new block using the REST API.
48
+ *
49
+ * @see https://developer.wordpress.org/rest-api/reference/blocks/#create-a-editor-block.
50
+ * @param this
51
+ * @param payload Block payload.
52
+ */
53
+ export async function createBlock(
54
+ this: RequestUtils,
55
+ payload: CreateBlockPayload
56
+ ) {
57
+ return this.createRecord( 'blocks', { ...payload } );
58
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { RequestUtils } from './index';
5
+
6
+ export interface Comment {
7
+ id: number;
8
+ author: number;
9
+ content: string;
10
+ post: number;
11
+ }
12
+
13
+ export interface CreateCommentPayload {
14
+ content: string;
15
+ post: number;
16
+ }
17
+
18
+ export interface User {
19
+ id: number;
20
+ }
21
+
22
+ /**
23
+ * Create new comment using the REST API.
24
+ *
25
+ * @param this
26
+ * @param payload
27
+ */
28
+ export async function createComment(
29
+ this: RequestUtils,
30
+ payload: CreateCommentPayload
31
+ ) {
32
+ const currentUser = await this.rest< User >( {
33
+ path: '/wp/v2/users/me',
34
+ method: 'GET',
35
+ } );
36
+
37
+ const author = currentUser.id;
38
+
39
+ const comment = await this.rest< Comment >( {
40
+ method: 'POST',
41
+ path: '/wp/v2/comments',
42
+ data: { ...payload, author },
43
+ } );
44
+
45
+ return comment;
46
+ }
47
+
48
+ /**
49
+ * Delete all comments using the REST API.
50
+ *
51
+ * @param this
52
+ * @param type - Optional comment type to delete.
53
+ */
54
+ export async function deleteAllComments( this: RequestUtils, type?: string ) {
55
+ // List all comments.
56
+ // https://developer.wordpress.org/rest-api/reference/comments/#list-comments
57
+ const comments = await this.rest( {
58
+ path: '/wp/v2/comments',
59
+ params: {
60
+ per_page: 100,
61
+ status: 'all',
62
+ type: type || 'comment',
63
+ },
64
+ } );
65
+
66
+ // Delete all comments one by one.
67
+ // https://developer.wordpress.org/rest-api/reference/comments/#delete-a-comment
68
+ // "/wp/v2/comments" doesn't support batch requests yet.
69
+ await Promise.all(
70
+ comments.map( ( comment: Comment ) =>
71
+ this.rest( {
72
+ method: 'DELETE',
73
+ path: `/wp/v2/comments/${ comment.id }`,
74
+ params: {
75
+ force: true,
76
+ },
77
+ } )
78
+ )
79
+ );
80
+ }
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Internal dependencies
3
+ */
4
+ import type { RequestUtils } from './index';
5
+
6
+ /**
7
+ * Sets the Gutenberg experiments.
8
+ *
9
+ * @param this
10
+ * @param experiments Array of experimental flags to enable. Pass in an empty array to disable all experiments.
11
+ * Use 'active_templates' for the template activation feature.
12
+ */
13
+ async function setGutenbergExperiments(
14
+ this: RequestUtils,
15
+ experiments: string[]
16
+ ) {
17
+ // Separate regular experiments from active_templates.
18
+ // active_templates is stored as a separate option, not in the experiments array.
19
+ const regularExperiments = experiments.filter(
20
+ ( exp ) => exp !== 'active_templates'
21
+ );
22
+ const hasActiveTemplates = experiments.includes( 'active_templates' );
23
+
24
+ // Build the experiments object with boolean values.
25
+ // When empty array is passed, we send an empty object to disable all experiments.
26
+ const experimentsData: Record< string, boolean > = {};
27
+
28
+ for ( const experiment of regularExperiments ) {
29
+ experimentsData[ experiment ] = true;
30
+ }
31
+
32
+ const settingsData: Record< string, unknown > = {
33
+ 'gutenberg-experiments': experimentsData,
34
+ };
35
+
36
+ // active_templates lives in a separate top-level option. Sending `{}`
37
+ // enables the experiment; sending `null` deletes the option and disables
38
+ // it.
39
+ if ( hasActiveTemplates ) {
40
+ settingsData.active_templates = {};
41
+ } else {
42
+ // WP_REST_Settings_Controller rejects null updates when the stored
43
+ // value does not match the `type: 'object'` schema (including when the
44
+ // option is absent and `get_option` falls back to `false`), so we only
45
+ // send null when the option actually exists.
46
+ const currentSiteSettings =
47
+ ( await this.getSiteSettings() ) as unknown as {
48
+ active_templates?: unknown;
49
+ };
50
+ if ( currentSiteSettings.active_templates !== null ) {
51
+ settingsData.active_templates = null;
52
+ }
53
+ }
54
+
55
+ await this.rest( {
56
+ path: '/wp/v2/settings',
57
+ method: 'POST',
58
+ data: settingsData,
59
+ } );
60
+ }
61
+
62
+ export { setGutenbergExperiments };