codesnippetfromselectedcells.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. const cellSelector =
  17. 'div.CodeMirror-lines[role="presentation"] > div[role="presentation"]';
  18. describe('Code snippet from cells tests', () => {
  19. beforeEach(() => {
  20. cy.resetJupyterLab();
  21. // Create new python notebook
  22. cy.get(
  23. '.jp-LauncherCard[data-category="Notebook"][title="Python 3 (ipykernel)"]'
  24. ).click();
  25. cy.wait(2000);
  26. });
  27. it('test empty cell', () => {
  28. cy.get(cellSelector)
  29. .first()
  30. .rightclick();
  31. cy.wait(2000);
  32. cy.get(
  33. 'li.lm-Menu-item[data-command="codesnippet:save-as-snippet"]'
  34. ).should('have.class', 'p-mod-disabled');
  35. });
  36. it('test 1 cell', () => {
  37. // Create new cell
  38. cy.get(
  39. '.jp-NotebookPanel-toolbar > div:nth-child(2) > button:nth-child(1)'
  40. ).click();
  41. cy.wait(2000);
  42. populateCells();
  43. cy.get(cellSelector)
  44. .first()
  45. .rightclick({
  46. force: true
  47. });
  48. cy.wait(2000);
  49. cy.get(
  50. 'li.lm-Menu-item[data-command="codesnippet:save-as-snippet"]'
  51. ).click();
  52. cy.wait(2000);
  53. // Verify snippet editor contents
  54. cy.get('span[role="presentation"]:visible').should(
  55. 'have.text',
  56. 'print("test")'
  57. );
  58. });
  59. it('test 2 cells', () => {
  60. // Create new cells
  61. cy.get(
  62. '.jp-NotebookPanel-toolbar > div:nth-child(2) > button:nth-child(1)'
  63. ).click();
  64. cy.wait(2000);
  65. populateCells();
  66. // Select all cells
  67. cy.get(
  68. ':nth-child(1) > .jp-Cell-inputWrapper > .jp-InputArea > .jp-InputPrompt'
  69. )
  70. .first()
  71. .click({
  72. shiftKey: true
  73. });
  74. cy.get('div.lm-Widget.p-Widget.jp-InputPrompt.jp-InputArea-prompt:visible')
  75. .first()
  76. .rightclick({
  77. force: true
  78. });
  79. cy.wait(2000);
  80. cy.get(
  81. 'li.lm-Menu-item[data-command="codesnippet:save-as-snippet"]'
  82. ).click();
  83. cy.wait(2000);
  84. // Verify snippet editor contents
  85. cy.get(
  86. '.elyra-form-code > .CodeMirror > .CodeMirror-scroll span[role="presentation"]:contains("test")'
  87. ).should('have.length', 2);
  88. });
  89. });
  90. // ------------------------------
  91. // ----- Utility Functions
  92. // ------------------------------
  93. // Populate cells
  94. const populateCells = (): void => {
  95. cy.get('span[role="presentation"]').each(cell => {
  96. cy.get(cell).type('print("test")');
  97. cy.dismissAssistant('notebook');
  98. });
  99. };