basic.spec.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. import { test, expect } from '@playwright/test'
  2. import { ElectronApplication, Page, BrowserContext, _electron as electron } from 'playwright'
  3. import { randomString, createRandomPage, openSidebar, newBlock } from './utils'
  4. let electronApp: ElectronApplication
  5. let context: BrowserContext
  6. let page: Page
  7. test.beforeAll(async () => {
  8. electronApp = await electron.launch({
  9. cwd: "./static",
  10. args: ["electron.js"],
  11. // NOTE: video recording for Electron is not supported yet
  12. // recordVideo: {
  13. // dir: "./videos",
  14. // }
  15. })
  16. context = electronApp.context()
  17. await context.tracing.start({ screenshots: true, snapshots: true });
  18. // Evaluation expression in the Electron context.
  19. const appPath = await electronApp.evaluate(async ({ app }) => {
  20. // This runs in the main Electron process, parameter here is always
  21. // the result of the require('electron') in the main app script.
  22. return app.getAppPath()
  23. })
  24. console.log("Test start with AppPath:", appPath)
  25. })
  26. test.beforeEach(async () => {
  27. // discard any dialog by ESC
  28. if (page) {
  29. await page.keyboard.press('Escape')
  30. await page.keyboard.press('Escape')
  31. } else {
  32. page = await electronApp.firstWindow()
  33. }
  34. })
  35. test.afterAll(async () => {
  36. // await context.close();
  37. await context.tracing.stop({ path: 'artifacts.zip' });
  38. await electronApp.close()
  39. })
  40. test('render app', async () => {
  41. // Direct Electron console to Node terminal.
  42. // page.on('console', console.log)
  43. // Wait for the app to load
  44. await page.waitForLoadState('domcontentloaded')
  45. await page.waitForFunction('window.document.title != "Loading"')
  46. // Logseq: "A privacy-first platform for knowledge management and collaboration."
  47. // or Logseq
  48. expect(await page.title()).toMatch(/^Logseq.*?/)
  49. page.once('load', async () => {
  50. console.log('Page loaded!')
  51. await page.screenshot({ path: 'startup.png' })
  52. })
  53. })
  54. test('first start', async () => {
  55. await page.waitForSelector('text=This is a demo graph, changes will not be saved until you open a local folder')
  56. })
  57. test('open sidebar', async () => {
  58. await openSidebar(page)
  59. await page.waitForSelector('#sidebar-nav-wrapper a:has-text("New page")', { state: 'visible' })
  60. await page.waitForSelector('#sidebar-nav-wrapper >> text=Journals', { state: 'visible' })
  61. })
  62. test('search', async () => {
  63. await page.click('#search-button')
  64. await page.waitForSelector('[placeholder="Search or create page"]')
  65. await page.fill('[placeholder="Search or create page"]', 'welcome')
  66. await page.waitForTimeout(500)
  67. const results = await page.$$('#ui__ac-inner .block')
  68. expect(results.length).toBeGreaterThanOrEqual(1)
  69. })
  70. test('create page and blocks', async () => {
  71. await createRandomPage(page)
  72. // do editing
  73. await page.fill(':nth-match(textarea, 1)', 'this is my first bullet')
  74. await page.press(':nth-match(textarea, 1)', 'Enter')
  75. // first block
  76. expect(await page.$$('.block-content')).toHaveLength(1)
  77. await page.fill(':nth-match(textarea, 1)', 'this is my second bullet')
  78. await page.press(':nth-match(textarea, 1)', 'Enter')
  79. await page.fill(':nth-match(textarea, 1)', 'this is my third bullet')
  80. await page.press(':nth-match(textarea, 1)', 'Tab')
  81. await page.press(':nth-match(textarea, 1)', 'Enter')
  82. await page.keyboard.type('continue editing test')
  83. await page.keyboard.press('Shift+Enter')
  84. await page.keyboard.type('continue')
  85. await page.keyboard.press('Enter')
  86. await page.keyboard.press('Shift+Tab')
  87. await page.keyboard.press('Shift+Tab')
  88. await page.keyboard.type('test ok')
  89. await page.keyboard.press('Escape')
  90. const blocks = await page.$$('.ls-block')
  91. expect(blocks).toHaveLength(5)
  92. // active edit
  93. await page.click('.ls-block >> nth=-1')
  94. await page.press('textarea >> nth=0', 'Enter')
  95. await page.fill('textarea >> nth=0', 'test')
  96. for (let i = 0; i < 5; i++) {
  97. await page.keyboard.press('Backspace')
  98. }
  99. await page.keyboard.press('Escape')
  100. await page.waitForTimeout(500)
  101. expect(await page.$$('.ls-block')).toHaveLength(5)
  102. })
  103. test('delete and backspace', async () => {
  104. await createRandomPage(page)
  105. await page.fill(':nth-match(textarea, 1)', 'test')
  106. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('test')
  107. // backspace
  108. await page.keyboard.press('Backspace')
  109. await page.keyboard.press('Backspace')
  110. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  111. // refill
  112. await page.fill(':nth-match(textarea, 1)', 'test')
  113. await page.keyboard.press('ArrowLeft')
  114. await page.keyboard.press('ArrowLeft')
  115. // delete
  116. await page.keyboard.press('Delete')
  117. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('tet')
  118. await page.keyboard.press('Delete')
  119. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  120. await page.keyboard.press('Delete')
  121. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('te')
  122. // TODO: test delete & backspace across blocks
  123. })
  124. test('selection', async () => {
  125. await createRandomPage(page)
  126. await page.fill(':nth-match(textarea, 1)', 'line 1')
  127. await page.press(':nth-match(textarea, 1)', 'Enter')
  128. await page.fill(':nth-match(textarea, 1)', 'line 2')
  129. await page.press(':nth-match(textarea, 1)', 'Enter')
  130. await page.press(':nth-match(textarea, 1)', 'Tab')
  131. await page.fill(':nth-match(textarea, 1)', 'line 3')
  132. await page.press(':nth-match(textarea, 1)', 'Enter')
  133. await page.fill(':nth-match(textarea, 1)', 'line 4')
  134. await page.press(':nth-match(textarea, 1)', 'Tab')
  135. await page.press(':nth-match(textarea, 1)', 'Enter')
  136. await page.fill(':nth-match(textarea, 1)', 'line 5')
  137. await page.keyboard.down('Shift')
  138. await page.keyboard.press('ArrowUp')
  139. await page.keyboard.press('ArrowUp')
  140. await page.keyboard.press('ArrowUp')
  141. await page.keyboard.up('Shift')
  142. await page.waitForTimeout(500)
  143. await page.keyboard.press('Backspace')
  144. expect(await page.$$('.ls-block')).toHaveLength(2)
  145. })
  146. test('template', async () => {
  147. const randomTemplate = randomString(10)
  148. await createRandomPage(page)
  149. await page.fill(':nth-match(textarea, 1)', 'template')
  150. await page.press(':nth-match(textarea, 1)', 'Shift+Enter')
  151. await page.type(':nth-match(textarea, 1)', 'template:: ' + randomTemplate)
  152. await page.press(':nth-match(textarea, 1)', 'Enter')
  153. await page.press(':nth-match(textarea, 1)', 'Enter')
  154. await page.press(':nth-match(textarea, 1)', 'Tab')
  155. await page.fill(':nth-match(textarea, 1)', 'line1')
  156. await page.press(':nth-match(textarea, 1)', 'Enter')
  157. await page.fill(':nth-match(textarea, 1)', 'line2')
  158. await page.press(':nth-match(textarea, 1)', 'Enter')
  159. await page.press(':nth-match(textarea, 1)', 'Tab')
  160. await page.fill(':nth-match(textarea, 1)', 'line3')
  161. await page.press(':nth-match(textarea, 1)', 'Enter')
  162. await page.press(':nth-match(textarea, 1)', 'Enter')
  163. await page.press(':nth-match(textarea, 1)', 'Enter')
  164. expect(await page.$$('.ls-block')).toHaveLength(5)
  165. await page.type(':nth-match(textarea, 1)', '/template')
  166. await page.click('[title="Insert a created template here"]')
  167. // type to search template name
  168. await page.keyboard.type(randomTemplate.substring(0, 3))
  169. await page.click('.absolute >> text=' + randomTemplate)
  170. await page.waitForTimeout(500)
  171. expect(await page.$$('.ls-block')).toHaveLength(8)
  172. })
  173. test('auto completion square brackets', async () => {
  174. await createRandomPage(page)
  175. await page.fill(':nth-match(textarea, 1)', 'Auto-completion test')
  176. await page.press(':nth-match(textarea, 1)', 'Enter')
  177. // [[]]
  178. await page.type(':nth-match(textarea, 1)', 'This is a [')
  179. await page.inputValue(':nth-match(textarea, 1)').then(text => {
  180. expect(text).toBe('This is a []')
  181. })
  182. await page.type(':nth-match(textarea, 1)', '[')
  183. // wait for search popup
  184. await page.waitForSelector('text="Search for a page"')
  185. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  186. // re-enter edit mode
  187. await page.press(':nth-match(textarea, 1)', 'Escape')
  188. await page.click('.ls-block >> nth=-1')
  189. await page.waitForSelector(':nth-match(textarea, 1)', { state: 'visible' })
  190. // #3253
  191. await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
  192. await page.press(':nth-match(textarea, 1)', 'ArrowLeft')
  193. await page.press(':nth-match(textarea, 1)', 'Enter')
  194. await page.waitForSelector('text="Search for a page"', { state: 'visible' })
  195. // type more `]`s
  196. await page.press(':nth-match(textarea, 1)', 'Escape')
  197. await page.type(':nth-match(textarea, 1)', ']')
  198. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  199. await page.type(':nth-match(textarea, 1)', ']')
  200. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]')
  201. await page.type(':nth-match(textarea, 1)', ']')
  202. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('This is a [[]]]')
  203. })
  204. test('auto completion and auto pair', async () => {
  205. await createRandomPage(page)
  206. await page.fill(':nth-match(textarea, 1)', 'Auto-completion test')
  207. await page.press(':nth-match(textarea, 1)', 'Enter')
  208. // {}
  209. await page.type(':nth-match(textarea, 1)', 'type {{')
  210. await page.press(':nth-match(textarea, 1)', 'Escape')
  211. // FIXME: keycode seq is wrong
  212. // expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type {{}}')
  213. // (()
  214. await newBlock(page)
  215. await page.type(':nth-match(textarea, 1)', 'type (')
  216. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type ()')
  217. await page.type(':nth-match(textarea, 1)', '(')
  218. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type (())')
  219. // ``
  220. await newBlock(page)
  221. await page.type(':nth-match(textarea, 1)', 'type `')
  222. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type ``')
  223. await page.type(':nth-match(textarea, 1)', 'code here')
  224. expect(await page.inputValue(':nth-match(textarea, 1)')).toBe('type `code here`')
  225. })
  226. // FIXME: Electron with filechooser is not working
  227. test.skip('open directory', async () => {
  228. await page.click('#sidebar-nav-wrapper >> text=Journals')
  229. await page.waitForSelector('h1:has-text("Open a local directory")')
  230. await page.click('h1:has-text("Open a local directory")')
  231. // await page.waitForEvent('filechooser')
  232. await page.keyboard.press('Escape')
  233. await page.click('#sidebar-nav-wrapper >> text=Journals')
  234. })