scripteditor.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /*
  2. * Copyright 2018-2022 Elyra Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. describe('Script Editor tests', () => {
  17. before(() => {
  18. cy.resetJupyterLab();
  19. cy.bootstrapFile('helloworld.py'); // load python file used to check existing contents
  20. cy.bootstrapFile('helloworld.r'); // load R file used to check existing contents
  21. });
  22. after(() => {
  23. // delete files created for testing
  24. cy.deleteFile('untitled*.py');
  25. cy.deleteFile('untitled*.r');
  26. cy.deleteFile('helloworld.py'); // delete python file used for testing
  27. cy.deleteFile('helloworld.r'); // delete R file used for testing
  28. // Delete runtime configuration used for testing
  29. cy.exec('elyra-metadata remove runtimes --name=kfp_test_runtime', {
  30. failOnNonZeroExit: false
  31. });
  32. });
  33. // Python Tests
  34. it('opens blank Python editor from launcher', () => {
  35. cy.createNewScriptEditor('Python');
  36. cy.get('.lm-TabBar-tab[data-type="document-title"]');
  37. });
  38. it('check Python editor tab right click content', () => {
  39. checkRightClickTabContent('Python');
  40. });
  41. it('close editor', () => {
  42. cy.closeTab(-1);
  43. });
  44. it('open Python file with expected content', () => {
  45. openFileAndCheckContent('py');
  46. });
  47. it('opens blank Python editor from menu', () => {
  48. cy.findByRole('menuitem', { name: /file/i }).click();
  49. cy.findByText(/^new$/i).click();
  50. cy.get(
  51. '[data-command="script-editor:create-new-python-editor"] > .lm-Menu-itemLabel'
  52. ).click();
  53. });
  54. it('check toolbar and its content for Python file', () => {
  55. checkToolbarContent();
  56. });
  57. it('check kernel dropdown has Python option', () => {
  58. cy.get('.elyra-ScriptEditor .jp-Toolbar select > option[value*=python]');
  59. });
  60. it('click the Run as Pipeline button should display dialog', () => {
  61. // Install runtime configuration
  62. cy.installRuntimeConfig({ type: 'kfp' });
  63. // Click Run as Pipeline button
  64. cy.findByText(/run as pipeline/i).click();
  65. // Check for expected dialog title
  66. cy.get('.jp-Dialog-header').should('have.text', 'Run file as pipeline');
  67. // Dismiss dialog
  68. cy.get('button.jp-mod-reject').click();
  69. // Close editor tab
  70. cy.closeTab(-1);
  71. });
  72. it('click the Run as Pipeline button on unsaved file should display save dialog', () => {
  73. // Create new python editor
  74. cy.createNewScriptEditor('Python');
  75. // Add some text to the editor
  76. cy.get('span[role="presentation"]').type('print("test")');
  77. cy.wait(500);
  78. dismissAssistant();
  79. // Click Run as Pipeline button
  80. cy.findByText(/run as pipeline/i).click();
  81. // Check expected save and submit dialog message
  82. cy.contains('.jp-Dialog-header', /this file contains unsaved changes/i);
  83. // Dismiss save and submit dialog
  84. cy.get('button.jp-mod-reject').click();
  85. // Close editor tab
  86. cy.closeTab(-1);
  87. // Dismiss save your work dialog by discarding changes
  88. cy.get('button.jp-mod-warn').click();
  89. });
  90. // check for new output console and scroll up/down buttons on output kernel
  91. it('opens new output console', () => {
  92. openFile('py');
  93. cy.get('button[title="Run"]').click();
  94. cy.get('[id=tab-ScriptEditor-output]').should(
  95. 'have.text',
  96. 'Console Output'
  97. );
  98. cy.get('button[title="Top"]').should('be.visible');
  99. cy.get('button[title="Bottom"]').should('be.visible');
  100. //close console tab
  101. cy.closeTab(-1);
  102. // Close editor tab
  103. cy.closeTab(-1);
  104. });
  105. // test to check if output kernel has expected output
  106. it('checks for valid output', () => {
  107. openFile('py');
  108. cy.get('button[title="Run"]').click();
  109. cy.get('.elyra-ScriptEditor-OutputArea-output').should(
  110. 'have.text',
  111. 'Hello Elyra\n'
  112. );
  113. //close console tab
  114. cy.closeTab(-1);
  115. // Close editor tab
  116. cy.closeTab(-1);
  117. });
  118. // test to check if output kernel has Error message for invalid code
  119. it('checks for Error message', () => {
  120. cy.createNewScriptEditor('Python');
  121. cy.get('span[role="presentation"]').type('print"test"');
  122. cy.wait(500);
  123. dismissAssistant();
  124. cy.get('button[title="Run"]').click();
  125. cy.findByText(/Error : SyntaxError/i).should('be.visible');
  126. //close console tab
  127. cy.closeTab(-1);
  128. // Close editor tab
  129. cy.closeTab(-1);
  130. // Dismiss save your work dialog by discarding changes
  131. cy.get('button.jp-mod-warn').click();
  132. });
  133. // R Tests
  134. it('opens blank R file from launcher', () => {
  135. cy.createNewScriptEditor('R');
  136. cy.get('.lm-TabBar-tab[data-type="document-title"]');
  137. });
  138. it('check R editor tab right click content', () => {
  139. checkRightClickTabContent('R');
  140. });
  141. it('close R editor', () => {
  142. cy.closeTab(-1);
  143. });
  144. it('open R file with expected content', () => {
  145. openFileAndCheckContent('r');
  146. });
  147. it('check icons', () => {
  148. // Check file menu editor contents
  149. cy.findByRole('menuitem', { name: /file/i }).click();
  150. cy.findByText(/^new$/i).click();
  151. cy.get(
  152. '[data-command="script-editor:create-new-r-editor"] svg[data-icon="elyra:rIcon"]'
  153. );
  154. cy.get(
  155. '[data-command="script-editor:create-new-python-editor"] svg[data-icon="elyra:pyIcon"]'
  156. );
  157. // Check python icons from launcher & file explorer
  158. cy.get(
  159. '.jp-LauncherCard[data-category="Elyra"][title="Create a new Python Editor"] svg[data-icon="elyra:pyIcon"]'
  160. ).click();
  161. cy.get(
  162. '#filebrowser [title*="Name: untitled1.py"] svg[data-icon="elyra:pyIcon"]'
  163. );
  164. cy.closeTab(-1);
  165. // Check r icons from launcher & file explorer
  166. cy.get(
  167. '.jp-LauncherCard[data-category="Elyra"][title="Create a new R Editor"] svg[data-icon="elyra:rIcon"]'
  168. ).click();
  169. cy.get(
  170. '#filebrowser [title*="Name: untitled1.r"] svg[data-icon="elyra:rIcon"]'
  171. );
  172. cy.closeTab(-1);
  173. });
  174. it('opens blank R file from menu', () => {
  175. cy.findByRole('menuitem', { name: /file/i }).click();
  176. cy.findByText(/^new$/i).click();
  177. cy.get(
  178. '[data-command="script-editor:create-new-r-editor"] > .lm-Menu-itemLabel'
  179. ).click();
  180. });
  181. it('check toolbar and its content for R file', () => {
  182. checkToolbarContent();
  183. });
  184. });
  185. // ------------------------------
  186. // ----- Utility Functions
  187. // ------------------------------
  188. const checkToolbarContent = (): void => {
  189. cy.get('.elyra-ScriptEditor .jp-Toolbar');
  190. // check save button exists and icon
  191. cy.get('button[title="Save file contents"]');
  192. cy.get('svg[data-icon="ui-components:save"]');
  193. // check run button exists and icon
  194. cy.get('button[title="Run"]');
  195. cy.get('svg[data-icon="ui-components:run"]');
  196. // check stop button exists and icon
  197. cy.get('button[title="Stop"]');
  198. cy.get('svg[data-icon="ui-components:stop"]');
  199. // check select kernel dropdown exists
  200. cy.get('.elyra-ScriptEditor .jp-Toolbar select');
  201. // check Run as Pipeline button exists
  202. cy.contains('Run as Pipeline');
  203. };
  204. const checkRightClickTabContent = (fileType: string): void => {
  205. // Open right-click context menu
  206. cy.get('.lm-TabBar-tab[data-type="document-title"]').rightclick({
  207. force: true
  208. });
  209. // Check contents of each menu item
  210. cy.get('[data-command="application:close"] > .lm-Menu-itemLabel').contains(
  211. 'Close Tab'
  212. );
  213. cy.get(
  214. '[data-command="application:close-other-tabs"] > .lm-Menu-itemLabel'
  215. ).contains('Close All Other Tabs');
  216. cy.get(
  217. '[data-command="application:close-right-tabs"] > .lm-Menu-itemLabel'
  218. ).contains('Close Tabs to Right');
  219. cy.get(
  220. '[data-command="filemenu:create-console"] > .lm-Menu-itemLabel'
  221. ).contains('Create Console for Editor');
  222. cy.get('[data-command="docmanager:rename"] > .lm-Menu-itemLabel').contains(
  223. `Rename ${fileType} File…`
  224. );
  225. cy.get('[data-command="docmanager:delete"] > .lm-Menu-itemLabel').contains(
  226. `Delete ${fileType} File`
  227. );
  228. cy.get('[data-command="docmanager:clone"] > .lm-Menu-itemLabel').contains(
  229. `New View for ${fileType} File`
  230. );
  231. cy.get(
  232. '[data-command="docmanager:show-in-file-browser"] > .lm-Menu-itemLabel'
  233. ).contains('Show in File Browser');
  234. cy.get(
  235. '[data-command="__internal:context-menu-info"] > .lm-Menu-itemLabel'
  236. ).contains('Shift+Right Click for Browser Menu');
  237. // Dismiss menu
  238. cy.get(
  239. '[data-command="docmanager:show-in-file-browser"] > .lm-Menu-itemLabel'
  240. ).click();
  241. };
  242. //open helloworld.py using file-> open from path
  243. const openFile = (fileExtension: string): void => {
  244. cy.findByRole('menuitem', { name: /file/i }).click();
  245. cy.findByText(/^open from path$/i).click({ force: true });
  246. // Search for helloworld file and open
  247. cy.get('input#jp-dialog-input-id').type(`/helloworld.${fileExtension}`);
  248. cy.get('.p-Panel .jp-mod-accept').click();
  249. };
  250. //open file and check contents
  251. const openFileAndCheckContent = (fileExtension: string): void => {
  252. openFile(fileExtension);
  253. // Ensure that the file contents are as expected
  254. cy.get('span[role="presentation"]').should($span => {
  255. expect($span.get(0).innerText).to.eq('print("Hello Elyra")');
  256. });
  257. // Close the file editor
  258. cy.closeTab(-1);
  259. };
  260. // Dismiss LSP code assistant box if visible
  261. const dismissAssistant = (): void => {
  262. cy.get('body').then($body => {
  263. if ($body.find('.lsp-completer').length > 0) {
  264. // Dismiss code assistant box
  265. cy.get('.CodeMirror-lines')
  266. .first()
  267. .type('{esc}');
  268. }
  269. });
  270. };