utils.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import { Page, Locator } from 'playwright'
  2. import { expect, ConsoleMessage } from '@playwright/test'
  3. import * as pathlib from 'path'
  4. import { modKey } from './util/basic'
  5. // TODO: The file should be a facade of utils in the /util folder
  6. // No more additional functions should be added to this file
  7. // Move the functions to the corresponding files in the /util folder
  8. // Criteria: If the same selector is shared in multiple functions, they should be in the same file
  9. export * from './util/basic'
  10. export * from './util/search-modal'
  11. export * from './util/page'
  12. /**
  13. * Locate the last block in the inner editor
  14. * @param page The Playwright Page object.
  15. * @returns The locator of the last block.
  16. */
  17. export async function lastBlock(page: Page): Promise<Locator> {
  18. // discard any popups
  19. await page.keyboard.press('Escape')
  20. // click last block
  21. if (await page.locator('text="Click here to edit..."').isVisible()) {
  22. await page.click('text="Click here to edit..."')
  23. } else {
  24. await page.click('.page-blocks-inner .ls-block >> nth=-1')
  25. }
  26. // wait for textarea
  27. await page.waitForSelector('textarea >> nth=0', { state: 'visible' })
  28. await page.waitForTimeout(100)
  29. return page.locator('textarea >> nth=0')
  30. }
  31. /**
  32. * Press Enter and create the next block.
  33. * @param page The Playwright Page object.
  34. */
  35. export async function enterNextBlock(page: Page): Promise<Locator> {
  36. // Move cursor to the end of the editor
  37. await page.press('textarea >> nth=0', modKey + '+a') // select all
  38. await page.press('textarea >> nth=0', 'ArrowRight')
  39. let blockCount = await page.locator('.page-blocks-inner .ls-block').count()
  40. await page.press('textarea >> nth=0', 'Enter')
  41. await page.waitForTimeout(10)
  42. await page.waitForSelector(`.ls-block >> nth=${blockCount} >> textarea`, { state: 'visible' })
  43. return page.locator('textarea >> nth=0')
  44. }
  45. /**
  46. * Create and locate a new block at the end of the inner editor
  47. * @param page The Playwright Page object
  48. * @returns The locator of the last block
  49. */
  50. export async function newInnerBlock(page: Page): Promise<Locator> {
  51. await lastBlock(page)
  52. await page.press('textarea >> nth=0', 'Enter')
  53. return page.locator('textarea >> nth=0')
  54. }
  55. export async function escapeToCodeEditor(page: Page): Promise<void> {
  56. await page.press('.block-editor textarea', 'Escape')
  57. await page.waitForSelector('.CodeMirror pre', { state: 'visible' })
  58. await page.waitForTimeout(300)
  59. await page.click('.CodeMirror pre')
  60. await page.waitForTimeout(300)
  61. await page.waitForSelector('.CodeMirror textarea', { state: 'visible' })
  62. }
  63. export async function escapeToBlockEditor(page: Page): Promise<void> {
  64. await page.waitForTimeout(300)
  65. await page.click('.CodeMirror pre')
  66. await page.waitForTimeout(300)
  67. await page.press('.CodeMirror textarea', 'Escape')
  68. await page.waitForTimeout(300)
  69. }
  70. export async function setMockedOpenDirPath(
  71. page: Page,
  72. path?: string
  73. ): Promise<void> {
  74. // set next open directory
  75. await page.evaluate(
  76. ([path]) => {
  77. Object.assign(window, {
  78. __MOCKED_OPEN_DIR_PATH__: path,
  79. })
  80. },
  81. [path]
  82. )
  83. }
  84. export async function openLeftSidebar(page: Page): Promise<void> {
  85. let sidebar = page.locator('#left-sidebar')
  86. // Left sidebar is toggled by `is-open` class
  87. if (!/is-open/.test(await sidebar.getAttribute('class') || '')) {
  88. await page.click('#left-menu.button')
  89. await page.waitForTimeout(10)
  90. await expect(sidebar).toHaveClass(/is-open/)
  91. }
  92. }
  93. export async function loadLocalGraph(page: Page, path: string): Promise<void> {
  94. await setMockedOpenDirPath(page, path);
  95. const onboardingOpenButton = page.locator('strong:has-text("Choose a folder")')
  96. if (await onboardingOpenButton.isVisible()) {
  97. await onboardingOpenButton.click()
  98. } else {
  99. console.log("No onboarding button, loading file manually")
  100. let sidebar = page.locator('#left-sidebar')
  101. if (!/is-open/.test(await sidebar.getAttribute('class') || '')) {
  102. await page.click('#left-menu.button')
  103. await expect(sidebar).toHaveClass(/is-open/)
  104. }
  105. await page.click('#left-sidebar #repo-switch');
  106. await page.waitForSelector('#left-sidebar .dropdown-wrapper >> text="Add new graph"',
  107. { state: 'visible', timeout: 5000 })
  108. await page.click('text=Add new graph')
  109. expect(page.locator('#repo-name')).toHaveText(pathlib.basename(path))
  110. }
  111. setMockedOpenDirPath(page, ''); // reset it
  112. await page.waitForSelector(':has-text("Parsing files")', {
  113. state: 'hidden',
  114. timeout: 1000 * 60 * 5,
  115. })
  116. const title = await page.title()
  117. if (title === "Import data into Logseq" || title === "Add another repo") {
  118. await page.click('a.button >> text=Skip')
  119. }
  120. await page.waitForFunction('window.document.title === "Logseq"')
  121. await page.waitForTimeout(500)
  122. // If there is an error notification from a previous test graph being deleted,
  123. // close it first so it doesn't cover up the UI
  124. let n = await page.locator('.notification-close-button').count()
  125. if (n > 1) {
  126. await page.locator('button >> text="Clear all"').click()
  127. } else if (n == 1) {
  128. await page.locator('.notification-close-button').click()
  129. }
  130. await expect(page.locator('.notification-close-button').first()).not.toBeVisible({ timeout: 2000 })
  131. console.log('Graph loaded for ' + path)
  132. }
  133. export async function editFirstBlock(page: Page) {
  134. await page.click('.ls-block .block-content >> nth=0')
  135. }
  136. /**
  137. * Wait for a console message with a given prefix to appear, and return the full text of the message
  138. * Or reject after a timeout
  139. *
  140. * @param page
  141. * @param prefix - the prefix to look for
  142. * @param timeout - the timeout in ms
  143. * @returns the full text of the console message
  144. */
  145. export async function captureConsoleWithPrefix(page: Page, prefix: string, timeout: number = 3000): Promise<string> {
  146. return new Promise((resolve, reject) => {
  147. let console_handler = (msg: ConsoleMessage) => {
  148. let text = msg.text()
  149. if (text.startsWith(prefix)) {
  150. page.removeListener('console', console_handler)
  151. resolve(text.substring(prefix.length))
  152. }
  153. }
  154. page.on('console', console_handler)
  155. setTimeout(reject.bind("timeout"), timeout)
  156. })
  157. }
  158. export async function queryPermission(page: Page, permission: PermissionName): Promise<boolean> {
  159. // Check if WebAPI clipboard supported
  160. return await page.evaluate(async (eval_permission: PermissionName): Promise<boolean> => {
  161. if (typeof navigator.permissions == "undefined")
  162. return Promise.resolve(false);
  163. return navigator.permissions.query({
  164. name: eval_permission
  165. }).then((result: PermissionStatus): boolean => {
  166. return (result.state == "granted" || result.state == "prompt")
  167. })
  168. }, permission)
  169. }
  170. export async function doesClipboardItemExists(page: Page): Promise<boolean> {
  171. // Check if WebAPI clipboard supported
  172. return await page.evaluate((): boolean => {
  173. return typeof ClipboardItem !== "undefined"
  174. })
  175. }
  176. export async function getIsWebAPIClipboardSupported(page: Page): Promise<boolean> {
  177. // @ts-ignore "clipboard-write" is not included in TS's type definition for permissionName
  178. return await queryPermission(page, "clipboard-write") && await doesClipboardItemExists(page)
  179. }